【问题标题】:Unable to locate an element within a table using selenium webdriver无法使用 selenium webdriver 在表中定位元素
【发布时间】:2018-05-17 13:04:28
【问题描述】:

没有使用 selenium webdriver 加载表格内容。它只显示表格的标题而不显示内容。

我已经在我的应用程序中成功登录,但在主屏幕上有一个列出某个项目的表格,而使用 selenium 时它不会加载表格中的内容。

硒罐: 2.53
Mozilla Firefox 版本: 46.0

同样使用谷歌浏览器,但它也不起作用。

当我关闭浏览器(打开了哪个硒)并手动重新打开它时,它工作正常。但是当 selenium 打开它并转到主页时,表数据不会加载。

 <div class="project-list-bg" _ngcontent-c1="">
<div class="col s12 project-container" _ngcontent-c1="">
<main _ngcontent-c1="">
<div class="col s12 search-block" _ngcontent-c1="">
<div class="search-wrapper" _ngcontent-c1="">
<label _ngcontent-c1="">Search: </label>
<div class="card" _ngcontent-c1="">
<input class="ng-untouched ng-pristine ng-valid" type="text" _ngcontent-c1="" name="search">
</div>
</div>
</div>
<table id="myTable" class="bordered centered " _ngcontent-c1="">
<thead _ngcontent-c1="">
<tr _ngcontent-c1="">
<th class="grey-text text-darken-1" width="10%" _ngcontent-c1="">
Project ID
<i class="material-icons" _ngcontent-c1="">keyboard_arrow_down</i>
</th>
<th class="grey-text text-darken-1" width="17%" _ngcontent-c1="">
Project Name
<i class="material-icons" _ngcontent-c1="">keyboard_arrow_down</i>
</th>
<th class="grey-text text-darken-1" _ngcontent-c1="" style="width: 12%;">
Date Completed
<i class="material-icons" _ngcontent-c1="">keyboard_arrow_down</i>
</th>
<th class="grey-text text-darken-1" width="10%" _ngcontent-c1="" style="background-image: none; ">
# of Runs
<i class="material-icons" _ngcontent-c1="">keyboard_arrow_down</i>
</th>
<th class="grey-text text-darken-1" width="10%" _ngcontent-c1="" style="background-image: none; ">
Status
<i class="material-icons" _ngcontent-c1="">keyboard_arrow_down</i>
</th>
<th class="grey-text text-darken-1" width="16%" _ngcontent-c1="" style="background-image: none; ">
Project Description
<i class="material-icons" _ngcontent-c1="">keyboard_arrow_down</i>
</th>
<th width="12%" _ngcontent-c1="" style="background-image: none; "></th>
</tr>
</thead>
<tbody _ngcontent-c1="">
</table>
<div class="pagination right-align" _ngcontent-c1="">
<pagination-controls _ngcontent-c1="">
<pagination-template>
<ul class="ngx-pagination" role="navigation" aria-label="Pagination">
</pagination-template>
</pagination-controls>
</div>
<div class="row" _ngcontent-c1="">
<div class="col s6 add-btn2 left-align" _ngcontent-c1="">
<a _ngcontent-c1="" routerlink="/project-information" href="/project-information">
<button class="btn next-btn p-btn" _ngcontent-c1=""> Add Project</button>
</a>
</div>
<div class="col s6 right-align" _ngcontent-c1="">
<ul id="myPager" class="pagination pager" _ngcontent-c1=""></ul>
</div>
</div>
</main>
</div>
</div>

------------硒代码------------------ -

package projectListingScreen;

import org.testng.annotations.Test;

import ObjectRepository.HomescreenObject;
import ObjectRepository.PageObject;
import loginScreen.LoginPage;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeTest;

public class HomeScreen extends LoginPage {

    public WebDriver driver;

  @Test
  public void f() {

         WebDriverWait wait=new WebDriverWait(driver, 20);
          WebElement addprojectbutton;
          addprojectbutton = wait.until(ExpectedConditions.elementToBeClickable(By.xpath( "html/body/app-root/app-home/div/div/main/div[3]/div[1]/a/button")));
          addprojectbutton.click();

  }

我必须单击此&lt;button&gt; 元素,但如果表格(您可以在 cmets 中找到的表格的 HTML)未正确加载,则此按钮向上移动,selenium 无法找到此按钮,作为回报它给出 null指针异常。

【问题讨论】:

  • 请显示您的表格的 HTML
  • 分享相关的 HTML。
  • 我必须点击这个按钮但是如果上面的表格没有正确加载然后这个按钮向上移动,selenium 无法找到这个按钮,作为回报它给出了空指针异常。
  • @Abhishek 使用此信息编辑主要问题,而不是 cmets。
  • 项目ID keyboard_arrow_down 项目名称keyboard_arrow_down

标签: selenium selenium-webdriver xpath webdriver


【解决方案1】:

要点击这个&lt;button&gt;,文本为添加项目,因为AUT包含Angular元素,你必须诱导 WebDriverWait 元素可点击,您可以使用以下代码行:

new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@class='btn next-btn p-btn'][normalize-space()='Add Project']"))).click();

【讨论】:

  • 这正是我之前尝试做的,但没有成功... WebDriverWait wait=new WebDriverWait(driver, 20); WebElement 添加项目按钮; addprojectbutton = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("html/body/app-root/app-home/div/div/main/div[3]/div[1]/a/button"))) ; addprojectbutton.click();
  • @Abhishek didn't work 没有说明发生了什么错误。使用您当前的 代码试用错误堆栈跟踪 更新问题
  • @Abhishek 这不完全是我提供的解决方案。复制粘贴确切的代码(解决方案)并向我们(或问题)更新状态。
  • 你要我分享 selenium 代码或完整的 HTML..??
  • 使用您的实际代码试用版编辑和更新主要问题
猜你喜欢
  • 1970-01-01
  • 2016-04-15
  • 1970-01-01
  • 1970-01-01
  • 2021-04-12
  • 1970-01-01
  • 2016-12-10
  • 2018-09-30
  • 1970-01-01
相关资源
最近更新 更多