【问题标题】:selenium webdriver can't find elementselenium webdriver找不到元素
【发布时间】:2013-11-06 10:20:52
【问题描述】:

我使用 selenium webdriver 最新版本。

Selenium 找不到元素(输入字段)。这是html代码:

<input id="findPath" class="pathCom" type="text" style="width: 100%; 
background-color: rgb(255, 255, 255);" name="$path$conFind" 
value="find" data-ctl="["TextInput"]">

我使用这样的硒代码:

driver.findElement(By.xpath(".//input[@id='findPath']")).sendKeys("find");

我也尝试通过 id 或 css 定位器查找。我在这段代码之前设置了等待:

WebDriverWait wait = new WebDriverWait(driver, 15);
    wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(" driver.findElement(By.xpath(".//input[@id='findPath']")).sendKeys("find");")));

我尝试使用 Selenium IDE,它找到了这个元素。我不知道 webdriver 有什么问题。 有人遇到过这样的问题吗?

【问题讨论】:

  • 尝试删除开头的“.”并仅使用“//input[@id='findPath']”。
  • 这个我试过了,谢谢

标签: html eclipse selenium-webdriver element


【解决方案1】:

这不是在 WebDriverWait 语句中使用 sendkeys() 的正确方式,

你可以试试这个,

WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//input[@id='findPath']"))).sendKeys("find");

如果你没有使用 wait 变量,你可以通过以下方式避免它

new WebDriverWait(driver, 15).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//input[@id='findPath']"))).sendKeys("find");

如果上述语句有效,那么使用 id 肯定会有效,并且它还可以提高您的代码性能。..

new WebDriverWait(driver, 15).until(ExpectedConditions.presenceOfElementLocated(By.id("findPath"))).sendKeys("find");

【讨论】:

    【解决方案2】:

    您的代码无法正常工作的原因是一个非常简单的错误。您在 xpath 中包含一个句点

    你的代码-

    driver.findElement(By.xpath(".//input[@id='findPath']")).sendKeys("find");
    

    正确的代码-

    driver.findElement(By.xpath("//input[@id='findPath']")).sendKeys("find");
    

    让我知道它是否有效!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-11
      • 2016-09-07
      • 2017-10-24
      • 1970-01-01
      • 1970-01-01
      • 2016-04-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多