如何使用Python将选项传递给Selenium Chrome驱动程序?

q43xntqr  于 2023-06-03  发布在  Go
关注(0)|答案(5)|浏览(208)

Selenium文档提到Chrome webdriver可以使用ChromeOptions的示例,但我不知道如何创建ChromeOptions
我希望将--disable-extensions标志传递给Chrome。

f45qwnt8

f45qwnt81#

找到chrome Options class in the Selenium source code了。
创建Chrome驱动程序示例的用法:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("--disable-extensions")
driver = webdriver.Chrome(chrome_options=chrome_options)
ecfdbz9o

ecfdbz9o2#

我就是这么做的。

from selenium import webdriver

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--disable-extensions')

chrome = webdriver.Chrome(chrome_options=chrome_options)
irlmq6kh

irlmq6kh3#

禁用chrome扩展的代码,使用DesiredCapabilities设置浏览器标志:

desired_capabilities['chromeOptions'] = {
    "args": ["--disable-extensions"],
    "extensions": []
}
webdriver.Chrome(desired_capabilities=desired_capabilities)
4ioopgfo

4ioopgfo4#

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument('--disable-logging')

# Update your desired_capabilities dict withe extra options.
desired_capabilities.update(options.to_capabilities())
driver = webdriver.Remote(desired_capabilities=options.to_capabilities())

desired_capabilities选项.to_capabilities()都是字典。您可以使用dict.update()方法将options添加到主集中。

w8f9ii69

w8f9ii695#

如果您使用的是普通chromedriver:

pip3 install selenium

示例代码:

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

def main():
    # Set the path to the chromedriver
    chromedriver_path = "path/to/chromedriver"

    # Create ChromeOptions
    options = Options()

    # Your Options here....
    # options.add_argument("--headless")  # Run in headless mode
    # options.add_argument("--disable-gpu")  # Disable GPU acceleration

    # Create a ChromeDriver
    driver = webdriver.Chrome(executable_path=chromedriver_path, options=options)

    # Open Website
    driver.get("https://www.google.com")

    # your code.. clicking on buttons etc..

    # Close the ChromeDriver
    driver.quit()

if __name__ == "__main__":
    main()

如果使用未检测到的chromedriver:

pip3 install undetected-chromedriver

示例代码:

import undetected_chromedriver as uc

def main():
    # Set the path to the chromedriver executable
    chromedriver_path = "path/to/chromedriver"

    # Create options object
    options = uc.ChromeOptions()

    # Add options if needed
    # options.add_argument("--headless")  # Run in headless mode
    # options.add_argument("--disable-gpu")  # Disable GPU acceleration

    # Create an instance of Undetected ChromeDriver with options
    driver = uc.Chrome(executable_path=chromedriver_path, options=options)

    try:
        # Open Google
        driver.get("https://www.google.com")

        # Rest of your code for interacting with the Google page

    except Exception as ex:
        print(ex)

    finally:
        # Close the ChromeDriver
        driver.quit()

if __name__ == "__main__":
    main()

以下是我经常使用的一些Chrome选项:

options.add_argument(f"--window-size=1366,768")
    options.add_argument(f'--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36')
    options.add_argument('--disable-blink-features=AutomationControlled')
    options.add_argument("--disable-extensions")
    options.add_argument("--proxy-server='direct://'")
    options.add_argument("--proxy-bypass-list=*")
    options.add_argument('--ignore-certificate-errors')
    options.add_argument("--password-store=basic")
    options.add_argument("--no-sandbox")
    options.add_argument("--disable-dev-shm-usage")
    options.add_argument("--disable-extensions")
    options.add_argument("--enable-automation")
    options.add_argument("--disable-browser-side-navigation")
    options.add_argument("--disable-web-security")
    options.add_argument("--disable-dev-shm-usage")
    options.add_argument("--disable-infobars")
    options.add_argument("--disable-gpu")
    options.add_argument("--disable-setuid-sandbox")
    options.add_argument("--disable-software-rasterizer")

    options.add_argument(f"--user-data-dir=PATH_TO_CHROME_PROFILE")
    options.add_argument('--proxy-server=IP_ADRESS:PORT')

相关问题