【问题标题】:select radio button in selenium c#在 selenium c# 中选择单选按钮
【发布时间】:2017-01-17 17:32:17
【问题描述】:

我是 XPath 和 CssSelector 的新手。

下面是目标html源代码。

<input value="1" name="uji.model.611876.button" type="radio"></input>

611876 是一个随机数。

我尝试了代码:

driver.FindElement(By.Id("//input[@value=\"1\"]")).Click();

driver.FindElement(By.Id("//input[@value='1']")).Click();

但是发生了无法定位元素错误。

这种情况我需要帮助。感谢您的阅读。

【问题讨论】:

  • 看起来你的 XPath 是正确的,但另一个元素阻止它被点击。您使用的是复杂的小部件吗?
  • 我无法理解复杂小部件的含义,但我在 c# 中使用 selenium webdriver for firefox。

标签: c# selenium xpath


【解决方案1】:

如果您得到ElementNotVisibleException,请尝试等待一段时间,直到目标input 变得可见:

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(15));
IWebElement element = wait.Until(ExpectedConditions.ElementIsVisible(By.XPath("//input[starts-with(@name, \"uji.model.\")][@type=\"radio\"]")));
element.Click();

【讨论】:

  • 另外,有时该元素可能会被某些菜单或与其重叠的东西阻止。在这些情况下也会发生此错误。
  • @DhirajDas,是的。有时候这种情况会发生。如果此代码不能解决当前问题,我将根据您的提示使用解决方案对其进行更新
  • 我试过了。但是 webdriver 在加载后仍在等待。我认为我的 XPath 是错误的。
  • @8berry,你试过我的XPath了吗?在您的代码中,您使用XPath 搜索id - 这就是问题所在。还要解释一下webdriver is still waiting after loading是什么意思?
  • @Andersson 是的,但症状是一样的。
【解决方案2】:

你可以这样做

driver.findElement(By.tagName("input")).Click();

【讨论】:

  • 谢谢。但是 Element is not visible 发生错误。
【解决方案3】:

如果有问题,你可以尝试JavascriptExecuter执行javascript代码-

IWebElement element= driver.FindElement(By.XPath("//input[@value=\"1\"]")));
IJavaScriptExecutor js = driver as IJavaScriptExecutor;
js.ExecuteScript("arguments[0].click();", element);

【讨论】:

    猜你喜欢
    • 2015-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-19
    • 2017-05-24
    相关资源
    最近更新 更多