【问题标题】:Selenium webdriver C#: Unable to identify button element inside form tag of HTMLSelenium webdriver C#:无法识别 HTML 表单标签内的按钮元素
【发布时间】:2018-05-16 18:51:49
【问题描述】:

Full Html Code

我需要点击带有 id(export_but) 的按钮

在图片中可以看到表单标签的 HTML 代码,当我点击一个按钮时会弹出表单。 ,尝试通过 XPath、CssSelector、Id 等所有可用选项查找元素。

而且弹出窗口不是警报,所以我尝试了

driver.SwitchTo().Alert()

它不起作用。

在识别此元素时需要帮助。

【问题讨论】:

  • 向我们展示您的代码和 html
  • 确认是来自新窗口或框架,如果是,则需要先切换到该窗口/框架,如果不是,在找到按钮之前添加一些睡眠
  • 你有什么错误吗?
  • 请检查是否在iframe中
  • 它不是 iframe。表单在对话框中弹出。而且我尝试切换窗口和 iframe 仍然不起作用。让睡眠也不起作用。我正在等待超时并且元素不可见错误。请在下面查看我的代码:wait.Until(ExpectedConditions.ElementIsVisible(By.LinkText(sLinkText))); IWebElement eElement = driver.FindElement(By.LinkTexT(sLinkText)); eElement.Click();

标签: c# selenium selenium-webdriver xpath css-selectors


【解决方案1】:

根据您提供的 HTML 和您的问题 当您单击按钮时会弹出表单,以识别您必须诱导的所需元素 WebDriverWait 使<input> 元素可点击,您可以使用以下任一定位器策略

  • cssSelector

    new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("input.submit#export_but[value='Export']")));
    
  • xpath

    new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//input[@class='submit' and @id='export_but' and @value='Export']")));
    

更新

根据您的评论更新,由于您仍然无法找到该元素,您也可以调用ExpectedConditions 方法ElementIsVisible(),如下所示:

  • cssSelector

    IWebElement element = new WebDriverWait(driver, TimeSpan.FromSeconds(3)).Until(ExpectedConditions.ElementIsVisible(By.CssSelector("input.submit#export_but[value='Export']")));
    element.Submit();
    
  • xpath

    IWebElement element = new WebDriverWait(driver, TimeSpan.FromSeconds(3)).Until(ExpectedConditions.ElementIsVisible(By.XPath("//input[@class='submit' and @id='export_but' and @value='Export']")));
    element.Submit()
    

【讨论】:

  • 我也使用过 webdriver wait 并尝试使用 XPath、CssSelector、Id。它仍然无法识别
  • @ShravanKoneru 用更多的outerHTML 更新问题。另外在开发控制台中遍历 DOM 并尝试观察元素是否在 iframe
  • DOM 中有一个 iframe,但该 webElement 不属于该 iframe。我添加了整个 html 的新图像(完整的 Html 代码)。请检查
  • 查看我更新的答案并让我知道状态
  • 您能否使用基于文本的 HTML 更新问题以进行进一步分析?
猜你喜欢
  • 2013-09-20
  • 1970-01-01
  • 2016-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-22
  • 1970-01-01
  • 2016-04-14
相关资源
最近更新 更多