如何使用Selenium和C#通过元素ID属性点击单选按钮
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')]"))
但是找不到元素,谁能帮帮我
要定位元素,您可以使用以下任一方法 :
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 并且您可以使用以下任一方法定位器策略:
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();
我正在尝试 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')]"))
但是找不到元素,谁能帮帮我
要定位元素,您可以使用以下任一方法
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 并且您可以使用以下任一方法定位器策略:
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();