![](/img/trans.png)
[英]How can I find out what type of c# project I'm using in Visual Studio 2017?
[英]How do I find and select a dropdown value with Selenium using C# in Visual Studio 2017
如果下拉列表是用 select 和 option 標簽定義的,那么您可以使用SelectElement
類從下拉列表中選擇值。
您可以使用任何一種方法從下拉列表中選擇值請參閱文檔
SelectByIndex - 按索引選擇一個選項
SelectByText - 通過顯示的文本選擇一個選項。
SelectByValue - 按值選擇一個選項。
您需要將下拉元素傳遞給SelectElement
類,並且可以使用上述任何一種方法
代碼:
IWebElement Depart = driver.FindElement(By.Name("fromPort"));
SelectElement select=new SelectElement(Depart);
選項1:
select.SelectByText("Sydney");
選項 2:
select.SelectByValue("Sydney");
選項 3:
select.SelectByIndex(8);//Sydney value index is 8
為此使用以下代碼:
using OpenQA.Selenium.Support.UI;
var selectElement = new SelectElement(driver.FindElement(By.Name("fromPort")));
selectElement.SelectByText("London");
希望對你有幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.