2015-02-06 33 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-