简体   繁体   English

Selenium arsparse 用于无头 chromedriver

[英]Selenium arsparse for headless chromedriver

So I have built a testing framework and I am able to run it Headless in the normal fashion.所以我已经建立了一个测试框架,我能够以正常的方式无头运行它。

    self.chrome_options = Options()
    self.chrome_options.add_argument('--headless')
    self.driver = webdriver.Chrome(options=self.chrome_options,
                                   service=Service(
                                       ChromeDriverManager().install())
                                   )

What I am looking for, as I built this for my colleagues and they don't want to have to go in and comment out the line self.chrome_options.add_argument('--headless') every time they don't want the tests to run headless, is an argsparse so that they can run something like test_file.py --headless.我正在寻找什么,因为我为我的同事构建了这个,他们不想在每次他们不想要测试时都输入 go 并注释掉self.chrome_options.add_argument('--headless')行无头运行,是一个argsparse ,以便他们可以运行类似 test_file.py --headless 的东西。

So far I have this到目前为止我有这个

args = self.args_cmds()
    if args['--headless:'] == 'True':
        self.chrome_options.headless = True
    else:
        self.chrome_options.headless = False

    self.chrome_options = Options()
    self.chrome_options.add_argument('window-size=1920x1480')
    self.driver = webdriver.Chrome(options=self.chrome_options,
                                   service=Service(
                                       ChromeDriverManager().install())
                                   )

    self.pixel = 4.9667

def args_cmds(self):
    parser = argparse.ArgumentParser()
    parser.add_argument('--headless:', help='Specifies if you want a window to open or not.')
    args = vars(parser.parse_args())
    return args

The function def args_cmd(self): is called under the if __name__ == "__main__": section. function def args_cmd(self):if __name__ == "__main__":部分下调用。 Yet when I run this I get the error error: argument --headless:: expected one argument然而,当我运行它时,我得到了错误error: argument --headless:: expected one argument

If anyone has any ideas that would be great.如果有人有任何想法,那就太好了。 Thanks in advance.提前致谢。

Try to specify that --headless argument is a boolean argument and has default value True :尝试指定--headless参数是 boolean 参数并且具有默认值True

def args_cmds(self):
    parser = argparse.ArgumentParser()
    parser.add_argument('--headless:', action='store_true', help='Specifies if you want a window to open or not.')
    args = parser.parse_args()
    return args

and then use args as然后使用args作为

args = self.args_cmds()
if args.headless: 
    self.chrome_options.headless = True
else:
    self.chrome_options.headless = False

You can also try to execute your script as您也可以尝试将脚本执行为

script.py --headless 'True'

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

相关问题 Selenium 仅在 Headless 中运行 ChromeDriver - Selenium ONLY Runs ChromeDriver in Headless 带有 Chromedriver 的 Selenium 仅适用于无头模式 - Selenium with Chromedriver only works with headless mode Docker:使用带无头Selenium Chromedriver的容器 - Docker: using container with headless Selenium Chromedriver 无头Chrome浏览器-使用Selenium和ChromeDriver - Headless Chrome Browser - Using Selenium and ChromeDriver Selenium 与无头 chromedriver 无法刮取 web 数据? - Selenium with headless chromedriver not able to scrape web data? Python Selenium Chromedriver 无法使用 --headless 选项 - Python Selenium Chromedriver not working with --headless option Python 3 硒 | 剪贴板不适用于 Windows 上的无头 chromedriver - Python 3 Selenium | Clipboard not working on headless chromedriver on Windows Headless Python Selenium 显示错误 'chromedriver' 可执行文件需要在 PATH 中 - Headless Python Selenium Shows Error 'chromedriver' executable needs to be in PATH 在我的覆盆子pi上运行无头chromedriver时,Selenium崩溃了 - Selenium crashing when running headless chromedriver on my raspberry pi Selenium 无头浏览器在使用未检测到的 chromedriver 时出现 Cloudflare 错误 - Selenium headless browser gives Cloudflare error when using undetected chromedriver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM