How to find button element on the page?

How do I find this element in Selenium (button on page to press)?

  • I am trying to press the "grid" class button that is on a web page but I am having trouble. Here is the HTML: <li id="prodlist" class="prodtab"> <span> Products</span> <div class="grid" onclick="goToView('productGrid');"></div> <div class="list" onclick="goToView('productList')"></div> </li> Here is what I tried but it gives org.openqa.selenium.NoSuchElementException: driver.findElement(By.xpath("div[contains(@class, 'grid')]")).click();

  • Answer:

    The solution to this kind of problems is usually either switch to an iframe, if the element is inside it: WebElement frame = driver.findElement(by.cssSelector("iframe.ajaxStoreNumberAppendSrc")); driver.switchTo().frame(frame); // then, search for element driver.findElement(By.xpath("//div[contains(@class, 'grid')]")).click(); Or, make an explicit wait to wait for element to become present: WebDriverWait wait = new WebDriverWait(webDriver, 5); wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[contains(@class, 'grid')]")));

Claude Bastien at Stack Overflow Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.