Java 无法读取<;p>;在<;h3>;使用硒
我有一个类似以下内容的html代码:Java 无法读取<;p>;在<;h3>;使用硒,java,selenium,selenium-webdriver,automation,selenium-firefoxdriver,Java,Selenium,Selenium Webdriver,Automation,Selenium Firefoxdriver,我有一个类似以下内容的html代码: <h3> Some Heading </h3> <p> Some String </p> <p> more string </p> <h3> Other heading</h3> <p> some text </p> 尝试了xpath类似于的“h3['/p']”但仍然没有成功。访问这些字符串的最佳方式是什么?尝试xpath//h3/以下同
<h3> Some Heading </h3>
<p> Some String </p>
<p> more string </p>
<h3> Other heading</h3>
<p> some text </p>
尝试了
xpath
类似于的“h3['/p']”
但仍然没有成功。访问这些字符串的最佳方式是什么?尝试xpath//h3/以下同级::p
以匹配所有3个段落
还要注意,XPathh3['/p']
不起作用,因为它表示返回h3
节点,即DOM根节点。谓词['/p']
将始终返回True,因为非空字符串('/p'
在您的情况下)始终为True
还要注意,XPathh3['/p']
不起作用,因为它表示返回h3
节点,即DOM根节点。谓词['/p']
将始终返回True,因为非空字符串('/p'
在您的情况下)始终为True要访问某些字符串,更多字符串和某些文本,可以使用以下定位器策略:
- 要访问文本为某些字符串的节点
By.xpath("//h3[normalize-space()='Some Heading']//following::p[1]")
- 使用文本作为更多字符串访问节点
By.xpath("//h3[normalize-space()='Some Heading']//following::p[2]")
- 使用文本作为某些文本访问节点
By.xpath("//h3[normalize-space()='Other heading']//following::p[1]")
- 找到这些元素后,可以使用
getAttribute(“innerHTML”)
方法提取节点中的文本
要访问一些字符串、更多字符串和一些文本,您可以使用以下定位器策略:
- 要访问文本为某些字符串的节点
By.xpath("//h3[normalize-space()='Some Heading']//following::p[1]")
- 使用文本作为更多字符串访问节点
By.xpath("//h3[normalize-space()='Some Heading']//following::p[2]")
- 使用文本作为某些文本访问节点
By.xpath("//h3[normalize-space()='Other heading']//following::p[1]")
- 找到这些元素后,可以使用
getAttribute(“innerHTML”)
方法提取节点中的文本
使用xpath//h3/following::p使用xpath//h3/following::p