使用Selenium永久修改Chrome配置[Python版本]

使用Selenium可以非常方便的模仿用户操作浏览器的行为(Chrome,Firefox…)等,对于如何选择页面元素,以及如何操作各种页面元素(输入框,下拉框)类型Selenium支持的都非常到位,这些基本的操作本文不再阐述,具体可以参考:http://www.selenium.org.cn/

网上找到大多数能找到的教程都是基于session机制做的演示,既脚本中对浏览器的设置只对当前sessio能有效,当重启浏览器时设置就会被还原,本文主要讲述如何永久设置Chrome浏览器的配置

很多人学习python,不知道从何学起。
很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手。
很多已经做案例的人,却不知道如何去学习更加高深的知识。
那么针对这三类人,我给大家提供一个好的学习平台,免费领取视频教程,电子书籍,以及课程的源代码!
QQ群:101677771

操作说明

假设需要设置chrome的一下配置:

核心代码以及注释如下:

# -*- coding:utf-8 -**
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.select import Select from selenium.webdriver.support.wait import WebDriverWait __author__ = 'eric.sun' from selenium import webdriver # 永久修改chrome配置 option = webdriver.ChromeOptions() option.add_experimental_option("excludeSwitches",['enable-automation']) # 运行需要注意两个问题 (1)设置user-data-dir 为chrome浏览器用户数据的目录 (2)需要先把浏览器关闭 option.add_argument("user-data-dir=c:\\Users\\admin\\AppData\\Local\\Google\\Chrome\\User Data") driver = webdriver.Chrome(options=option) # 执行完modify后,重新启动浏览器发现该值已经被设置 def modify(): driver.get("chrome://flags") # 通过xpath找到页面的下拉框并进行设置 s1=Select(driver.find_element_by_xpath('//*[@id="disable-accelerated-2d-canvas"]/div/div[2]/select')) s1.select_by_value("disabled") s1.select_by_visible_text("Disabled") # 等待relunch 按钮出现 wait = WebDriverWait(driver,60) wait.until(expected_conditions.presence_of_element_located((By.XPATH,'//*[@id="experiment-restart-button"]'))) if __name__ == '__main__': modify()

你可能感兴趣的:(使用Selenium永久修改Chrome配置[Python版本])