且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Selenium Webdriver Python 3中使用Chrome配置文件

更新时间:2022-12-05 19:32:16

如果您要打开 Chrome浏览器会话,请根据您的问题和代码试用以下选项:

As per your question and your code trials if you want to open a Chrome Browsing Session here are the following options:

  • 要使用默认的 Chrome配置文件:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\\Users\\AtechM_03\\AppData\\Local\\Google\\Chrome\\User Data\\Default")
driver = webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe', chrome_options=options)
driver.get("https://www.google.co.in")

  • 注意:您的默认Chrome配置文件将包含许多书签,扩展名,主题,cookie等. Selenium 可能无法加载.因此,按照***做法,为您的 @Test 创建一个新的 chrome配置文件,并在配置文件中存储/保存/配置所需的数据.

  • Note: Your default chrome profile would contain a lot of bookmarks, extensions, theme, cookies etc. Selenium may fail to load it. So as per the best practices create a new chrome profile for your @Test and store/save/configure within the profile the required data.

    要使用自定义的 Chrome配置文件:

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    options = Options()
    options.add_argument("user-data-dir=C:\\Users\\AtechM_03\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 2")
    driver = webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe', chrome_options=options)
    driver.get("https://www.google.co.in")
    

  • 在这里您将找到有关如何进行详细讨论通过Python打开Chrome配置文件