Can I run Selenium ChromeDriver with cookies from actual Chrome installation?
所以我正在Ubuntu机器上使用IntelliJ IDEA + chromedriver运行硒测试...
在安装Google Chrome时,我已经登录了一个帐户,例如Google。 在硒测试中访问http://accounts.google.com时,我进入登录页面,而不是实际的帐户管理页面。
我敢肯定,我不完全了解Selenium和chrome驱动程序的确切运行方式,但是我确实记得,将" Google Chrome安装在默认位置"是使用Selenium测试运行的条件之一。 chrome驱动程序。
我可以在安装的浏览器的上下文中运行,即可以访问我的浏览器历史记录和Cookie吗?
Selenium每次打开浏览器(Chrome / Firefox / IE)时,都会打开该浏览器的规范形式。作为测试人员,您可以使用DesiredCapabilities对象设置浏览器首选项,对于chrome,还可以使用ChromeOptions对象来传递chrome命令行参数。
选择您的个人资料
1 2 | ChromeOptions options = new ChromeOptions(); options.addArguments("user-data-dir=/path/to/your/custom/profile"); |
有关chrome驱动程序功能的更多信息:
https://sites.google.com/a/chromium.org/chromedriver/capabilities
有关chrome的user-data-dir命令行选项的更多信息:
https://www.chromium.org/user-experience/user-data-directory
试试这个:
1 2 3 4 5 | from selenium import webdriver options = webdriver.ChromeOptions() options.add_argument("user-data-dir=C:\\\\Path") #Path to your chrome profile w = webdriver.Chrome(executable_path="C:\\\\Users\\\\chromedriver.exe", chrome_options=options) |
要查找chrome配置文件数据的路径,您需要在地址栏中输入chrome:// version /。对于前。我的显示为C:\ Users \ pc \ AppData \ Local \ Google \ Chrome \ User Data \ Default,要在脚本中使用它,我必须排除\ Default \,所以最终只得到C:\ Users \ pc \ AppData \ Local \ Google \ Chrome \ User Data。
来源:如何使用Python Selenium Webdriver在Chrome中加载默认配置文件?