1

I want to get the text of li (second) element as below. Code is able to find the element but it is throwing the error while fetching the gettext. Please help.

WebElement element = driver.findElement(By.xpath("//div[contains(@id,'location_group')]/div/ul/li[2]"));

element.getText();

Error

Stale element reference: element is not attached to the page document

HTML

<div id=location_group>
  <div>
    <ul class="og-tooltip js-og-tooltip" style="display: none; opacity: 100;">
      <li class="title_text">Organization Group</li>
      <li class="value_text" style="background: rgb(204, 136, 136); border: 2px solid red;">Global / Aricent / gemsecure </li>
      <li>&nbsp;</li>
      <li class="title_text">Group ID</li>
      <li class="value_text">gemsecure</li>
    </ul>
  </div>
</div>
JeffC
  • 22,180
  • 5
  • 32
  • 55
sudha vedi
  • 97
  • 1
  • 11
  • Have you checked [Stale Element Reference Exception](http://docs.seleniumhq.org/exceptions/stale_element_reference.jsp)? Also, what language and browser are you using? – dey.shin Sep 07 '17 at 12:38
  • Possible duplicate of [StaleElementReference Exception in PageFactory](https://stackoverflow.com/questions/44838538/staleelementreference-exception-in-pagefactory) – undetected Selenium Sep 07 '17 at 13:54

4 Answers4

0

Because this element is wrapped with a ul that has display: none selenium can not interact with it. Options to try:

element.getAttribute("innerHTML");

or you can use the JavascriptExecutor:

 JavascriptExecutor executor = (JavascriptExecutor)driver;
 String text= executor.executeScript("document.document.getElementsByClassName('value_text')[0].innerHTML");

Another option is to use querySelector/querySelectorAll which has broader support than getElementsByClassName

iamsankalp89
  • 4,607
  • 2
  • 15
  • 36
Moe Ghafari
  • 2,227
  • 1
  • 12
  • 17
  • If the element wasn't visible, it wouldn't have thrown a stale element exception. This doesn't solve the problem. Also, there is more than one element with that class name (and probably others on the page). – JeffC Sep 07 '17 at 13:42
0

May be you have to wait for the parent element to be visible then interact with it

WebElement elementUL = driver.findElement(By.xpath("//div[contains(@id,'location_group')]/div/ul"));
WebDriverWait wait=new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.visibilityOf(elementUL));

WebElement element = driver.findElement(By.xpath("//div[contains(@id,'location_group')]/div/ul/li[2]"));

element.getText();
Murthi
  • 5,299
  • 1
  • 10
  • 15
  • If the element wasn't visible, it wouldn't have thrown a stale element exception. This doesn't solve the problem. – JeffC Sep 07 '17 at 13:42
  • The elements are loading and the parent element is hidden. That's why I am waiting. I am not saying, it will throw stale error if element is not visible. – Murthi Sep 07 '17 at 14:35
  • According to OP it is currently throwing a stale element exception. Your answer to the question is to wait for the element to be visible. So how does waiting for the element to be visible solve the stale element exception? – JeffC Sep 07 '17 at 15:47
  • Can you tell me, when stale error occurs? Then I will answer your question. – Murthi Sep 07 '17 at 15:56
  • A stale element exception occurs when you get a reference to an element on the page and then the element that you have the reference to changes or is removed. A simple example of this is to get a reference to an element on page 1 of a site and then navigate to page 2. Now the reference to the element on page 1 is stale and will throw the exception if you try to access it. For more details: http://www.seleniumhq.org/exceptions/stale_element_reference.jsp – JeffC Sep 07 '17 at 16:03
  • It means the stale error occurs when we refer the element second time after Dom changes or page changes. But in question it is getting stale when he access the element at first time. Let the OP try the solution. Otherwise. You can given him the solution. I am saying, it may helps and not saying, it will be best solution. – Murthi Sep 07 '17 at 17:14
0

Place a explicit waitWebDriverWait and wait till element to be visible

WebElement ele = driver.findElement(By.xpath("Your locator "));
WebDriverWait wait=new WebDriverWait(driver, 25);
wait.until(ExpectedConditions.visibilityOf(ele));

and then getText()

iamsankalp89
  • 4,607
  • 2
  • 15
  • 36
0

What I recommend is one use the xPath. I think I've tried every solution in java's selenium library, thus my website had tricky selects - once user picks an option, the options are reloaded (ex. selected value sometimes disappears, sometimes different options where loaded depends on particular select). Lot of stale element exceptions. What I've used:


    private WebElement getDesiredOptionByVisibleText(WebDriver driver, String selectId, String text) {
        List<WebElement> options = driver.findElements(By.xpath("//select[@id='" + selectId 
+ "']/option[contains(text(),'" + text + "')]"));
        if (options == null || options.size() == 0) {
            return null;
        } else {
            return options.get(0);
        }
    }

especially use this method with waits, for example my wait:


    private void waitUntilDesiredOptionVisible(WebDriver driver, String selectId) {
        getFluentWait(driver).until(d -> (getDesiredOptionByVisibleText(driver, selectId, value) != null));
    }

    public static FluentWait<WebDriver> getFluentWait(WebDriver driver) {
        return new FluentWait<>(driver)
            .withTimeout(Duration.ofSeconds(60))
            .pollingEvery(Duration.ofMillis(100));
    }

Last but not least clicking that option.

    private void selectByVisibleTextRegex(WebDriver driver, String selectId) {
        WebElement option = getDesiredOptionByVisibleText(driver, selectId, value);
        if (option == null) {
            throw new NoSuchElementException(String.format("Cannot locate element with text like: %s.", value));
        }
        option.click();
    }

Rafał
  • 582
  • 1
  • 9
  • 14