使用CSS選擇器:
您可以通過CSS選擇器發現它獲得第n個孩子的價值。
// first child: will return "Option 1"
driver.findElement(By.cssSelector("ul.rcbList > li:nth-child(1)"));
// second child: will return "Option 2"
driver.findElement(By.cssSelector("ul.rcbList > li:nth-child(2)"));
// nth child: will return "Option n"
driver.findElement(By.cssSelector("ul.rcbList > li:nth-child(n)"));
在當<li>
項目是動態的情況下,然後得到計數和遍歷並獲得所有的值。
var el_count = driver.FindElements(By.CssSelector("ul.rcbList"));
for(int index=0; index < el_count.count(); index++){
// 0 (zero) is the first element in <ul> DOM Array
driver.findElement(By.cssSelector("ul > li:nth-child(index)"));
}
通過查找文本元素(值)
更簡單的方法就是通過XPath和類
var title = driver.FindElement(By.XPath("./div[@class='aCont']/div/a/span[text() = 'TextToFind']"));
// now title contains text, title = "text to be find"
更多-Example code
這是一個'ul'風格看起來像一個下拉列表?還是你的意思是使用一個實際的'select'元素? –
這是一個'ul'元素,看起來像一個下拉式。 – Annie