【问题标题】:How to click on the radio button through the element ID attribute using Selenium and C#如何使用 Selenium 和 C# 通过元素 ID 属性单击单选按钮
【发布时间】:2020-10-08 14:04:30
【问题描述】:

我正在尝试选择一个单选按钮和输入元素,它有一个 id 组和 In_Group 值。有 4 个不同的单选按钮具有相同的 id 但不同的值,因此我正在尝试选择我正在寻找的正确的单选按钮。

<input class="custom-radio" id="group" name="group" type="radio" value="In_Group">

我尝试过这样的事情:

driver.FindElement(By.XPath("//*[contains(@id='group' and @value='In_Group')]"))

但是找不到元素,谁能帮帮我

【问题讨论】:

    标签: c# selenium xpath css-selectors webdriverwait


    【解决方案1】:

    要定位元素,您可以使用以下任一Locator Strategies

    • CssSelector:

      driver.FindElement(By.CssSelector("input#group[value='In_Group']"));
      
    • XPath:

      driver.FindElement(By.XPath("//input[@id='group' and @value='In_Group']"));
      

    但是,由于它是一个 &lt;input&gt; 元素,并且您可能会在理想情况下与之交互,因此您必须为所需的 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();
      

    【讨论】:

    • 嘿,//input[@id='group' and @value='In_Group'] 是我要找的,非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-27
    • 2011-02-18
    • 1970-01-01
    • 2019-08-24
    • 2020-09-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多