示例场景:
问题是:
问题:
如何从对象List<WebElement> findList
指向的对象同级中检索所有信息?
试用版:
By mainList = By.xpath("//a[@class='*******']");
By findSubInfo = By.xpath("//a[@class='*****']//following::div[@class='****']");
List<WebElement> findList = driver.findElements(mainList);
for (WebElement webElement : findList ) {
if (webElement.isDisplayed()) {
String temp1= "Info1:" + webElement.getAttribute("ng-href");
String temp2= "Info2: " + driver.findElement(findSubInfo).getText();
}
}
OR
String mainList "//a[@class='*******']";
String findSubInfo = "//following::div[@class='****']";
List<WebElement> findList = driver.findElements(By.xpath(mainList));
for (WebElement webElement : findList ) {
if (webElement.isDisplayed()) {
String temp1= "Info1:" + webElement.getAttribute("ng-href");
String temp2= "Info2: " + driver.findElement(By.xpath(webElement + findSubInfo )).getText();
}
}
异常详细信息:
SyntaxError:无法在“文档”上执行“评估”:字符串 '[[ChromeDriver:XP上的Chrome(3208ef0a2ffd32812c33e159291eebe4)]-> xpath:// a [@ class ='noDecoration addPointer']] //以下:: div [@ class ='assignedAccountMasterAddress']' 不是有效的XPath表达式。
答案 0 :(得分:1)
如果要搜索从当前元素(webelement
开始具有相关XPath的元素,请尝试
# Note that it should start with the dot
String findSubInfo = "./following::div[@class='****']";
# use webElement.findElement instead of driver.findElement. No XPath concatenations needed
String temp2= "Info2: " + webElement.findElement(By.xpath(findSubInfo)).getText();