Selenium 与 Python:send_keys() 在无头 ubuntu 上不起作用 [英] Selenium with Python: send_keys() doesn't work on headless ubuntu

查看:30
本文介绍了Selenium 与 Python:send_keys() 在无头 ubuntu 上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 python Selenium,在 digitalocean 上使用无头 ubuntu,上面有无头 Chrome.我用过

I am using python Selenium, with headless ubuntu at digitalocean, which has headless Chrome on it. I used

driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.TAB)

在senium.webdriver.common.keys

at senium.webdriver.common.keys

但它不起作用.

我导入了所有需要的东西,没有出现 python 语法错误,并且运行成功,但是标签没有用我的代码切换.

I imported everything needed, with no python syntax error, and ran successfully, but tabs are not switched with my code.

driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 't')

也不行.相同的代码可以在具有物理键盘和显示器的本地计算机上的选项卡之间切换.顺便说一句,我在无头 Chrome 上使用了 pyvirtualdisplay.

also doesn't work. The same code can switch between tabs on my local computer, which has physical keyboard and monitor. Btw, I used pyvirtualdisplay with my headless Chrome.

我怀疑在其上使用无头 Ubuntu 和无头 Chrome 可能会导致此问题.我猜无头 Ubuntu 可以t 按照上面的代码发送密钥.

I suspect that using headless Ubuntu and headless Chrome on it may cause this problem. I guess headless Ubuntu can' t send keys, as the code above directed.

如何让我的远程无头 Ubuntu 向浏览器发送密钥?

How can I make my remote, headless Ubuntu send keys to the browser?

推荐答案

这是 chromedriver 的知名问题.Chromium 开发者团队的评论

This is well-known issue of chromedriver. Comment from Chromium developer's team

这是我们在 ChromeDriver 中模拟键盘输入方式的限制.密钥绕过浏览器进程直接发送到渲染进程.所以浏览器进程中的任何键盘快捷键处理程序都不会被 sendKeys() 调用.

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

This will allow you to open URL in new tab

附:如果它解决了您的问题,请将此答案标记为已接受",或者在出现问题时告诉我

P.S. Please mark this answer as "Accepted" if it solved your problem or let me know in case of issues

这篇关于Selenium 与 Python:send_keys() 在无头 ubuntu 上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆