使用无头 Chrome 浏览器时的 ElementNotVisibleException [英] ElementNotVisibleException when use headless Chrome browser

查看:134
本文介绍了使用无头 Chrome 浏览器时的 ElementNotVisibleException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在无头模式 chrome 浏览器中运行测试脚本时,元素链接不可见,无法执行 linkElement.click().在头部模式下一切正常.所有其他信息都在堆栈跟踪中.有谁知道该怎么做吗?

When I run test script in headless mode chrome browser, element link is not visible, is not able to do linkElement.click(). in head mode is everything OK. All other info are in stacktrace. Anyone knows what to do, please?

堆栈跟踪:

发生错误:消息:元素不可见
(会话信息:headless chrome=60.0.3112.90)
(驱动信息: chromedriver=2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8),platform=Windows NT 6.1.7601 SP1 x86_64)
回溯(最近一次调用最后一次):
文件C:\nik-x.py",第 148 行,在 main
功能(尼克)
文件C:\lib\support.py",第 121 行,包装器
提高 ret
文件C:\lib\support.py",第 108 行,在 newFunc
res[0] = func(*args, **kwargs)
文件C:\testcases\nik-1003.py",第 37 行,在测试用例中
i.click()
文件C:\Python36\lib\site-packages\selenium\webdriver\remote\webelement.py",第 7 行
7、在点击
self._execute(Command.CLICK_ELEMENT)
文件C:\Python36\lib\site-packages\selenium\webdriver\remote\webelement.py",第 4 行
93、在_execute
返回 self._parent.execute(command, params)
文件C:\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py",第 25 行
6、在执行
self.error_handler.check_response(响应)
文件C:\Python36\lib\site-packages\selenium\webdriver\remote\errorhandler.py",行
194,在check_response
引发异常类(消息,屏幕,堆栈跟踪)
selenium.common.exceptions.ElementNotVisibleException:消息:元素不可见
(会话信息:headless chrome=60.0.3112.90)
(驱动程序信息:chromedriver=2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8),平台=Windows NT 6.1.7601 SP1 x86_64)

ERROR occurred: Message: element not visible
(Session info: headless chrome=60.0.3112.90)
(Driver info: chromedriver=2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8),platform=Windows NT 6.1.7601 SP1 x86_64)
Traceback (most recent call last):
File "C:\nik-x.py", line 148, in main
func(nik)
File "C:\lib\support.py", line 121, in wrapper
raise ret
File "C:\lib\support.py", line 108, in newFunc
res[0] = func(*args, **kwargs)
File "C:\testcases\nik-1003.py", line 37, in testcase
i.click()
File "C:\Python36\lib\site-packages\selenium\webdriver\remote\webelement.py", line 7
7, in click
self._execute(Command.CLICK_ELEMENT)
File "C:\Python36\lib\site-packages\selenium\webdriver\remote\webelement.py", line 4
93, in _execute
return self._parent.execute(command, params)
File "C:\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 25
6, in execute
self.error_handler.check_response(response)
File "C:\Python36\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line
194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: element not visible
(Session info: headless chrome=60.0.3112.90)
(Driver info: chromedriver=2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8),platform=Windows NT 6.1.7601 SP1 x86_64)

这是我的一段代码:
icons = nik.elementLeftmenuSportIcons()对于图标中的 i[:-1]:尝试:i.click()

来自测试页面的 HTML:<span class="left-menu-only-large-res">Futbal</span>

HTML from testing page: <a href="#" class="default b_futbal gaPageEventElement" data-ga-cat="Sporty" data-ga-action="futbal"> <span class="left-menu-only-large-res">Futbal</span> </a>

推荐答案

我认为问题是,Element 在 Headless Chrome 的默认视图框 (600x800) 中确实不可见.

I think the problem is, that the Element is really not visible in the default viewbox (600x800) of Headless Chrome.

Headless Browser 的窗口大小必须在启动 chrome 时设置为 Argument.我正在使用 javascript(我认为 API 在 python 下看起来很相似):

The window size of the Headless Browser must be set as a Argument when starting chrome. I'm using javascript (I think the API looks similar under python):

var Options = require('selenium-webdriver/chrome').Options;
var options = new Options();
options.addArguments('headless');
options.addArguments('disable-gpu');
options.addArguments('window-size=1200,1100');

browser = builder.forBrowser('chrome').setChromeOptions(options).build();

附加信息

我也通过 webdriver 使用 browser.manage().window().setSize(1200,1100); 设置了窗口大小,但是这个命令在无头 chrome 中是不够的.在非无头变体中,这是有效的.

I'm setup up the window size also by webdriver with browser.manage().window().setSize(1200,1100); But this command is not sufficient in headless chrome. In the non headless variant this is working.

这篇关于使用无头 Chrome 浏览器时的 ElementNotVisibleException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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