600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > java如何读取下拉列表的值_java - 如何在Selenium 2中选择/获取下拉选项

java如何读取下拉列表的值_java - 如何在Selenium 2中选择/获取下拉选项

时间:2021-08-17 03:06:11

相关推荐

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 -08-11T12:22:16Z

5 votes

driver.findElement(By.id("id_dropdown_menu")).click();

driver.findElement(By.xpath("xpath_from_seleniumIDE")).click();

祝好运

thrasher answered -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 -08-11T12:23:07Z

3 votes

尝试使用:

selenium.select("id=items","label=engineering")

要么

selenium.select("id=items","index=3")

coolcub answered -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 -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 -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 -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 -08-11T12:25:50Z

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。