繁体   English   中英

Python:Function相当于firefoxdriver中的chromedriver。 chromedriver --> firefoxdriver(firefoxdriver 给我语法错误)

[英]Python: Function equivalent to chromedriver in firefoxdriver. chromedriver --> firefoxdriver (firefoxdriver gives me syntax error)

我有这两个功能:

def get_chromedriver(headless = False):
    import os
    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    options = Options()
    options.binary_location = '/usr/bin/google-chrome'
    options.add_argument('user-data-dir=' + os.environ['HOME'] + '/.config/chromedriver')
    options.add_experimental_option('detach', True)
    options.add_experimental_option('excludeSwitches', ['enable-automation'])
    options.add_experimental_option('useAutomationExtension', False)
    options.headless = headless
    driver = webdriver.Chrome(executable_path='/usr/bin/chromedriver', options=options)
    return driver

def get_firefoxdriver(headless = False):
    import os
    from selenium import webdriver
    from selenium.webdriver.firefox.options import Options
    options = Options()
    options.binary_location = '/usr/bin/firefox'
    options.add_argument('user-data-dir=' + os.environ['HOME'] + '/.config/firefoxdriver')
    options.add_experimental_option('detach', True)
    options.add_experimental_option('excludeSwitches', ['enable-automation'])
    options.add_experimental_option('useAutomationExtension', False)
    options.headless = headless
    driver = webdriver.Firefox(executable_path='/usr/bin/geckodriver', options=options)
    return driver

get_chromedriver function 可以完美运行,但 get_firefoxdriver 不能,它是 get_chromedriver function 的副本。
如何使 get_firefoxdriver function 在功能上等同于 get_chromedriver function,除了使用的路径、webdriver 和浏览器?

Firefox webdriver 没有与 Chrome webdriver 相同的实例方法。 我看到在您的 Firefox 驱动程序创建 function 中,您正在调用“分离”和“exludeSwitches”选项,但是如果您查看 Z763F7F1AEC350CD1A46238D1D5C3C29 选项,则在 Chrome 文档中找到相同的可用文档选项。

使用每个驱动程序都有利有弊,并且在每种情况下,解决方案都应与用例相匹配。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM