Chrome无头元素不可见

时间:2018-04-06 22:03:13

标签: selenium selenium-webdriver webdriver selenium-chromedriver

我在尝试与iframe中的一个编辑框进行互动时遇到以下错误。

请注意代码工作正常,如果我没有在无头模式下运行它。仅在无头模式下运行时才会出现问题。

错误:

  

org.openqa.selenium.ElementNotVisibleException:元素不可见

堆栈跟踪:

org.openqa.selenium.ElementNotVisibleException: element not visible
  (Session info: headless chrome=60.0.3112.113)
  (Driver info: chromedriver=2.33.506092 (733a02544d189eeb751fe0d7ddca79a0ee28cce4),platform=Linux 4.9.51-10.52.amzn1.x86_64 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 21 milliseconds
Build info: version: '2.53.0', revision: '35ae25b1534ae328c771e0856c93e187490ca824', time: '2016-03-15 10:43:46'
System info: host: 'ip-172-31-41-152', ip: '172.31.41.152', os.name: 'Linux', os.arch: 'amd64', os.version: '4.9.51-10.52.amzn1.x86_64', java.version: '1.8.0_45'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.33.506092 (733a02544d189eeb751fe0d7ddca79a0ee28cce4), userDataDir=/tmp/.org.chromium.Chromium.aRCh3q}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=60.0.3112.113, platform=LINUX, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true, setWindowRect=true, unexpectedAlertBehaviour=}]
Session ID: ebc4a4ce4a038d2440aedb1f4d952a64
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678)
    at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:327)
    at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:85)
    at com.medexpress.pages.Buyer.CheckoutPage.submitPage(CheckoutPage.java:148)
    at com.medexpress.steps.Order.Submits_the_order(Order.java:120)
    at ✽.And Submits the order(com/medexpress/steps/2oder_checkout.feature:44)

要解决此问题,我尝试滚动到Iframe或将使此iframe成为焦点的元素。我试过以下事情。然而,没有任何工作。它不会滚动到给定的元素。

使用原生事件滚动

actions.moveToElement(element).build().perform();

使用JavaScript滚动

Actions actions = new Actions(driver);
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].scrollIntoView()", element);

使用javascript

滚动到页面末尾
js.executeScript("window.scrollTo(0,document.body.scr‌​ollHeight);");

上面的代码不起作用,并给出以下错误。

  

org.openqa.selenium.WebDriverException:未知错误:Runtime.evaluate引发异常:SyntaxError:无效或意外的令牌

请注意,以上所有代码都在使用chrome,只有无头问题。

广告代码

ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("headless");
chromeOptions.addArguments("disable-gpu");
chromeOptions.addArguments("window-size=1200,1100");
driver = new ChromeDriver(chromeOptions);

3 个答案:

答案 0 :(得分:4)

如果元素可见(高度和宽度大于0),则无需滚动即可点击该元素。

我遇到了同样的问题,并通过将窗口设为FullHD来修复它,所以只需更改此行:

select user_id, avg(interaction_time)
from (
   select user_id, 
          interaction_date - lag(interaction_date) over (partition by user_id order by interaction_date) as interaction_time
   from the_table
) t
group by user_id;

chromeOptions.addArguments("window-size=1200,1100");

我不知道Java的语法,因为我在C#中修复它,但是值得一试。

答案 1 :(得分:0)

面临类似的问题。以下代码行为我工作 -

options.addArguments("--proxy-server='direct://'");
options.addArguments("--proxy-bypass-list=*");

来源 - https://github.com/Codeception/CodeceptJS/issues/561

答案 2 :(得分:0)

增加“窗口大小”可修复Gulp量角器中的错误

{
    browserName: 'chrome',
    chromeOptions: {
        args: [ "--headless", "--disable-gpu", "--window-size=1200,900" ]
    }
}