webdriver C# - dropdown selectByVisibleText

2019-08-09 11:09发布

Trying to select a word inside a dropdown menu.

In Webdriver IDE appear this to click the dropdown (which Id is "p" and the to click the word "Barcelona" inside the dropdown.:

enter image description here

I just can open the dropdown menu using:

            driver.FindElement(By.Id("p")).Click();

Now I'm trying to select a word inside this dropdown menu, using "selectelement" and "select visibleText" but does not work in C# Webdriver for me.

            SelectElement selector = new SelectElement.selectByVisibleText("Barcelona");

Any helps please?

Using C# Webdriver and not java.

2条回答
Luminary・发光体
2楼-- · 2019-08-09 11:39

I think the problem is in the SelectElement initialization. You can try the following code:

SelectElement selectElement = new SelectElement(driver.FindElement(By.Id("p")));
selectElement.SelectByText("Germany");

If you are new in the C# WebDriver API, you can find the following article useful: http://automatetheplanet.com/getting-started-webdriver-c-10-minutes/

查看更多
Emotional °昔
3楼-- · 2019-08-09 11:59

In WebDriver.Support.dll Version: 3.1.0 we have SelectElement class. SelectElement mainly three methods.

Please find the methods:

  • To select text we can use SelectByText
  • To select index we can use SelectByIndex
  • To select Value we can use SelectByValue

Code samples: SelectByIndex

SelectElement selectObj = new SelectElement(driver.FindElement(By.Id("year")));
selectObj.SelectByIndex(4);

Code samples: SelectByText

SelectElement selectObj = new SelectElement(driver.FindElement(By.Id("year")));
selectObj.SelectByText("1990");

Code samples: SelectByValue

SelectElement selectObj = new SelectElement(driver.FindElement(By.Id("year")));
selectObj.SelectByValue("1990");

Please find attached photo for your reference Sample Image

Further clarification and other methods Refer

查看更多
登录 后发表回答