[英]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.