如何使用硒的webdriver选择在C#中的选项? [英] How to Using Webdriver Selenium for selecting an option in C#?
本文介绍了如何使用硒的webdriver选择在C#中的选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是想为我的Web测试中选择一个选项。一个例子可以在这里找到:<一href=\"http://www.tizag.com/phpT/examples/formex.php\">http://www.tizag.com/phpT/examples/formex.php
除了选择一个选项的一部分一切的伟大工程。如何通过值或标签中选择一个选项?
我的code:
使用OpenQA.Selenium.Firefox;
使用OpenQA.Selenium;
使用System.Collections.ObjectModel;
使用System.Text.RegularEx pressions;
使用的System.Threading;
使用System.Diagnostics程序;
使用System.Runtime.InteropServices;类GoogleSuggest
{
静态无效的主要()
{
IWebDriver司机=新FirefoxDriver(); //注意导航比Java版本略有不同
//这是因为'得到'是在C#关键字
。driver.Navigate()GoToUrl(http://www.tizag.com/phpT/examples/formex.php);
IWebElement查询= driver.FindElement(By.Name(其中fname));
query.SendKeys(约翰);
driver.FindElement(By.Name(L-NAME))的SendKeys(李四);
。driver.FindElement(By.XPath(//输入[@名称='性别'和@值='男']))点击();
。driver.FindElement(By.XPath(//输入[@名称='食[]'和@值='鸡']))点击();
driver.FindElement(By.Name(报价))多云()。
。driver.FindElement(By.Name(报价))的SendKeys(要present!);
driver.FindElement(By.Name(教育))的SendKeys(Keys.Down + Keys.Enter)。 //工作,但是这不是我一直在寻找
// driver.FindElement(By.XPath(//选项[@值='类证书高中']))点击();不工作
// driver.FindElement(By.XPath(\"/html/body/table[2]/tbody/tr/td[2]/table/tbody/tr/td/div[5]/form/select/option[2]\")).Click();不工作
// driver.FindElement(By.XPath(ID(examp')/ X:格式/ X:选择[1] / X:选项[2])),点击();不工作 }
}
解决方案
您必须从下拉列表中创建一个select元素的对象。
//选择下拉列表
VAR教育= driver.FindElement(By.Name(教育));
//创建选择元素对象
VAR selectElement =新SelectElement(教育); //按值选择
selectElement.SelectByValue(Jr.High);
//通过文本选择
selectElement.SelectByText(类证书高中);
更多信息这里
I was trying for my web test selecting an option. An example can be found here: http://www.tizag.com/phpT/examples/formex.php
Everything works great except the selecting an option part. How to selecting an option by value or by label ?
My Code:
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
}
}
解决方案
You must create a select element object from the drop down list.
// 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");
More info here
这篇关于如何使用硒的webdriver选择在C#中的选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文