joh*_*970 2 html c# selenium automation
我在C#中使用Selenium,我需要能够根据与该单选按钮关联的标签文本指定网站上的哪个单选按钮.我需要能够将该文本作为参数传递.下面是单选按钮代码的示例(这是三个按钮):
<input id="radSelect152_222_1369" class="bgRadioNotChecked" type="radio" onclick="BGQ.Ajax.SelectAnswer(this, '152','1369','3', '547');" value="1369" onfocus="BGQ.View.setLastElement(true);" name="radSelect152_222">
<label class="inline" for="radSelect152_222_1369">Watermelon</label>
<input id="radSelect152_222_1370" class="bgRadioNotChecked" type="radio" onclick="BGQ.Ajax.SelectAnswer(this, '152','1370','3', '547');" value="1370" onfocus="BGQ.View.setLastElement(true);" name="radSelect152_222">
<label class="inline" for="radSelect152_222_1370">Papaya</label>
<input id="radSelect152_222_1371" class="bgRadioNotChecked" type="radio" onclick="BGQ.Ajax.SelectAnswer(this, '152','1371','3', '547');" value="1371" onfocus="BGQ.View.setLastElement(true);" name="radSelect152_222">
<label class="inline" for="radSelect152_222_1371">Mango</label>
Run Code Online (Sandbox Code Playgroud)
我希望能够在Excel输入文件中指定"Mango"(我将所有文件输入内容正常工作)并让Selenium单击相关的单选按钮.我读过的一种我一直在尝试的方法是执行以下操作:
问题是,我对Selenium很新,并且不能完全理解如何执行这四个步骤的语法.有人可以用特定的代码示例向我展示方式吗?另外,或者,是否有更好/更聪明的方法来做到这一点?如果是这样,请具体说明.
我在这里已经包含了我开始编写的方法,其中我传入了目标文本.我知道这是错的(特别是在By.XPath部分).
public void ClickRadioButtonByLabelText(string labelText)
{
// (1) Find the element that has the label text as its text
IWebElement labelForButton = commondriver.FindElement(By.XPath(//label[text()='labelText']));
// (2) Get the FOR attribute from that element
string forAttribute
// (3) Find the input that has an id equal to that FOR attribute
// (4) Click that input element
}
Run Code Online (Sandbox Code Playgroud)
谢谢.
你有两个选择.
选项1 - 在单个XPath查询中完成所有操作
XPath查询将是:
//input[@id=//label[text()='TestCheckBox']/@for]
Run Code Online (Sandbox Code Playgroud)
也就是说,get input
具有id
来自具有"TestCheckBox" 的for
属性的属性label
text
选项2 - 从标签中获取属性,然后以单独的步骤查找元素
public void ClickRadioButtonByLabelText(string labelText)
{
// (1) Find the element that has the label text as its text
IWebElement labelForButton = commondriver.FindElement(By.XPath("//label[text()='labelText']"));
// (2) Get the FOR attribute from that element
string forAttribute = labelForButton.GetAttribute("for");
// (3) Find the input that has an id equal to that FOR attribute
IWebElement radioElement = commondriver.FindElement(By.Id(forAttribute));
// (4) Click that input element
radioElement.Click();
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
11123 次 |
最近记录: |