无法使用 Selenium C# 单击单选按钮

Unable to Click on Radio button using Selenium C#

下面是我的 HTML 应用程序片段。我的方案是单击单选按钮。我使用的 xpath 是 //input[@id='authorizedContact1'] 但我仍然无法单击单选按钮。

HTML是:

<div class="radio">
<input id="authorizedContact1" name="authorizedContactValue" class="authorizedContact" type="radio">
<label for="authorizedContact1">
::before ==[=10=]
"YES"
::after
</label>
</div>

这是我在 Selenium 中使用的代码

WebDriverWait wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(100));
            IWebElement element = wait.Until(ExpectedConditions.ElementIsVisible(By.XPath("//input[@id='authorizedContact1']")));
            element.Click();

我将 XPATH 用作 //input[@id='authorizedContact1'] 但仍然无法单击单选按钮 谁能帮我解决这个问题

由于您需要 Click() 在您需要为 ElementToBeClickable() 引入 WebDriverWait 的元素上,您可以使用 :

  • CssSelector:

    new WebDriverWait(_driver, TimeSpan.FromSeconds(100)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("label[for='authorizedContact1']"))).Click();
    
  • XPath:

    new WebDriverWait(_driver, TimeSpan.FromSeconds(100)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//label[@for='authorizedContact1']"))).Click();