我正在尝试选择一个选项进行网络测试。可以在此处找到示例:http://www.tizag.com/phpT/examples/formex.php
除了选择选项部分之外,一切都很好。如何按值或按标签选择选项?
我的代码:
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium;
using System.Collections.ObjectModel;
using System.Text.RegularExpressions;
using System.Threading;
using System.Diagnostics;
using System.Runtime.InteropServices;
class GoogleSuggest
{
static void Main()
{
IWebDriver driver = new FirefoxDriver();
//Notice navigation is slightly different than the Java version
//This is because 'get' is a keyword in C#
driver.Navigate().GoToUrl("http://www.tizag.com/phpT/examples/formex.php");
IWebElement query = driver.FindElement(By.Name("Fname"));
query.SendKeys("John");
driver.FindElement(By.Name("Lname")).SendKeys("Doe");
driver.FindElement(By.XPath("//input[@name='gender' and @value='Male']")).Click();
driver.FindElement(By.XPath("//input[@name='food[]' and @value='Chicken']")).Click();
driver.FindElement(By.Name("quote")).Clear();
driver.FindElement(By.Name("quote")).SendKeys("Be Present!");
driver.FindElement(By.Name("education")).SendKeys(Keys.Down + Keys.Enter); // working but that's not what i was looking for
// driver.FindElement(By.XPath("//option[@value='HighSchool']")).Click(); not working
// driver.FindElement(By.XPath("/html/body/table[2]/tbody/tr/td[2]/table/tbody/tr/td/div[5]/form/select/option[2]")).Click(); not working
// driver.FindElement(By.XPath("id('examp')/x:form/x:select[1]/x:option[2]")).Click(); not working
}
}
您必须从下拉列表中创建一个选择元素对象。
using OpenQA.Selenium.Support.UI;
// select the drop down list
var education = driver.FindElement(By.Name("education"));
//create select element object
var selectElement = new SelectElement(education);
//select by value
selectElement.SelectByValue("Jr.High");
// select by text
selectElement.SelectByText("HighSchool");
更多信息这里
添加一点 - 在将 Selenium.NET 绑定安装到 C# 项目中后,我遇到了 OpenQA.Selenium.Support.UI 命名空间不可用的问题。后来发现我们可以通过运行命令轻松安装最新版本的 Selenium WebDriver 支持类:
Install-Package Selenium.Support
在 NuGet 包管理器控制台中,或从 NuGet 管理器安装 Selenium.Support。
其他方式可能是这样的:
driver.FindElement(By.XPath(".//*[@id='examp']/form/select[1]/option[3]")).Click();
您可以更改 option[x] 中的索引,通过要选择的元素数量更改 x。
我不知道这是否是最好的方法,但希望对你有帮助。
用于从下拉列表中选择项目的 Selenium WebDriver C# 代码:
IWebElement EducationDropDownElement = driver.FindElement(By.Name("education"));
SelectElement SelectAnEducation = new SelectElement(EducationDropDownElement);
有 3 种选择下拉项目的方法: i) 按文本选择 ii) 按索引选择 iii) 按值选择
通过文字选择:
SelectAnEducation.SelectByText("College");//There are 3 items - Jr.High, HighSchool, College
按索引选择:
SelectAnEducation.SelectByIndex(2);//Index starts from 0. so, 0 = Jr.High 1 = HighSchool 2 = College
按值选择:
SelectAnEducation.SelectByValue("College");//There are 3 values - Jr.High, HighSchool, College
通过文本选择选项;
(new SelectElement(driver.FindElement(By.XPath(""))).SelectByText("");
通过值选择选项:
(new SelectElement(driver.FindElement(By.XPath(""))).SelectByValue("");
您只需传递值并输入密钥即可:
driver.FindElement(By.Name("education")).SendKeys("Jr.High"+Keys.Enter);
这就是它对我的作用(通过 ID 选择控件和通过文本选择选项):
protected void clickOptionInList(string listControlId, string optionText)
{
driver.FindElement(By.XPath("//select[@id='"+ listControlId + "']/option[contains(.,'"+ optionText +"')]")).Click();
}
用途:
clickOptionInList("ctl00_ContentPlaceHolder_lbxAllRoles", "Tester");
如果您只是从下拉框中查找任何选择,我还发现“按索引选择”方法非常有用。
if (IsElementPresent(By.XPath("//select[@id='Q43_0']")))
{
new SelectElement(driver.FindElement(By.Id("Q43_0")))**.SelectByIndex(1);** // This is selecting first value of the drop-down list
WaitForAjax();
Thread.Sleep(3000);
}
else
{
Console.WriteLine("Your comment here);
}
var select = new SelectElement(elementX);
select.MoveToElement(elementX).Build().Perform();
var click = (
from sel in select
let value = "College"
select value
);
IWebElement element = _browserInstance.Driver.FindElement(By.XPath("//Select"));
IList<IWebElement> AllDropDownList = element.FindElements(By.XPath("//option"));
int DpListCount = AllDropDownList.Count;
for (int i = 0; i < DpListCount; i++)
{
if (AllDropDownList[i].Text == "nnnnnnnnnnn")
{
AllDropDownList[i].Click();
_browserInstance.ScreenCapture("nnnnnnnnnnnnnnnnnnnnnn");
}
}
如果您不想安装额外的 Nuget 包(“Selenium 支持”),您可以在页面上执行 JavaScript
((IJavaScriptExecutor)driver)
.ExecuteScript("document.getElementById('mySelect').value=123;");