selenium使用chrome进行登录时如何关闭弹出的密码提示框

最近在使用chrome登录网站的时候总有密码保存提示框(并不是所有的都会有密码保存提示框)

其实只需要设置启动chrome的相关参数就可以避免这种问题,参数:

prefs[“credentials_enable_service”] = False
prefs[“profile.password_manager_enabled”] = False

以上两个参数就可以避免密码提示框的弹出。

以下是测试环境及代码:

环境:win7 64位

python:2.7 64位

当然chrome得和chromedriver.exe版本相对应(既然弹出了说明没问题哈 这个条件废话了)

coding:utf-8

import os
import sys
reload(sys)
sys.setdefaultencoding(‘utf8’)
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By

os.chdir(“C:\python27”)#chromedriver.exe存放的路径

options = webdriver.ChromeOptions()
prefs = {“”:”“}
prefs[“credentials_enable_service”] = False
prefs[“profile.password_manager_enabled”] = False
options.add_experimental_option(“prefs”, prefs)
browser = webdriver.Chrome(chrome_options=options)
browser.get(r’https://www.csdn.net/‘)

sleep(3)

  • 看到代码的时间如果久远了,可能csdn的的网页结构发生调整,因此可能无法定位到控件

  • 即需要重新获得控件的xpath

browser.find_element_by_xpath(‘//*[@id=”login”]/a[1]’).click()
sleep(2)

browser.find_element_by_xpath(‘//*[@id=”username”]’).send_keys(“”)
browser.find_element_by_xpath(‘//*[@id=”password”]’).send_keys(“”)
browser.find_element_by_xpath(‘//*[@id=”fm1”]/input[7]’).click()

猜你喜欢

转载自blog.csdn.net/zwq912318834/article/details/81263618