Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法读取<;p>;在<;h3>;使用硒_Java_Selenium_Selenium Webdriver_Automation_Selenium Firefoxdriver - Fatal编程技术网

Java 无法读取<;p>;在<;h3>;使用硒

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/以下同

我有一个类似以下内容的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/以下同级::p
以匹配所有3个段落


还要注意,XPath
h3['/p']
不起作用,因为它表示返回
h3
节点,即DOM根节点。谓词
['/p']
将始终返回True,因为非空字符串(
'/p'
在您的情况下)始终为True


还要注意,XPath
h3['/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