Claude Bastien
Claude Bastien

Reputation: 141

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();

Upvotes: 1

Views: 742

Answers (1)

alecxe
alecxe

Reputation: 473833

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')]")));

Upvotes: 1

Related Questions