2017-07-10 89 views
-1

我想知道如何從值池中的下拉列表中選擇特定值。 我的邏輯是使用硒搜索並從下拉列表中選擇值Webdriver

1.Open the page 
2.Fetch all the values from dropdown in list 
3.Use a loop 
4.Look for that value 
5.If the value is not there,select the value x 

對我來說,這是說沒有這樣的元素異常

它是我們需要關注的是元素第一 在我的代碼,我想選擇Nextran Corporation 下面是我的代碼

@Test(dependsOnMethods={"go2personalsettings"}) 
    void setru() 
    { 
     driver.switchTo().frame("contentFrame"); 
     Select rudropdown=new Select(driver.findElement(By.id("DefaultOrganisationDropDown"))); 
     List<WebElement> drop=rudropdown.getOptions(); 
     int e=drop.size(); 
     String actual_ru="999425, NEXTRAN CORPORATION - JACKSONVILLE"; 
     for(int i=0;i<e;i++) 
     { 
      String expected_ru=drop.get(i).getText(); 
      if(!expected_ru.equals(actual_ru)) 
      { 
      rudropdown.selectByValue(actual_ru);  
      } 

    } 
+1

嘗試改變'如果(!expected_ru.equals(actual_ru))''來,如果(expected_ru.equals(actual_ru))'謝謝 – DebanjanB

+0

更換drop.get(I).getText()與drop.get( ⅰ).getAttribute( 「的innerText」)。如果這不起作用,請發佈錯誤堆棧跟蹤 –

+0

以上解決方案都不起作用。 下拉框架和動作應該從下拉列表中選擇具體的值。 我們是否需要在這裏應用用戶操作,如移動到元素 –

回答

0

試試這個:

WebDriverWait wait = new WebDriverWait(driver, 15); 

wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//li[contains(text(),'" + ElementValue + "')]"))); 

WebElement webElement = driver.findElement(By.xpath("//li[contains(text(),'" + ElementValue + "')]")); 

webElement.click(); 
0

您的代碼中的邏輯與您陳述的意圖不符。你聲明你想從下拉列表中選擇一個特定的值,如果它存在的話。如果不是,請選擇值'x'。你的代碼永遠不會選擇'x'值,所以我不確定'x'是什麼,但你可以在下面填寫。代碼基本上試圖選擇期望的值。如果該期望值不存在,它將拋出一個被捕獲的異常,然後選擇'x'。如果它存在,則選擇該值並選擇「x」被跳過。

driver.switchTo().frame("contentFrame"); 
Select rudropdown = new Select(driver.findElement(By.id("DefaultOrganisationDropDown"))); 
String actual_ru = "999425, NEXTRAN CORPORATION - JACKSONVILLE"; 
try 
{ 
    rudropdown.selectByValue(actual_ru); 
} 
catch (NoSuchElementException e) 
{ 
    // expected value does not exist, select the value x instead 
    rudropdown.selectByValue("x"); 
} 
+0

現在它的工作正常。 版主可以關閉此主題。 –

+0

如果這是你之後的事,你可以自己刪除這個問題。 – JeffC

相關問題