4

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

堆栈跟踪:发生

错误:消息:元素不可见
(会话信息:无头铬= 60.0.3112.90)
(驾驶员信息:chromedriver = 2.31.488763(092de99f48a300323ecf8c2a4e2e7cab51de5ba8),平台= Windows NT的6.1.7601 SP1 x86_64的)
回溯(最近通话最后一个):
文件 “C:\ nik-x.py”,线路148,在主
FUNC(聂)
文件“C:\ LIB \支持。 PY”,线121,在包装
加注保留
文件 “C:\ lib中\ support.py”,线路108,在newFunc
RES [0] = FUNC(*指定参数时,** kwargs)
文件“C:\ testcases \ nik-1003.py”,第37行,在testcase中
i.click()
文件“C:\ Python36 \ lib \ site-packages \ selenium \ webdriver \ remote \ webelement.py” 7行
7,在点击
self._execute(Command.CLICK_ELEMENT)
文件 “C:\ Python36 \ lib中\站点包\硒\的webdriver \远程\ webelement.py”,第4行
93,在_execute
返回self._parent.execute(命令,则params)
文件 “C:\ Python36 \ lib中\站点包\硒\的webdriver \远程\ webdriver.py”,第25行
6,在执行
self.error_handler.check_response(响应)
文件 “C:\ Python36 \ lib中\站点包\硒\的webdriver \远程\ errorhandler.py”,线
194,在check_response
加注exception_class(消息,屏幕,堆栈跟踪)
selenium.common.exceptions.ElementNotVisibleException:消息:元素不可见
(会话信息:无头铬= 60.0.3112.90)
(驾驶员信息:chromedriver = 2.31.488763(092de99f48a300323ecf8c2a4e2e7cab51de5ba8),平台= Windows NT的6.1.7601 SP1 x86_64的)

这里是我的代码:从测试页
icons = nik.elementLeftmenuSportIcons() for i in icons[:-1]: try: i.click()

HTML:<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>

+0

错误堆栈跟踪了一切'ElementNotVisibleException:消息:元素不可见“要么等待元素可见,要么必须将该元素置于视口内才能点击。 – DebanjanB

+0

这也可能是JS中的一些实际差异。因为JS可能会改变属性。取代单击元素,获取元素并打印其所有属性,特别是坐标和所有属性。然后看看有什么区别 –

+0

@ DebanjanB谢谢,我试图添加等待(10),switch_to_frame,窗口等,但我仍然有同样的问题。 – Ghost

回答

1

我认为问题是,该元素是真的没有无头的Chrome默认视框(600×800)可见。

启动chrome时,无头浏览器的窗口大小必须设置为参数。我使用JavaScript(我觉得API看起来下蟒类似):

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);这个命令是不够的在无头铬。在非无头变体中,这是行得通的。

0

options.addArguments('window-size = 1200,1100');

在无头的反转片模式为我工作:)感谢很多@powerpete

下面是无头的镀铬完成设置在Groovy: -

 ChromeOptions options = new ChromeOptions() 
     DesiredCapabilities capabilities = DesiredCapabilities.chrome() 
     options.addArguments('headless','disable-gpu','--test-type','--ignore-certificate-errors') 
     options.addArguments('window-size=1200,1100'); 
     capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true); 
     capabilities.setCapability(ChromeOptions.CAPABILITY, options) 
     driver = {new ChromeDriver(capabilities)}