2015-02-06 15 views
3

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

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; 
    } 
} 

然后,如果类是用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)); 
+0

您可以在'findElement'的结果执行'findElement',但alec的解决方案更好。 – Richard 2015-02-06 18:21:07

回答

3

硒具有special mechanism处理“选择/选项”的情况:

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

答案的一个后续问题:使用Explicit Wait

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

在等待一个选择的情况下,以内部选择加载,我怕,你需要创建一个自定义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"]'))); 
+0

我会用这个;但是,我试图等待这个元素存在于页面上。我正在使用的网站制作了许多AJAX调用,并且我选择的最佳解决方案是在选择框之前等待选择框中存在单个值。 – bagelmakers 2015-02-06 18:19:52

+0

@bagelmakers没关系,更新了答案,检查出来,谢谢。 – alecxe 2015-02-06 18:23:36

+0

问题在于网页的Ajax调用较差,有时会在选择内找到元素并等待在其中选择一个选项之间更新值。我需要等待具体选项在具体的选择 – bagelmakers 2015-02-06 18:25:50

1

我试图做类似你的东西 - 使用WebDriverWaitExpectedConditions让我可以等待元素在那里,并将其定位为相对于现有元素的子元素。

另外的方法现在在硒可用来处理这样的:

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]"))); 

在这里看到的Javadoc:https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html#presenceOfNestedElementLocatedBy-org.openqa.selenium.By-org.openqa.selenium.By-