使用无头Chrome浏览器时ElementNotVisibleException

Gho*_*ost 10 selenium selenium-chromedriver python-3.6 google-chrome-headless

当我在无头模式chrome浏览器中运行测试脚本时,元素链接不可见,无法做到linkElement.click().在头模式下一切都好.所有其他信息都在stacktrace中.谁知道该怎么办,拜托?

堆栈跟踪:

发生错误:消息:元素不可见
(会话信息:headless chrome = 60.0.3112.90)
(驱动程序信息:chromedriver = 2.31.488763(092de99f48a300323ecf8c2a4e2e7cab51de5ba8),platform = Windows NT 6.1.7601 SP1 x86_64)
Traceback(最近一次调用最后一次):
文件"C:\nik-x.py",第148行,在主
func(nik)
文件"C:\ lib\support.py"中,第121行,在包装器中
提升
文件"C:\ lib\support.py" ",第108行,在newFunc
res [0] = func(*args,**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\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",li ne
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() 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>

pow*_*ete 10

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

启动chrome时,必须将Headless Browser的窗口大小设置为Argument.我正在使用javascript(我认为API在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();
Run Code Online (Sandbox Code Playgroud)

附加信息

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