How to click on the radio button through the element ID attribute using Selenium and C#(如何使用 Selenium 和 C# 通过元素 ID 属性单击单选按钮)
问题描述
我正在尝试选择一个单选按钮和输入元素,它有一个组的 id 和 In_Group 的值.有 4 个不同的单选按钮具有相同的 id 但不同的值,因此我正在尝试选择我正在寻找的正确的单选按钮.
I am trying to select a radio button and input element, it has an id of group and value of In_Group. There are 4 different radio buttons with the same id but different values hence I am trying to select the correct one i am looking for.
<input class="custom-radio" id="group" name="group" type="radio" value="In_Group">
我尝试过这样的事情:
driver.FindElement(By.XPath("//*[contains(@id='group' and @value='In_Group')]"))
但是找不到元素,谁能帮帮我
But the element is not found could someone help me out
推荐答案
要定位元素,您可以使用以下任一方法 定位器策略:
To locate the element you can use either of the following Locator Strategies:
CssSelector:
driver.FindElement(By.CssSelector("input#group[value='In_Group']"));
XPath:
driver.FindElement(By.XPath("//input[@id='group' and @value='In_Group']"));
但是,由于它是一个 <input> 元素,并且您可能会在理想情况下与之交互,因此您必须诱导 WebDriverWait 用于所需的 ElementToBeClickable() 并且您可以使用以下任一 Locator Strategies:
However, as it is a <input> element and possibly you will interact with it ideally you have to induce WebDriverWait for the desired ElementToBeClickable() and you can use either of the following Locator Strategies:
CssSelector:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("input.custom-radio#group[value='In_Group'][name='group']"))).Click();
XPath:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//input[@id='group' and @value='In_Group'][@class='custom-radio' and @name='group']"))).Click();
这篇关于如何使用 Selenium 和 C# 通过元素 ID 属性单击单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 Selenium 和 C# 通过元素 ID 属性单击单选
基础教程推荐
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
