- Java中的代码**
public class SeleniumService {
private int dirationInSeconds = 120;
private WebElement findChildByXpath(WebDriver driver, WebElement parent, String xpath) throws WebElementNotFoundException {
loggingService.timeMark("findElementByXpath", "begin. Xpath: " + xpath);
WebElement result = null;
try {
result = new WebDriverWait(driver, Duration.ofSeconds(dirationInSeconds))
.until(ExpectedConditions.elementToBeClickable(By.xpath(xpath)));
} catch (TimeoutException e) {
throw new WebElementNotFoundException();
} catch (Exception e) {
// Mustn't be here.
System.out.println(e.getMessage());
System.out.println("QUIT!");
System.exit(0);
}
return result;
}
}
上面的代码不起作用,因为它是关于"WebDriverWait(driver)"的。它与"driver"得到的页面相关,而不是父元素。
此代码适用于:
WebElement frequencyElement = parent.findElement(By.xpath("//span[@class='dc-Text__text dc-verticalAlign dc-verticalAlign_text_12-16 dc-verticalAlign_addon_12 dc-typography dc-typography_size_12-16 dc-typography_role_main dc-typography_color_gray dc-typography_bold dc-Text dc-Text_textAlign_left']"));
但是这段代码没有配备WebDriverWait。
你能帮我把这两个代码组合起来,这样子元素就应该被找到,WebDriverWait就被应用了。
1条答案
按热度按时间3pvhb19x1#
您可以为定位器创建一个变量,然后声明等待并按照示例中的方式使用它。
`
`