Java 使用By.Xpath时的NoTouchElementException

Java 使用By.Xpath时的NoTouchElementException,java,firefox,selenium,xpath,Java,Firefox,Selenium,Xpath,编辑: 元素位于iframe中,这是它最终的工作方式: WebDriverWait wait = new WebDriverWait(_driver, 60); wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By .xpath("//*[@class='IFrameID']"))); WebElement element_t = _driver.findElement(

编辑:


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

WebDriverWait wait = new WebDriverWait(_driver, 60);
    wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By
            .xpath("//*[@class='IFrameID']")));
WebElement element_t = _driver.findElement(
          By.xpath("//*[@myattribute='mytest']"));
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">
WebElement element_t = _driver.findElement(
          By.xpath(".//*[@myattribute='mytest']"));
因为唯一已知的属性是这个,所以我没有其他方法访问输入字段。 在webdriver中使用xpath之前,我正在使用Firefox 17.0.11

良好实践(imho),使用selenium ide()测试它

错误可能是因为«/»之前的«。»。尝试移除它

请阅读以下内容:

//*[@myattribute='mytest']

«。»=选择当前节点

«/»=从当前节点选择文档中与所选内容匹配的节点,无论它们位于何处

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

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

现在,
\u driver.findElement(By.xpath(“/*[@myattribute='mytest'])
=在整个页面中搜索第一个属性为«myattribute»且值为«mytest»的节点

_driver.findElement(By.xpath("//input[@myattribute='mytest']"))
=使用«myattribute»=«mytest»搜索整个页面的第一个输入

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

\u driver.findelelement(By.xpath(“//input[@myattribute='mytest'])。然后(By.path(“./*[@comatr]”)在输入中使用«myattribute»=«mytest»查找任何具有atribute=«

的节点是否尝试改用CSS选择器

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

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

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

我看到一个打字错误:
myattribute
vs
myattribute
。这是从复制到这里,还是实际的问题?只是从复制。。。sry您是否尝试添加等待元素?可能是selenium在页面加载之后,但在浏览器javascript将其添加到页面之前查找它。我现在尝试了,但没有效果:
WebElement元素(new WebDriverWait(_driver,10))。直到(ExpectedConditions.presenceOfElementLocated(By.xpath(./*[@myattribute='mytest'])
是您在iFrame中查找的元素吗?我想应该有一个findAll方法。您可以尝试查找List webElements=driver.findAll(By.xpath(“//input[@id]”)并使用断点检查此元素是否存在(在第页找到它,找到id值,在ide中查找此值)。很遗憾,我无法使用此id,因为它已生成且未知。我要做的就是这个属性。我尝试了firepath和java中所有建议的变体,它们都在firepath中工作,没有一个来自java。。。。。。。。。。。。无法定位元素:{“方法”:“xpath”,“选择器”:“//input[@myattribute='mytest']]”无法定位元素:{“方法”:“xpath”,“选择器”:“//input[@myattribute='mytest']]”无法定位元素:{“方法”:“xpath”,“选择器”:“/*[@myattribute='mytest']]”无法定位元素:{“方法”:“xpath”,“选择器”:“/*[@myattribute='mytest']]”}