Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.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 Webdriver_Xpath_Css Selectors_Webdriverwait - Fatal编程技术网

Java 如何使用xpath获取以下文本名?

Java 如何使用xpath获取以下文本名?,java,selenium-webdriver,xpath,css-selectors,webdriverwait,Java,Selenium Webdriver,Xpath,Css Selectors,Webdriverwait,我尝试使用以下xpath driver.findElement(By.xpath("//div[contains(@class,'ReactTags__selected')]//span[1]/text()")); 但得到以下错误 “org.openqa.selenium.InvalidSelectorException:无效选择器:xpath表达式的结果”//div[contains(@class,'reactags\uu selected')]///span[2]/text()”是:[对象

我尝试使用以下xpath

driver.findElement(By.xpath("//div[contains(@class,'ReactTags__selected')]//span[1]/text()"));
但得到以下错误

“org.openqa.selenium.InvalidSelectorException:无效选择器:xpath表达式的结果”//div[contains(@class,'reactags\uu selected')]///span[2]/text()”是:[对象文本]。它应该是一个元素。”


“测试名称”
×

请尝试下面的Xpath

希望能有帮助

//div[contains(@class,'ReactTags__selected')]//span[2]/a/text()
xpath返回文本节点中的
text()
,Selenium不支持该节点。找到元素并使用
getText()
获取文本

WebElement element = driver.findElement(By.xpath("//div[contains(@class,'ReactTags__selected')]//span[1]"));
String text = element.getText();

由于父元素包含属性
draggable=“true”
始终是一个动态元素,确切地说是一个动态元素。此外,由于文本TestName是一个文本节点,您需要为
元素定位()的可见性引入WebDriverWait,并且您可以使用以下任一选项:

  • css选择器

    System.out.println(((JavascriptExecutor)driver).executeScript('return arguments[0].firstChild.textContent;', new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.ReactTags__selected > span.tag-wrapper.ReactTags__tag")))).toString());
    
  • xpath

    System.out.println((String)((JavaScriptExecutor)driver).executeScript("return arguments[0].firstChild.textContent;", new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='ReactTags__selected']/span[@class='tag-wrapper ReactTags__tag']")))));
    
html中没有两个
标记。Selenium不支持
text()。
System.out.println((String)((JavaScriptExecutor)driver).executeScript("return arguments[0].firstChild.textContent;", new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='ReactTags__selected']/span[@class='tag-wrapper ReactTags__tag']")))));