【问题标题】:Selenium web driver can't find elementSelenium Web 驱动程序找不到元素
【发布时间】:2014-10-25 17:30:49
【问题描述】:

我在尝试使用 Selenium Web 驱动程序查找 Web 元素时遇到问题。

我已经用两个页面完成了这个,我想要抓取的两个元素都在很多类、id 等中。

这是第一个元素,我没有遇到任何问题。

<input type="text" class="input-large" name="host" value="">

我用这个来抓的:

WebElement HtmlHost = driver.findElement(By.name("host"));

现在这是我的问题开始的地方。我现在正试图抓住这个:

<button type="submit" name="bootBtn" class="btn btn-inverse btn-large">Start hashing</button>

我尝试使用所有这些功能抓取,但没有一个能够找到该元素。

driver.findElement(By.partialLinkText("Start "));
driver.findElement(By.linkText("Start Hashing"));
driver.findElement(By.name("bootBtn"));
driver.findElement(By.cssSelector("button[type='submit']"));

你建议我做什么?

谢谢。

【问题讨论】:

  • 这些看起来都应该工作,但试试By.CssSelector("button[type='submit']")怎么样?
  • 不,那也行不通。还有什么建议吗?
  • 依赖链接文本的方法将不起作用,因为您正在寻找button,而通过链接文本搜索的方法仅搜索a 元素。按名称搜索和按 css 选择器搜索应该可以工作。按钮是否仅在其他内容出现后才显示在页面上?另外,您收到的确切错误消息是什么?
  • 当您加载页面时,该按钮就在那里。我收到 NoSuchElementException 错误。
  • 是一个框架内的元素吗?

标签: java selenium webdriver


【解决方案1】:

driver.findElement(By.xpath("//button[contains(text(),'开始散列')]"));
这将抓取元素。

【讨论】:

    【解决方案2】:

    好的,我发现了问题,我觉得我错过了这个问题很愚蠢。我试图找到该按钮的网站要求您登录。我已经知道这一点,因为我已经编写了登录过程,但是当我创建这个新的 void 时,我将它放在登录过程之前。

    抱歉浪费时间,谢谢帮助!

    【讨论】:

    • 那么请删除您的问题,因为这对其他人没有用处。
    【解决方案3】:

    试试这个

    Thread.sleep(2000);
    driver.findElement(By.xpath("//button[@type='submit']"));
    

    【讨论】:

      猜你喜欢
      • 2018-08-15
      • 2016-09-28
      • 1970-01-01
      • 2015-12-16
      • 1970-01-01
      • 2017-10-16
      • 2018-01-15
      • 1970-01-01
      • 2014-03-09
      相关资源
      最近更新 更多