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 如何使用XPath从以下子元素中提取文本_Java_Selenium_Xpath_Webdriverwait_Xpath 1.0 - Fatal编程技术网

Java 如何使用XPath从以下子元素中提取文本

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

我只有很少的数据行,它们没有可跟踪的id或类,因此需要一个子/类XPath。 以下是HTML内容:


单位类型
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')]

由于元素是动态元素,要提取文本,例如CHDBF1080,您必须为
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());