参数:--headless
用途:启用无界面模式
参数:--disable-popup-blocking
用途:禁用弹出拦截
无界面模式示例:
1 from selenium import webdriver 2 chrome_options = webdriver.ChromeOptions() 3 #启用无界面模式 4 chrome_options.add_argument('--headless') 5 driver = webdriver.Chrome(chrome_options=chrome_options) 6 url = "file:///E:/log/index.html" 7 driver.get(url) 8 keys = [] 9 for i in range(1,13): 10 #使用format代替%s 11 key = driver.find_element_by_xpath('//*[@id="statisticsTable"]/thead/tr/th[{id}]/div'.format(id = i)).text 12 keys.append(key) 13 14 valuestr = driver.find_element_by_xpath('//*[@id="statisticsTable"]/tbody[2]/tr').text 15 values = valuestr.split(" ") 16 #字典推导式,in后面必须加元祖或zip的迭代器等 17 18 dict = {key: value for key,value in zip(keys, values)} 19 print(dict) 20 21 driver.close()
参考:
https://www.cnblogs.com/carlvine/articles/9151837.html
https://www.cnblogs.com/roystime/p/7117515.html (无界面浏览)
https://www.cnblogs.com/xiaoxiao-niao/p/7765627.html (Chrome启动参数)
https://mp.weixin.qq.com/s/038NwipSwT5GgIykm09m7Q (format代替%s等)
1 def open(browser="Chrome",url="http://10.138.61.61/PostLoan/BILLDUN/DUNLETTERGENERATION.ASPX"): 2 options = webdriver.ChromeOptions() 3 options.add_experimental_option("excludeSwitches",["ignore-certificate-errors"]) 4 option.add_argument('--user-data-dir=C:\\Users\FF\AppData\Local\Google\Chrome\\User Data') #设置成用户自己的数据目录 5 driver = eval("webdriver.%s()" %browser) 6 driver.get(url) 7 driver.find_element_by_id("txtUserName").send_keys("fanyw") 8 driver.find_element_by_id("txtPwd").send_keys("1234567b") 9 sleep(1) 10 # driver.find_element_by_id("btnLogin").click() 11 print("I was at the chrome %s" %ctime()) 12 13 14 def startFirefoxWithSpecificLocation(): 15 """启动安装在 非 默认位置的Firefox浏览器,并自动转到 百度 首页""" 16 binary = FirefoxBinary('D:\Program Files\Mozilla Firefox\\firefox.exe') 17 driver = webdriver.Firefox(firefox_binary=binary) 18 driver.get("http://10.138.61.61/PostLoan/BILLDUN/DUNLETTERGENERATION.ASPX")