ElementNotVisibleException仅在虚拟显示中 [英] ElementNotVisibleException only in virtual display
问题描述
我有一个脚本,该脚本需要通过硒与网页进行交互.我需要使用某种虚拟显示器来阻止浏览器显示.
I have a script that needs to interact with a webpage via selenium. I need to use some kind of virtual display to keep the browser from showing up.
在我将Xvfb引入混合脚本之前,该脚本总体上可以很好地发挥作用.当我这样做时,我第一次真正尝试与页面进行交互.
The script as a whole works great until I introduce Xvfb into the mix. When I do that I get an ElementNotVisibleException
the first time I actually try to interact with the page.
我尝试使用xvfbwrapper
和pyvirtualdisplay
具有相同的效果.
I've tried using xvfbwrapper
and pyvirtualdisplay
with the same effect.
这是无效的代码:
from xvfbwrapper import Xvfb
vdisplay = Xvfb()
vdisplay.start()
oBrowser = Browser()
oBrowser.visit(sUrl)
oBrowser.find_by_id('some_field')[0].fill(sValue) #<--ERROR
vdisplay.stop()
这是起作用的代码(但显示浏览器):
And here is the code that does work (but displays the browser):
oBrowser = Browser()
oBrowser.visit(sUrl)
oBrowser.find_by_id('some_field')[0].fill(sValue) #<--works every time
那我如何在虚拟显示器上运行我的代码?
So how can I run my code on a virtual display?
在尝试填写字段之前,我尝试过执行time.sleep
的问题,但是问题似乎与缓慢加载页面无关.有什么想法吗?
I've tried doing a time.sleep
before trying to fill the field in but the problem doesn't seem to have anything to do with the page loading slowly. Any ideas?
推荐答案
这是一种替代方法,而不是直接解决方案:
This is a workaround more than a direct solution:
我替换了这一行:
oBrowser.find_by_id('some_field')[0].fill(sValue)
与此:
oBrowser.execute_script("document.getElementById('some_field').value = {}".format(sValue))
它可靠地工作.我仍然不确定为什么它不能一开始就发挥作用.
And it works reliably. I'm still not sure why it didn't just work in the first place though.
这篇关于ElementNotVisibleException仅在虚拟显示中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!