Selenium webdriver 如何从<;Strong>;使用xpath标记?

Selenium webdriver 如何从<;Strong>;使用xpath标记?,selenium-webdriver,webdriver,Selenium Webdriver,Webdriver,这是我的HTML代码 <p> <strong>Telephone:</strong> 1(111)656-3485 <br/> 电话: 1(111)656-3485 Selenium具有从WebElements获取文本的函数,您不需要在xpath中提取文本。使用 driver.findElement(By.xpath("//strong")).getText(); 您可以使用XPath获取,请查找以下代码: WebElement eleT

这是我的HTML代码

<p>
<strong>Telephone:</strong>
 1(111)656-3485
<br/>

电话:
1(111)656-3485


Selenium具有从WebElements获取文本的函数,您不需要在xpath中提取文本。使用

driver.findElement(By.xpath("//strong")).getText();

您可以使用XPath获取,请查找以下代码:

WebElement eleText=driver.findElement(By.xpath("//strong[contains(text(),'Telephone:')]"));

String textTelephone=eleText.getText();
System.out.println(textTelephone);
您可以使用另一个类似的xpath

WebElement eleText=driver.findElement(By.xpath("//*[contains(text(),'Telephone:')]"));

String textTelephone=eleText.getText();
System.out.println(textTelephone);

我的HTML代码是电话:1(111)656-3485
我尝试过使用此xpath,但在执行脚本时出错,因为xpath驱动程序.findElement(By.xpath(“//strong/following sibling::text()”)中有text()方法;请参阅:SO的期望是,用户提问时不仅要进行研究以回答自己的问题,还要分享研究、代码尝试和结果。这表明你花了时间来帮助自己,它使我们避免重复显而易见的答案,最重要的是,它帮助你得到一个更具体和相关的答案!另见: