Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/78.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/1/visual-studio-2008/2.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 试图使用driver.findElement(By.xpath()从div检索文本时出现的问题_Java_Html_Selenium_Selenium Webdriver - Fatal编程技术网

Java 试图使用driver.findElement(By.xpath()从div检索文本时出现的问题

Java 试图使用driver.findElement(By.xpath()从div检索文本时出现的问题,java,html,selenium,selenium-webdriver,Java,Html,Selenium,Selenium Webdriver,我正在尝试验证页面上的一些内容,并且有一个带有标题列的网格。我想从这些内容中获取文本,这是验证的一部分。出于某种原因,它返回为空,我不明白为什么要使用包含文本来查找元素 String expectedName = "Employee Name"; String actualName = driver.findElement(By.xpath("//div[contains(text(), 'Employee Name')]")).getText(); Assert.

我正在尝试验证页面上的一些内容,并且有一个带有标题列的网格。我想从这些内容中获取文本,这是验证的一部分。出于某种原因,它返回为空,我不明白为什么要使用包含文本来查找元素

     String expectedName = "Employee Name";
     String actualName = driver.findElement(By.xpath("//div[contains(text(), 'Employee Name')]")).getText();
     Assert.assertEquals(expectedName, actualName);
我得到的错误是:
java.lang.AssertionError:应该是[Employee Name],但找到了[]

HTML:

<div class="dx-datagrid-text-content dx-text-content-alignment-left dx-header-filter-indicator" role="presentation">Employee Name</div>
员工姓名

您似乎很接近。要提取
元素的文本内容,理想情况下,您需要为
元素的可见性引入WebDriverWait()
,您可以使用以下任一选项:

  • css选择器

    String expectedName = "Employee Name";
    String actualName = new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.dx-datagrid-text-content.dx-text-content-alignment-left.dx-header-filter-indicator"))).getText();
    Assert.assertEquals(expectedName, actualName);
    
  • xpath

    String expectedName = "Employee Name";
    String actualName = new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='dx-datagrid-text-content dx-text-content-alignment-left dx-header-filter-indicator']"))).getText();
    Assert.assertEquals(expectedName, actualName);
    

这似乎不是一个好的解决方案,因为不需要等待元素可见。如果元素还不可见,则使用
String actualName=driver.findElement(By.xpath(//div[contains(text(),'Employee Name'))))。getText();
会抛出一个错误,不是吗?嗯,您是通过文本Employee Name查找元素,然后使用
getText()
提取相同的文本Employee Name?您能告诉我们有关您的用例的更多信息吗?您所说的“用例”是什么意思这是一个健全的检查,以确保屏幕按应有的方式加载并填充数据。我想我应该首先确认一些列标题是可见的,然后检查预期的数据。这就是你要找的吗?我不在乎如何找到元素,包含文本似乎是最干净的方式。