gpt4 book ai didi

java - 如何在 Selenium 中找到这个元素(页面上的按钮)?

转载 作者:行者123 更新时间:2023-11-30 08:03:27 25 4
gpt4 key购买 nike

我正在尝试按网页上的“网格”类按钮,但遇到了麻烦。这是 HTML:

<li id="prodlist" class="prodtab">
<span> Products</span>
<div class="grid" onclick="goToView('productGrid');"></div>
<div class="list" onclick="goToView('productList')"></div>
</li>

这是我尝试过的,但它给出了org.openqa.selenium.NoSuchElementException:

driver.findElement(By.xpath("div[contains(@class, 'grid')]")).click();

最佳答案

此类问题的解决方案通常是切换到 iframe(如果元素位于其中):

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

或者,进行显式等待来等待元素出现:

WebDriverWait wait = new WebDriverWait(webDriver, 5);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[contains(@class, 'grid')]")));

关于java - 如何在 Selenium 中找到这个元素(页面上的按钮)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31529963/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com