Selenium:在Java中搜索By定位器的子级



我在页面上有一个WebElements的By定位器的枚举列表。我希望能够使用枚举以及我希望选择的选项的值的附加信息的组合来选择选择框的特定选项。有办法做到这一点吗?我注意到By类也有方法findElement(searchContext)。我可以用这个作为类似的东西吗

public enum Dictionary {
    TYPE                        (By.id("vehType")),
    PROVINCE                    (By.id("provId")),
    TERRITORY                   (By.id("territoryId")),
    STAT_CODE                   (By.id("statCodeId")),
    CLASS                       (By.id("class1Id"));
    private final By locator;
    private DetailVehicleDictionary (By value) {
        this.locator = value;
    }
    public By getLocation() {
        return this.locator;
    }
}

然后,如果CLASS是一个HTML为:的选择框

<select id="class1Id" name="select_box">
    <option value="1"/>
    <option value="2"/>
    <option value="3"/>
</select>

我可以做一些类似的事情吗

WebElement specificValue = driver.findElement(Dictionary.CLASS.getLocation().findElement(By.cssSelector("option[value=2]"));

我需要访问实际的元素,这样我就可以等待值出现在DOM中。我计划在等待命令中实现这一点,例如:

wait.until(ExpectedConditions.presenceOfElementLocated(specificValue));

Selenium有一种特殊的机制来处理"选择/选项"情况:

import org.openqa.selenium.support.ui.Select;  // this is how to import it
WebElement select = driver.findElement(Dictionary.CLASS.getLocation());
Select dropDown = new Select(select); 
dropDown.selectByValue("1");

后续问题的答案:使用显式等待:

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement select = wait.until(ExpectedConditions.presenceOfElement(Dictionary.CLASS.getLocation()));

如果在select中等待加载选项,恐怕您需要制作一个自定义ExpectedCondition(未测试):

public static ExpectedCondition<Boolean> selectContainsOption(
    final WebElement select, final By locator) {
    return new ExpectedCondition<Boolean>() {
        @Override
        public Boolean apply(WebDriver driver) {
            try {
                return elementIfVisible(select.findElement(locator));
            } catch (StaleElementReferenceException e) {
                return null;
            }
        }
    };
}

用法:

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement select = wait.until(ExpectedConditions.presenceOfElement(Dictionary.CLASS.getLocation()));
WebElement option = wait.until(selectContainsOption(select, By.cssSelector('.//option[@value = "1"]')));   

我试图做一些类似的事情-将WebDriverWaitExpectedConditions一起使用,这样我就可以等待元素出现,并将其定位为相对于现有元素的子元素。

Selenium中现在提供了其他方法来处理此问题:

static ExpectedCondition<WebElement> presenceOfNestedElementLocatedBy(By locator, By sub_locator)将子WebElement作为父元素的一部分进行检查以呈现的期望

static ExpectedCondition<WebElement> presenceOfNestedElementLocatedBy(WebElement element, By sub_locator)将子WebElement作为父元素的一部分进行检查以呈现的期望

static ExpectedCondition<java.util.List<WebElement>> presenceOfNestedElementsLocatedBy(By locator, By sub_locator)将子WebElement作为父元素的一部分进行检查以呈现的期望

因此,对于您的情况,您可以执行以下操作:

     wait.until(ExpectedConditions.presenceOfNestedElementLocatedBy(Dictionary.CLASS.getLocation(), By.cssSelector("option[value=2]")));

有关Javadocs,请参阅此处:https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html#presenceOfNestedElementLocatedBy-org.openqa.selenium.By-org.openqa.selennium.By-

相关内容

最新更新