java 如何使用硒在下拉列表中选择值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17512702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 02:12:23  来源:igfitidea点击:

How to select values in dropdownlist with selenium?

javaseleniumselenium-webdriverselenium-rctestng

提问by Elton da Costa

I am using selenium, TestNG and java with eclipse to perform an automated test. I'm having success with commands like click on a button (selenium.click ("button"), pass values ??to textboxes (selenium.type ("component", "value") and clicks too, but when it comes with a component type dropdown list (relating to common or asp.net MVC) I can not select the field with the command selenium.select ("field", "value").

我在 Eclipse 中使用 selenium、TestNG 和 java 来执行自动化测试。我在点击按钮(selenium.click(“button”),将值传递给文本框(selenium.type(“component”,“value”)和点击)等命令方面取得了成功,但是当它出现时组件类型下拉列表(与 common 或 asp.net MVC 相关)我无法使用命令 selenium.select ("field", "value") 选择字段。

To select the values ??and even the fields, I am using XPath to it, but even so, with the dropdown list can not, or can partially.

为了选择值甚至字段,我使用 XPath 来实现它,但即便如此,下拉列表也不能,或者可以部分使用。

When a drop-down list accepts the value I type, I can use the selenium.click but if not, nothing I've tried so far works.

当下拉列表接受我输入的值时,我可以使用 selenium.click 但如果没有,到目前为止我尝试过的任何内容都不起作用。

回答by Devi Kiran

using webdriver you can do it with Select class i have posted a code which was working below please have a look on that,Select Class had api to select the drop down values by its index as well as value,have a look on Select api for more info

使用 webdriver 你可以用 Select 类来完成更多信息

   public static void dropdown() 
    {
    WebDriver driver = new FirefoxDriver();
    driver.get("http://demosthenes.info/blog/166/HTML-Forms-Drop-down-Menus");
    Select sele = new Select(driver.findElement(By.id("state")));
    sele.selectByIndex(1);
    }

回答by Devdun

There are several ways to select elements from dropdown list. Below are some of them you can keep them as common drop-down operation and call what ever method you need.

有多种方法可以从下拉列表中选择元素。下面是其中的一些,您可以将它们保留为常见的下拉操作并调用您需要的任何方法。

//select the dropdown using "select by visible text"
        public static void dropDownSelectByText(WebElement webElement, String VisibleText){
            Select selObj=new Select(webElement);
            selObj.selectByVisibleText(VisibleText);
        }

//select the dropdown using "select by index"
public static void dropDownSelectByIndex(WebElement webElement, int IndexValue){
    Select selObj=new Select(webElement);
    selObj.selectByIndex(IndexValue);
}

//select the dropdown using "select by value"
public static void dropDownSelectByValue(WebElement webElement, String Value){
    Select selObj=new Select(webElement);
    selObj.selectByValue(Value);
}

You can call above methods like

您可以调用上述方法,例如

CommonPageOperations.dropDownSelectByValue(selectSubClientFromDropDownXpath,strSubClientName);

Drop down list appear only if mouse move to specific location, Then you have to use Actions as well

仅当鼠标移动到特定位置时才会出现下拉列表,然后您还必须使用操作

public void mouseMoveToExpandIcon(){
        Actions action = new Actions(driver);
        action.moveToElement(expandButtonXpath).perform();
    }

回答by Archana Singh

WebElement select = driver.findElement(By.id("selection"));
List<WebElement> options = select.findElements(By.tagName("option"));
for (WebElement option : options) {
  if("Germany".equals(option.getText()))
    option.click();
}

回答by testing

Actions actions = new Actions(driver);
WebElement dBox1= (new    WebDriverWait(driver,10)).until(ExpectedConditions.elementToBeClickable(By.id("selection""))).    selectByVisibleText("");
actions.moveToElement(dBox1);
actions.click();
actions.perform();

回答by SamK

You have to use Select in selenium to select from the drop down value.

您必须在 selenium 中使用 Select 从下拉值中进行选择。

//by ID

//通过ID

WebDriver driver = new FirefoxDriver();
new Select (driver.findElement(By.id("usState"))).selectByVisibleText("FL");

//by XPath

//通过XPath

new Select (driver.findElement(By.xpath("xPath for dropdown"))).selectByVisibleText("FL");