selenium WebDriverWait和查找子项

6tqwzwtp  于 2023-01-30  发布在  其他
关注(0)|答案(1)|浏览(167)
    • 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就被应用了。

3pvhb19x

3pvhb19x1#

您可以为定位器创建一个变量,然后声明等待并按照示例中的方式使用它。
`

By exampleElement = 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 wait = new WebDriverWait(driver, Duration.ofSeconds(15));     
wait.until(ExpectedConditions.elementToBeClickable(exampleElement)).click();

`

相关问题