我无法在以下代码的文章中检测到按钮:
<article id="ride-f6ba24ca-d847-44b7-987e-81db6e6dee47" class="DetailPage__container--1VLdd"><div class="DetailPage__highlights--1uyrQ"><section></section><form aria-label="Offer highlights" class="DetailPage__section--qtXxV"><button type="submit"><span>Accept offer</span></button></form></div></article>
我尝试:
driver.findElement(By.xpath("//*[text()='Details']"))
driver.findElement(By.xpath("//button[.//span[text()='Accept offer']]"))
没有运气
我无法在Java中检测到元素接受硒提供的条件
答案 0 :(得分:1)
所需元素是动态元素,因此必须找到要为 elementToBeClickable()引入 WebDriverWait 的元素,并且可以使用以下{{3 }}:
cssSelector
:
WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("article[class^='DetailPage__container--'][id^='ride-']>div[class^='DetailPage__highlights--'] button[type='submit']>span")));
xpath
:
WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//article[starts-with(@class, 'DetailPage__container--') and starts-with(@id, 'ride-')]/div[starts-with(@class, 'DetailPage__highlights--')]//button[@type='submit']/span[text()='Accept offer']")));
答案 1 :(得分:0)
最后,您的方法可以使用以下方法
private void waitForElement(By by, long delay) {
LocalDateTime end = LocalDateTime.now().plusSeconds(delay);
while (LocalDateTime.now().compareTo(end) <= 0) {
if (driver.findElements(by).size() > 0) {
break;
}
}
}
我先运行方法,然后再检查是否有下面这样的元素的列表
if (driver.findElements(By.xpath("//article[starts-with(@class, 'DetailPage__container--') and starts-with(@id, 'ride-')]/div[starts-with(@class, 'DetailPage__highlights--')]//button[@type='submit']/span[text()='Accept offer']")).size() > 0){//Here i call the element}
所以如果元素存在,我可以在里面调用它,否则就没有时间例外了