【发布时间】:2017-12-23 19:04:43
【问题描述】:
我有任意数量的具有唯一类名的图块。其中一个有一个独特的标题,所以我正试图专门抓住那个。完成此操作后,我想要在其结构中相当深入地引用子元素。
做“平铺”。 tile.findElement() 的一部分在这里为我做什么?我想专门使用相对于 tile 的 xpath。
public WebElement getArticleTileByTitle(String title){
By xpath = By.xpath(".//div[contains(., '" + title + "') or @title='" + title + "']");
List<WebElement> tiles = findPresentElements(By.className("articleTile"));
for(WebElement tile : tiles){
if(isElementPresent(tile, xpath))
return tile;
}
return null;
}
public boolean articleTileImageIsVisible(WebElement tile){
WebElement background = tile.findElement(By.xpath("/*[2]/*[2]/*[1]/*[3]")); //This throws NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"/*[2]/*[2]/*[1]/*[3]"}
//figure out stuff about background
}
我知道背景是 img 类型的,所以我试过了
tile.findElement(By.xpath("./img/*[2]/*[2]/*[1]/*[3]"));
tile.findElement(By.xpath("./*/*[2]/*[2]/*[1]/*[3]"));
tile.findElement(By.xpath(".//*/*[2]/*[2]/*[1]/*[3]"));
tile.findElement(By.xpath("./*/*[2]/*[2]/*[1]/*[3]"));
不过如此,我不断收到关于 NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"/[2]/[ 2]/[1]/[3]"} 这不是有效的 xpath。
这个问题与Selenium 2 - Can findElement(By.xpath) be scoped to a particular element? 非常相似,但人们似乎要么误解了它,要么我的用例不同。
【问题讨论】:
-
您是否将“tiles”用于其他用途,或者您只是将其用作获取子元素的垫脚石?顺便说一句,您试图用来获取子元素的 xpath 太可怕了。这种风格肯定会被破坏并且很难调试。如果您发布 html 示例,我相信我们能够为您提供更好的东西。
-
网站的网址是什么?
标签: java selenium xpath selenium-webdriver