java如何读取下拉列表的值_java - 如何在Selenium 2中选择/获取下拉选项
java - 如何在Selenium 2中選擇/獲取下拉選項
我正在將我的selenium 1代碼轉換為selenium 2,并且無法找到在下拉菜單中選擇標簽的任何簡單方法或獲取下拉列表的選定值。 你知道如何在Selenium 2中做到這一點嗎?
以下是兩個在Selenium 1中有效但在2中不起作用的語句:
browser.select("//path_to_drop_down", "Value1");
browser.getSelectedValue("//path_to_drop_down");
8個解決方案
183 votes
查看有關使用selenium文檔中的webdriver和Select類的javadoc填寫表單的部分。
要根據標簽選擇選項:
Select select = new Select(driver.findElement(By.xpath("//path_to_drop_down")));
select.deselectAll();
select.selectByVisibleText("Value1");
要獲得第一個選定值:
WebElement option = select.getFirstSelectedOption()
janderssn answered 2019-08-11T12:22:16Z
5 votes
driver.findElement(By.id("id_dropdown_menu")).click();
driver.findElement(By.xpath("xpath_from_seleniumIDE")).click();
祝好運
thrasher answered 2019-08-11T12:22:36Z
4 votes
在紅寶石中不斷使用,添加如下:
module Selenium
module WebDriver
class Element
def select(value)
self.find_elements(:tag_name => "option").find do |option|
if option.text == value
option.click
return
end
end
end
end
end
你將能夠選擇價值:
browser.find_element(:xpath, ".//xpath").select("Value")
AlekseiPetrovski answered 2019-08-11T12:23:07Z
3 votes
嘗試使用:
selenium.select("id=items","label=engineering")
要么
selenium.select("id=items","index=3")
coolcub answered 2019-08-11T12:23:28Z
0 votes
與janderson上面發布的內容類似的選項就是在selenium 2中使用.GetAttribute方法。使用此方法,您可以獲取具有您正在尋找的特定值或標簽的任何項目。 這可用于確定元素是否具有標簽,樣式,值等。執行此操作的常用方法是循環顯示下拉列表中的項目,直到找到所需的項目并選擇它。 在C#中
int items = driver.FindElement(By.XPath("//path_to_drop_Down")).Count();
for(int i = 1; i <= items; i++)
{
string value = driver.FindElement(By.XPath("//path_to_drop_Down/option["+i+"]")).GetAttribute("Value1");
if(value.Conatains("Label_I_am_Looking_for"))
{
driver.FindElement(By.XPath("//path_to_drop_Down/option["+i+"]")).Click();
//Clicked on the index of the that has your label / value
}
}
Ben answered 2019-08-11T12:23:53Z
0 votes
你可以這樣做:
public void selectDropDownValue(String ValueToSelect)
{
webelement findDropDownValue=driver.findElements(By.id("id1")) //this will find that dropdown
wait.until(ExpectedConditions.visibilityOf(findDropDownValue)); // wait till that dropdown appear
super.highlightElement(findDropDownValue); // highlight that dropdown
new Select(findDropDownValue).selectByValue(ValueToSelect); //select that option which u had passed as argument
}
Praveen answered 2019-08-11T12:24:17Z
0 votes
此方法將返回下拉列表的選定值,
public static String getSelected_visibleText(WebDriver driver, String elementType, String value)
{
WebElement element = Webelement_Finder.webElement_Finder(driver, elementType, value);
Select Selector = new Select(element);
Selector.getFirstSelectedOption();
String textval=Selector.getFirstSelectedOption().getText();
return textval;
}
與此同時
String textval = Selector.getFirstSelectedOption();
element.getText();
將返回下拉列表中的所有元素。
Jophin P John answered 2019-08-11T12:25:10Z
-2 votes
這是從下拉列表中選擇值的代碼
selectlocator的值將是下拉框的xpath或名稱,而optionLocator將具有從下拉框中選擇的值。
public static boolean select(final String selectLocator,
final String optionLocator) {
try {
element(selectLocator).clear();
element(selectLocator).sendKeys(Keys.PAGE_UP);
for (int k = 0; k <= new Select(element(selectLocator))
.getOptions().size() - 1; k++) {
combo1.add(element(selectLocator).getValue());
element(selectLocator).sendKeys(Keys.ARROW_DOWN);
}
if (combo1.contains(optionLocator)) {
element(selectLocator).clear();
new Select(element(selectLocator)).selectByValue(optionLocator);
combocheck = element(selectLocator).getValue();
combo = "";
return true;
} else {
element(selectLocator).clear();
combo = "The Value " + optionLocator
+ " Does Not Exist In The Combobox";
return false;
}
} catch (Exception e) {
e.printStackTrace();
errorcontrol.add(e.getMessage());
return false;
}
}
private static RenderedWebElement element(final String locator) {
try {
return (RenderedWebElement) drivers.findElement(by(locator));
} catch (Exception e) {
errorcontrol.add(e.getMessage());
return (RenderedWebElement) drivers.findElement(by(locator));
}
}
謝謝,
雷卡。
Kartmcad answered 2019-08-11T12:25:50Z
總結
以上是生活随笔為你收集整理的java如何读取下拉列表的值_java - 如何在Selenium 2中选择/获取下拉选项的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java泛型机制_Java泛型机制必要性
- 下一篇: mysql dump 导入导出_使用my