WebElement getText() is an empty string in Firefox if element is not physically visible on the screen

后端 未结 2 1356
醉梦人生
醉梦人生 2020-12-04 01:11

My Selenium Webdriver tests broke recently after upgrading Firefox to version 19. In several of my tests I need to retrieve elements that are on the page but not visible on

相关标签:
2条回答
  • 2020-12-04 01:37

    I couldn't find an answer as to why WebDriver and/or Firefox behave the way they do. In my AUT I have a grid that displays a report and any cells/WebElements that are not visible on the screen I'm able to see in the Html, they do not appear to be hidden and WebDriver will admit it can see them however I can not retrieve any of the element's values, i.e. getText, getAttribute, etc. So to work around this limitation (not sure if it's a bug or just the way it behaves) I used the following little bit of JavaScript which seems to have resolved the issue for me:

    private void scrollToElement(WebElement element){
    
    ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
    }
    

    This was definitely a change in Firefox behavior from v16+. Hopefully someone else can also find this helpful.

    0 讨论(0)
  • 2020-12-04 01:49

    Try the same thing by disabling native events like below

    FirefoxProfile profile=new FirefoxProfile();
    profile.setEnableNativeEvents(false);
    DesiredCapabilities dc = DesiredCapabilities.firefox(); 
    dc.setCapability(FirefoxDriver.PROFILE, profile); 
    driver = new FirefoxDriver(dc);
    

    This is some work around to do page down

    driver.findElement(By.tagName("body")).sendKeys(Keys.PAGE_DOWN);
    

    Have you upgraded to selenium-server-standalone-2.31.0 ?

    0 讨论(0)
提交回复
热议问题