我被困在一端,我无法获得文本值。
为了更好的看法:-
<div class="">
<span class="address_city">Glenwood</span>
<span class="address_state">GA</span>
<span class="address_zip xh-highlight">30428</span>
</div>
我使用以下xpath标识类
address_zip...
://*[contains(text(),'30428')]
如何获得GA和Glenwood的文字值?
最佳答案
您可以使用preceding-sibling
//*[contains(text(),'30428')]/preceding-sibling::span[@class='address_city']
//*[contains(text(),'30428')]/preceding-sibling::span[@class='address_state']
您也可以找到邮政编码元素并使用它
WebElement zip = driver.findElement(By.xpath("//*[contains(text(),'30428')]"));
String city = zip.findElement(By.xpath("//preceding-sibling::span[@class='address_city']")).getText();
String state = zip.findElement(By.xpath("//preceding-sibling::span[@class='address_state']")).getText();
关于html - 使用xpath的Selenium-如何使用同级元素文本获取元素文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42219710/