[英]How to click on the radio button through the element ID attribute using Selenium and C#
我正在尝试 select 一个单选按钮和输入元素,它具有组id
和In_Group
值。 有 4 个不同的单选按钮具有相同的 id 但不同的值,因此我正在尝试 select 我正在寻找的正确的单选按钮。
<input class="custom-radio" id="group" name="group" type="radio" value="In_Group">
我试过这样的事情:
driver.FindElement(By.XPath("//*[contains(@id='group' and @value='In_Group')]"))
但是找不到元素可以帮助我吗
要定位元素,您可以使用以下任一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>
元素,并且您可能会在理想情况下与之交互,因此您必须为所需的ElementToBeClickable()
诱导WebDriverWait ,并且您可以使用以下任一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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.