问题描述
我通过设置以下参数创建了无头的webdriver chrome浏览器:
chrome_options.add_argument("--headless")
,然后使用以下命令打开浏览器:
driver = webdriver.Chrome(executable_path=os.path.abspath("chromedriver"), chrome_options=chrome_options)
一旦满足条件,是否可以使浏览器出现?我尝试使用以下方法再次删除该属性:
chrome_options.arguments.remove("--headless")
但是那什么也没做.
启动--headless
google-chrome 实例的问题:
chrome_options.add_argument("--headless")
此外,将不可能在同一会话中再次显示浏览上下文.
原因
当您配置 ChromeDriver 使用ChromeOptions()
发起headless 启动新的 Chrome浏览器会话的过程将配置烘焙到 chromedriver 可执行文件中,并将持续到 WebDriver的生命周期,并保持不可修改.因此,您可以修改当前正在执行的 WebDriver 实例的 ChromeOptions .
即使您能够提取 ChromeDriver 和 ChromeSession 属性,例如来自已启动的 ChromeDriver 和 Chrome浏览器会话的会话ID ,和其他会话属性,您仍然不会能够更改 ChromeDriver 的属性集.
一种更简洁的方法是在tearDown(){}
方法中调用 driver.quit()
来关闭并销毁当前的 ChromeDriver 和 Chrome浏览器实例,然后使用新的配置集覆盖一组新的 ChromeDriver 和 Chrome浏览器实例.
参考文献
您可以在以下位置找到一些相关的讨论:
I have created a headless webdriver chrome browser by setting this argument:
chrome_options.add_argument("--headless")
and then opening the browser using:
driver = webdriver.Chrome(executable_path=os.path.abspath("chromedriver"), chrome_options=chrome_options)
Is it possible to make the browser appear once a condition is met? I have tried removing the attribute again using:
chrome_options.arguments.remove("--headless")
but that does not do anything.
As you have initiated a --headless
google-chrome instance by setting:
chrome_options.add_argument("--headless")
further, it won't be possible to make the Browsing Context visible again within the same session.
Reason
When you configure ChromeDriver using ChromeOptions()
to initiate headless in the process of initiating a new Chrome Browsing Session the configuration gets baked into the chromedriver executable and will persist till the lifetime of the WebDriver and remains uneditable. So you modify the ChromeOptions of the WebDriver instance which is currently in execution.
Even if you are able to extract the ChromeDriver and ChromeSession attributes e.g. Session ID, Cookies and other session attributes from the already initiated ChromeDriver and Chrome Browsing Session still you won't be able to change the set of attributes of the ChromeDriver.
A cleaner way would be to call driver.quit()
within tearDown(){}
method to close and destroy the current ChromeDriver and Chrome Browser instances gracefully and then span a new set of ChromeDriver and Chrome Browser instance with the new set of configurations.
References
You can find a couple of relevant discussions in:
- How to set selenium webdriver from headless mode to normal mode within the same session?
- Change ChromeOptions in an existing webdriver
- How do I make Chrome Headless after I login manually
这篇关于如何使无头浏览器可见的Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!