2013-05-09 31 views

回答

-1

你可以使用下面的命令。讓我知道它是否工作。

driver.findElement(By.id("id of the dropdown")).sendkeys("part of visible text"); 
driver.findElement(By.id("id of the dropdown")).sendKeys(Keys.ENTER); 
+0

非常感謝你對你的快速響應。這個問題是,我試圖在這裏找到的文本模式應該是起始文本。另外它不會從元素中釋放焦點。 – user2365105 2013-05-09 09:04:49

2

我沒有測試過這個,但這裏是你如何在C#中完成它,你應該能夠很容易地轉換成Java代碼。兩種方法我能想到的:

1)

string selBoxID = "id of select box"; 
string partialText = "option text to match"; 
driver.FindElement(By.XPath("//select[@id='" + selBoxID + "']/option[contains(text(), '" + partialText + "')]")).Click(); 

OR

2)

SelectElement elSel = new SelectElement(driver.FindElement(By.Id("id of select box"))); 
IList<IWebElement> opts = elSel.Options; 
foreach (IWebElement elOpt in opts) 
{ 
    if(elOpt.Text.Contains("partial text to look for"){ 
     elOpt.Click(); 
     return true; 
    } 
} 
return false; 
+0

+1,僅供參考,Java相當於「Select」類:https://code.google.com/p/selenium/source/browse/java/client/src/org/openqa/selenium/support/ ui/Select.java – Arran 2013-05-09 09:13:34

+0

感謝您的幫助,它對我有用(選項1)。 – user2365105 2013-05-09 09:20:24

+0

不客氣! – ragamufin 2013-05-09 11:42:44

0

也許這是否行得通呢?

new Select(driver.findElement(By.id("MyIdOrOtherSelector"))).selectByVisibleText("Something"); 

雖然我不確定是否允許部分文本。 還有

selectByValue(value) 
selectByIndex(index) 

如果他們使用任何

0

下面是該

WebElement dropdown = driverObj.findElement(By.id(id)); 
    dropdown.click(); 

    List<WebElement> options = dropdown.findElements(By.tagName("option")); 
    for(WebElement option : options){ 
     String optTxt = option.getText(); 
     if(optTxt.contains(partialText)){ 
      option.click(); 
      break; 
     } 
    } 
} 
1

C#與LINQ一個java代碼

var menuOptions = new SelectElement(Driver.FindElement({LocatorForMenu})).Options; 
var requiredOption = menuOptions.FirstOrDefault(element => element.Text.Contains(partialTextToMatch)); 
if (requiredOption == null) 
    throw new Exception("Wasn't able to select menu item: " + partialTextToMatch); 
requiredOption.Click(); 
相關問題