【问题标题】:NoSuchElementException when using By.Xpath使用 By.Xpath 时出现 NoSuchElementException
【发布时间】:2014-12-03 13:10:25
【问题描述】:

编辑:

元素在 iframe 中,这就是它最终的工作方式:

WebDriverWait wait = new WebDriverWait(_driver, 60);
    wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By
            .xpath("//*[@class='IFrameID']")));
WebElement element_t = _driver.findElement(
          By.xpath("//*[@myattribute='mytest']"));

编辑:

我的问题似乎是页面的结构。我尝试了不同的东西,我只能通过 id 获取正文,我试图通过 id 获取的其他元素或任何其他属性都找不到...


我正在尝试使用 By.xpath 方法获取元素,xpath 本身在 firebug/firepath 中使用时工作得很好,但在 java 应用程序中使用时出现异常:

org.openqa.selenium.NoSuchElementException: Unable to locate element:    {"method":"xpath","selector":".//*[@myattribute='mytest']"}

我尝试访问的属性不是标准的 html 属性,而是从框架生成的,因此该字段如下所示:

<input id="F_19" class="FIELDInputEdit" type="text" style=" width:100%;" maxlength="40" myattribute="mytest" name="CC">

javacode 本身看起来像这样:

WebElement element_t = _driver.findElement(
          By.xpath(".//*[@myattribute='mytest']"));

由于唯一已知的属性是这个,我没有其他方法可以访问输入字段。 我正在使用 Firefox 17.0.11

【问题讨论】:

  • 我看到一个错字:myattribut vs myattribute。这是从复制到这里,还是实际问题?
  • 只是复制... sry
  • 您是否尝试过添加等待元素?可能是 selenium 在页面加载之后但在浏览器 javascript 将其添加到页面之前查找它。
  • 我现在试过没有效果:WebElement element_t = (new WebDriverWait(_driver, 10)) .until(ExpectedConditions.presenceOfElementLocated( By.xpath(".//*[@myattribute='mytest']")));
  • 您要查找的元素是否在 iFrame 中?

标签: java firefox selenium xpath


【解决方案1】:

在 webdriver 中使用 xpath 之前的良好做法(恕我直言)使用 selenium ide (http://docs.seleniumhq.org/download/) 测试它

错误可能是由于«.»前 ”//”。尝试删除它。

阅读:http://www.w3schools.com/xpath/xpath_syntax.asp

.//*[@myattribute='mytest']

«.» = 选择当前节点

«//» = 从当前节点中选择文档中与选择匹配的节点,无论它们在哪里

«*» = 匹配任何元素节点

«[@myattribute='mytest']» (= [@myattribute = \"mytest\"]) = 节点,包含属性“myattribute”,其值为“mytest”

现在,_driver.findElement(By.xpath("//*[@myattribute='mytest']")) = 在整个页面中搜索第一个节点,其属性为“myattribute”,值为“mytest”

_driver.findElement(By.xpath("//input[@myattribute='mytest']"))

= 使用 «myattribute» = «mytest» 搜索整个页面以查找第一个输入

_driver.findElement(By.xpath("//input[@myattribute='mytest']")).then(By.path("./*[@comeAtr]")) = 在输入中带有 «myattribute» = «mytest» 找到任何带有属性 = «

的节点

【讨论】:

  • 我的意思是我应该有 findAll 方法。您可以尝试查找 List webElements = driver.findAll(By.xpath("//input[@id]")) 并使用断点检查该元素是否存在(在页面上找到它,定位 id 值,在 ide 中查看这个值)
  • 很遗憾,我无法使用 ID,因为它是生成的且未知的。我所要做的就是这个属性。
  • 我在 firepath 和 java 中尝试了所有建议的变体,它们都在 firepath 中工作,没有一个来自 java............ 无法找到元素: {"method":"xpath","selector":"//input[@myattribute='mytest']"} 无法定位元素:{"method":"xpath","selector":".//input [@myattribute='mytest']"} 无法定位元素:{"method":"xpath","selector":"//*[@myattribute='mytest']"} 无法定位元素:{"method ":"xpath","selector":".//*[@myattribute='mytest']"}
【解决方案2】:

您是否尝试过改用 CSS 选择器?

By.cssSelector("input[myattribute=\"mytest\"]")

【讨论】:

    【解决方案3】:

    元素在 iframe 中,这就是它最终的工作方式:

    WebDriverWait wait = new WebDriverWait(_driver, 60);
      wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By
            .xpath("//*[@class='IFrameID']")));
    WebElement element_t = _driver.findElement(
          By.xpath("//*[@myattribute='mytest']"));
    

    【讨论】:

      猜你喜欢
      • 2022-01-17
      • 2019-10-13
      • 1970-01-01
      • 1970-01-01
      • 2019-08-15
      • 2020-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多