Python 的 Selenium:send_keys() 不适用于无头 ubuntu
Selenium with Python: send_keys() doesn't work on headless ubuntu
我正在使用 python Selenium,在 digitalocean 上使用无头 ubuntu,它上面有无头 Chrome。
我用了
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.TAB)
于
senium.webdriver.common.keys
但是不行。
我导入了所有需要的东西,没有 python 语法错误,运行 成功,但标签没有用我的代码切换。
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 't')
也不行。相同的代码可以在我的本地计算机上的选项卡之间切换,该计算机具有物理键盘和显示器。顺便说一句,我在无头 Chrome.
中使用了 pyvirtualdisplay
我怀疑在上面使用headless Ubuntu 和headless Chrome 可能会导致这个问题。我猜 headless Ubuntu 可以'
t 按照上面的代码指示发送密钥。
如何让我的远程无头 Ubuntu 向浏览器发送密钥?
这是 chromedriver
的 well-known 期。来自 Chromium
开发者团队的评论
This is a limitation in the way we simulate keyboard input in ChromeDriver. Keys get sent directly to the render process, bypassing the browser process. So any keyboard shortcut handlers in the browser process will not be invoked by sendKeys().
您可以改用以下代码:
driver.execute_script("window.open('url_of_page_to_get', 'new_window')")
这将允许您在新标签页中打开URL
P.S。如果它解决了您的问题,请将此答案标记为 "Accepted",或者如果有问题请告诉我
我正在使用 python Selenium,在 digitalocean 上使用无头 ubuntu,它上面有无头 Chrome。 我用了
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.TAB)
于 senium.webdriver.common.keys
但是不行。
我导入了所有需要的东西,没有 python 语法错误,运行 成功,但标签没有用我的代码切换。
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 't')
也不行。相同的代码可以在我的本地计算机上的选项卡之间切换,该计算机具有物理键盘和显示器。顺便说一句,我在无头 Chrome.
中使用了 pyvirtualdisplay我怀疑在上面使用headless Ubuntu 和headless Chrome 可能会导致这个问题。我猜 headless Ubuntu 可以' t 按照上面的代码指示发送密钥。
如何让我的远程无头 Ubuntu 向浏览器发送密钥?
这是 chromedriver
的 well-known 期。来自 Chromium
开发者团队的评论
This is a limitation in the way we simulate keyboard input in ChromeDriver. Keys get sent directly to the render process, bypassing the browser process. So any keyboard shortcut handlers in the browser process will not be invoked by sendKeys().
您可以改用以下代码:
driver.execute_script("window.open('url_of_page_to_get', 'new_window')")
这将允许您在新标签页中打开URL
P.S。如果它解决了您的问题,请将此答案标记为 "Accepted",或者如果有问题请告诉我