无法使用selenium webdriver

时间:2018-05-17 13:04:29

标签: selenium selenium-webdriver xpath webdriver

未使用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无法找到此按钮,作为回报给出空指针异常。

1 个答案:

答案 0 :(得分:0)

要点击此<button>,文字为添加项目,因为 AUT 包含 Angular 元素,您必须诱导< em> WebDriverWait ,元素可点击,您可以使用以下代码行:

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