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