Java 如何使用XPath从以下子元素中提取文本
我只有很少的数据行,它们没有可跟踪的id或类,因此需要一个子/类XPath。 以下是HTML内容:Java 如何使用XPath从以下子元素中提取文本,java,selenium,xpath,webdriverwait,xpath-1.0,Java,Selenium,Xpath,Webdriverwait,Xpath 1.0,我只有很少的数据行,它们没有可跟踪的id或类,因此需要一个子/类XPath。 以下是HTML内容: 单位类型 CHDB F1080 使用下面的XPath。这将返回您要查找的两个元素 //span[text()='Unit type']/following::table[@role='presentation']//td//div[contains(@class,'v-label-undef-w')] 您需要诱导WebDriverWait和由()分类的所有元素的可见性,并使用getAttri
单位类型
CHDB
F1080
使用下面的XPath
。这将返回您要查找的两个元素
//span[text()='Unit type']/following::table[@role='presentation']//td//div[contains(@class,'v-label-undef-w')]
您需要诱导WebDriverWait
和由()分类的所有元素的可见性,并使用getAttribute
()获取innterText
或textContent
WebDriverWait wait = new WebDriverWait(driver, 30);
List<WebElement> elements = wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//span[text()='Unit type']/following::table[@role='presentation']//td//div[contains(@class,'v-label-undef-w')]")));
for(int i=0;i<elements.size();i++)
{
System.out.println(elements.get(i).getAttribute("innerText"));
System.out.println(elements.get(i).getAttribute("textContent"));
}
WebDriverWait wait=newwebdriverwait(驱动程序,30);
List elements=wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath(//span[text()='Unit type']/following::table[@role='presentation']//td//div[contains(@class,'v-label-unde-w'))));
对于(int i=0;i您可以尝试此xpath。此xpath使用“Unit type”来定位子元素(这是动态的)。此xpath将返回两个元素。因此您必须循环使用它来获取文本
//div[contains(text(),'Unit type')]/parent::td/following-sibling::td//tbody//td[contains(@class,'v-label')]
由于元素是动态元素,要提取文本,例如CHDB和F1080,您必须为ElementLocated()的可见性引入WebDriverWait,并且您可以使用以下任一项:
- 文本CHDB的xpath:
- 文本F1080的xpath:
您是否希望提取文本,例如CHDB
和F1080
?
System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//tr//span[text()='Unit type']//following::td[@class='v-formlayout-contentcell']/div[starts-with(@id, 'gwt-uid-') and starts-with(@aria-labelledby, 'gwt-uid-')]/div[@class='v-slot v-slot-hide-indicator']//div[@class="v-label v-widget tiny v-label-tiny smalllabel v-label-smalllabel v-label-undef-w"][@style]"))).getText());
System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//tr//span[text()='Unit type']//following::td[@class='v-formlayout-contentcell']/div[starts-with(@id, 'gwt-uid-') and starts-with(@aria-labelledby, 'gwt-uid-')]/div[@class='v-slot v-slot-hide-indicator']//div[@class="v-label v-widget tiny v-label-tiny smalllabel v-label-smalllabel v-label-undef-w" and not(@style)]"))).getText());