本文介紹了使用 Selenium 和 Java 自動(dòng)化基于 jQuery 的引導(dǎo)下拉菜單的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
我正在嘗試從引導(dǎo)下拉列表中列出所有元素,然后選擇某個(gè)值.但是,它返回 0 值.任何建議將不勝感激.
I am trying to list all elements from the bootstrap dropdown and then select a certain value. However, it returns 0 values. Any suggestions will be greatly appreciated.
driver.findElement(By.id("imgSelectButton")).click();
Thread.sleep(3000);
List<WebElement> list = driver.findElements(By.xpath("http://ul/li[@class='logoSelectOpt']//li"));
System.out.println(list.size());
for(int i=0; i<list.size(); i++){
System.out.println(list.get(i).getText());
if (list.get(i).getText().contains("History")){
list.get(i).click();
break;
}
}
DOM:
<div class="logoSelect" style="z-index:1; top:878px;">==$0
<ul>
<li class="logoSelectOpt" id="12" onmouseover="jQuery(QWE01Title.activate(this);" onmouseout="jQuery(QWE01Title.deactivate(this);" onmousedown="jQuery(QWE01Title.selectItem(this);" logoColor="#FFF">Facts</li>==0
<li class="logoSelectOpt" id="12" onmouseover="jQuery(QWE01Title.activate(this);" onmouseout="jQuery(QWE01Title.deactivate(this);" onmousedown="jQuery(QWE01Title.selectItem(this);" logoColor="#FFF">History</li>==0
<li class="logoSelectOpt" id="12" onmouseover="jQuery(QWE01Title.activate(this);" onmouseout="jQuery(QWE01Title.deactivate(this);" onmousedown="jQuery(QWE01Title.selectItem(this);" logoColor="#FFF">Opinions</li>==0
<li class="logoSelectOpt" id="12" onmouseover="jQuery(QWE01Title.activate(this);" onmouseout="jQuery(QWE01Title.deactivate(this);" onmousedown="jQuery(QWE01Title.selectItem(this);" logoColor="#FFF">Questions</li>==0
</ul>
</div>
推薦答案
元素是 jQuery 啟用元素所以找到您必須為 visibilityOfAllElementsLocatedBy()
誘導(dǎo) WebDriverWait 的元素,您可以使用以下任一 定位器策略:
The elements are jQuery enabled element so to locate the elements you have to induce WebDriverWait for the visibilityOfAllElementsLocatedBy()
and you can use either of the following Locator Strategies:
使用cssSelector:
driver.findElement(By.id("imgSelectButton")).click();
List<WebElement> list = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("div.logoSelect > ul li.logoSelectOpt")));
System.out.println(list.size());
for(int i=0; i<list.size(); i++){
System.out.println(list.get(i).getText());
if (list.get(i).getText().contains("History")){
list.get(i).click();
break;
}
}
使用xpath:
driver.findElement(By.id("imgSelectButton")).click();
List<WebElement> list = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("http://div[@class='logoSelect']/ul//li[@class='logoSelectOpt']")));
System.out.println(list.size());
for(int i=0; i<list.size(); i++){
System.out.println(list.get(i).getText());
if (list.get(i).getText().contains("History")){
list.get(i).click();
break;
}
}
這篇關(guān)于使用 Selenium 和 Java 自動(dòng)化基于 jQuery 的引導(dǎo)下拉菜單的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!