简体   繁体   中英

Selenium- How to check if element is present or not

Consider the following scenario:-

A bookstore website has number of books and each book has some details like author name, publisher, etc. The main page displays number of books and on clicking any book on the page a pop-up opens which displays details of that particular book. The main html page looks something like below.

link Book 1

<book id="abc">
    <a href="linkAddress">link</a>
    <div class="name">Book 2</div>
</book>

<book id="abc">
    <a href="linkAddress">link</a>
    <div class="name">Book 3</div>
</book>

<book id="abc">
    <a href="linkAddress">link</a>
    <div class="name">Book 4</div>
</book>

<book id="abc">
    <a href="linkAddress">link</a>
    <div class="name">Book 5</div>
</book>

I want to fetch details of all books so what I did is I fetched a list of all the book elements from the main page

List<WebElement> items = fireFoxDriver.findElements(By.id("abc"));

and then iterated through that list and clicked each element one by one to open a pop-up that shows details of the book

for(WebElement itemDetails : items){
    itemDetails.click();
    //After clicking the element pop-up opens which shows details of this particular book.
    //Get details of this particular book from pop-up.
}

and fetched the details I required from that opened pop-up and then closed the popup after getting all required details and moved to next element.

Now the problem is that I am using itemDetails.click() to open pop-up but sometimes I get the following errors when opening a pop-up window. I am not sure how to handle these errors so please guide me. Elements are there on the main page I am not changing the main page while opening pop-up but still getting DOM errors.

  1. org.openqa.selenium.StaleElementReferenceException: Element is no longer attached to the DOM
  2. com.thoughtworks.selenium.SeleniumException: Timed out waiting for action to finish

Thanks.

You may be running into a race condition.

If the page changes between

List<WebElement> items = fireFoxDriver.findElements(By.id("abc"));

and the loop

for(WebElement itemDetails : items){
    itemDetails.click();
}

then it is possible that the itemDetails no longer belong to the DOM and you will get a StateElementReferenceException.

A similar problem is described here . You could try explicitly waiting until the page is in a state where it will not change (perhaps using WebDriverWait ).

You could try finding each element by itself right before you want to interact with it rather than iterating through the list so that they won't ever get stale. You could accomplish this using xpath like this:

int elementCount = fireFoxDriver.findElements(By.id("abc")).Count;

for (int i = 0; i < elementCount; i++) {

firefoxDriver.findElement(By.XPath(String.Format("//book[id=\\"abc\\"][{0}]", i.ToString()))).click();

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM