通过元素ID属性单击单选按钮

通过元素ID属性单击单选按钮

本文介绍了如何使用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(),您可以使用以下任一定位器策略:

    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属性单击单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    08-22 23:52