无法使用selenium webdriver在表中找到元素

问题描述 投票:0回答:1

未使用selenium webdriver加载表内容。它仅显示表格的标题,但不显示内容。

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

硒罐:2.53 Mozilla Firefox版本:46.0

使用谷歌浏览器浏览器相同,但它也没有用。

当我关闭浏览器(打开了哪个selenium)并手动重新打开它时,它可以正常工作。但是当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>

------------ Selenium Code ----------------------------------

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();

  }

我必须单击此<button>元素,但如果表(您可以在注释中找到的表的HTML)无法正确加载,则此按钮向上移动,selenium无法找到此按钮,作为回报,它会给出空指针异常。

selenium selenium-webdriver xpath webdriver
1个回答
0
投票

要点击这个<button>,文本为Add Project,因为AUT包含Angular元素,你必须引导WebDriverWait才能使元素可点击,你可以使用以下代码行:

new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@class='btn next-btn p-btn'][normalize-space()='Add Project']"))).click();
© www.soinside.com 2019 - 2024. All rights reserved.