[英]Selenium click in headless mode
I've been trying to wrap my brain around this problem.我一直在努力解决这个问题。 I have a webscraping project with Selenium and my script works when headless mode is disabled.
我有一个带有 Selenium 的网络抓取项目,我的脚本在无头模式被禁用时工作。 Once I add the argument ("--headless") to my chrome driver options (chrome driver version 88.0.4324) it isn't able to click the element.
一旦我将参数(“--headless”)添加到我的 chrome 驱动程序选项(chrome 驱动程序版本 88.0.4324)中,它就无法单击该元素。 I have seen other people mentioning that maximizing the window or setting the window size of the headless driver will solve this, I have had no luck with such solutions.
我看到其他人提到最大化 window 或设置无头驱动程序的 window 大小将解决这个问题,我对这样的解决方案没有运气。
This is the line that clicks the element:这是单击元素的行:
browser.find_element_by_xpath('//a [@href][@class="button icon arrowdown"]').click()
This is what I was using before the above xpath:这是我在上述 xpath 之前使用的:
browser.find_element_by_class('button icon arrowdown').click()
Thanks for Everyones Help!感谢大家的帮助!
I had same issue tried using javascript click我有同样的问题尝试使用 javascript 点击
driver.execute_script("document.getElementsByClassName('button icon arrowdown')[0].click()")
Take a screenshot and snapshot of the headless window when exception occurs.发生异常时对无头 window 进行屏幕截图和快照。 That will help you to find out why this happens.
这将帮助您找出发生这种情况的原因。 You can try to use selene library https://github.com/yashaka/selene which already have these features, or write your own snap-shooter.
您可以尝试使用已经具备这些功能的 selene 库https://github.com/yashaka/selene ,或者编写自己的 snap-shooter。
Depending on this quote取决于此报价
I have seen other people mentioning that maximizing the window or setting the window size of the headless driver will solve this, I have had no luck with such solutions.
我看到其他人提到最大化 window 或设置无头驱动程序的 window 大小将解决这个问题,我对这样的解决方案没有运气。
you need to know, that maximizing in headless - doesn't work because it headless and doesn't know which screen size is to maximize.您需要知道,在无头模式中最大化 - 不起作用,因为它是无头模式并且不知道要最大化哪个屏幕尺寸。 You should take the dimensions of the browser in non-headless mode and set it up directly in selenium both for head and headless mode.
您应该在非无头模式下获取浏览器的尺寸并直接在 selenium 中针对有头模式和无头模式进行设置。 This can solve your issue.
这可以解决您的问题。 And even if not - try to take screenshot or make a video to figure out what's going on in headless mode.
即使没有 - 尝试截取屏幕截图或制作视频以了解无头模式下发生了什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.