首页 > 解决方案 > 使用 Python 执行 Chrome 在 Selenium 中无头,但抛出错误

问题描述

根据这里的答案,我试图让 Chrome 在我的脚本中无头运行。

下面的代码片段在一个名为的函数内,该函数login()登录到我们的ERP 系统

if headless == True:
    options = Options()
    options.headless = True
    #Load webdriver
    driver = webdriver.Chrome(options=options, executable_path=r'C:/Users/d.kelly/Desktop/Python/chromedriver_win32/chromedriver.exe')
if headless == False:
    driver = webdriver.Chrome('C:/Users/d.kelly/Desktop/Python/chromedriver_win32/chromedriver.exe')
window_before_login = driver.window_handles[0]
### Removed Code Block that fills out login form and clicks 'Login' button ###
# Switch to new window ERP (PLEX)  launches and close original blank one no longer needed.
window_before_login = driver.window_handles[0]
window_title = driver.title
driver.switch_to.window(window_before_login)
driver.close()
driver.switch_to.window(driver.window_handles[0])

当我这样调用我的函数时:

login(headless=False)

它不会引发任何错误,并且我的整个脚本执行得很好。

当我这样调用我的函数时:

def login(headless=True)

我收到以下错误:

DevTools listening on ws://127.0.0.1:57567/devtools/browser/69f9e357-dccf-4e38-8d6b-78030462379a
[0204/072436.206:INFO:CONSOLE(6)] "Error parsing a meta element's content: ';' is not a valid key-value pair separator. Please use ',' instead.", source: https://test.plexonline.com/modules/systemadministration/login/index.aspx? (6)
[0204/072437.699:INFO:CONSOLE(6)] "Error parsing a meta element's content: ';' is not a valid key-value pair separator. Please use ',' instead.", source: https://test.plexonline.com/Modules/SystemAdministration/Login/Index.aspx (6)
[0204/072437.722:INFO:CONSOLE(1)] "Scripts may close only the windows that were opened by it.", source:  (1)
[0204/072441.162:INFO:CONSOLE(751)] "Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg.org/.", source: https://test.plexonline.com/Modules/scripts/ajax.js (751)

我正在使用Chrome Version 79.0.3945.130 (Official Build) (64-bit),Selenium 3.141.0Python 3.7.4.

任何想法我做错了什么?谢谢!

标签: selenium-webdriver

解决方案


推荐阅读