我正在尝试等待一段时间,但在加载页面之前会抛出异常。请告诉我如何在页面中明确地等待。 场景是 - 当主页加载时,加载程序加载5-6秒。在这里,我想等到装载机没有禁用。
基类 - >
package BasePOI;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Setup {
public static WebDriver driver;
By counselor=By.id("counselor_login");
public void Websiteopen()
{
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("url");
}
public Setup(WebDriver driver)
{
this.driver=driver;
}
public Setup()
{
}
public WebDriver getDriver()
{
return this.driver;
}
public void click_Login_counselor()
{
driver.findElement(counselor).click();
}
}
LoginPOI类--->
package BasePOI;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class LoginPOI {
public WebDriver driver;
// home
By home_login = By.linkText("Login");
By about_us = By.linkText("About Us");
// login
By counselor = By.id("counselor_login");
By user = By.id("user_login");
By username = By.id("username");
By password = By.id("password");
By Login = By.name("Login");
By create_account = By.name("Login");
By Logout = By.linkText("Logout");
By Loader=By.id("loader");
public void click_Login_button() {
this.driver = new Setup().getDriver();
//driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
driver.findElement(home_login).click();
}
public void click_Login_counselor() {
driver.findElement(counselor).click();
}
public void click_Login_user() {
driver.findElement(user).click();
}
public void Enter_login_data(String uname, String pwd) {
driver.findElement(username).clear();
driver.findElement(username).sendKeys(uname);
driver.findElement(password).clear();
driver.findElement(password).sendKeys(pwd);
}
public void click_Login() {
driver.findElement(Login).click();
}
public void waitforLogin(){
System.out.println("a");
WebDriverWait wait=new WebDriverWait(driver,15);
WebElement b=wait.until(ExpectedConditions.visibilityOfElementLocated(Login));
System.out.println(b);
}
}
登录类 - >
package Functionlity;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;
import BasePOI.LoginPOI;
import BasePOI.Setup;
public class Login
{
public WebDriver driver;
@Test
public void openwebsite() throws InterruptedException {
Setup a = new Setup(driver);
LoginPOI b = new LoginPOI();
a.Websiteopen();
b.waitforLogin();
b.click_Login_button();
b.click_Login_user();
a.click_Login_counselor();
}
}
误差--->
java.lang.NullPointerException
at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:212)
at org.openqa.selenium.support.ui.FluentWait.<init>(FluentWait.java:102)
at org.openqa.selenium.support.ui.WebDriverWait.<init>(WebDriverWait.java:71)
at org.openqa.selenium.support.ui.WebDriverWait.<init>(WebDriverWait.java:45)
at BasePOI.LoginPOI.waitforLogin(LoginPOI.java:58)
at Functionlity.Login.openwebsite(Login.java:20)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:643)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:820)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1128)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
at org.testng.TestRunner.privateRun(TestRunner.java:782)
at org.testng.TestRunner.run(TestRunner.java:632)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
at org.testng.SuiteRunner.run(SuiteRunner.java:268)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
at org.testng.TestNG.run(TestNG.java:1064)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:113)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:206)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:177)
执行前点击登录按钮,我想等到页面加载器没有禁用
答案 0 :(得分:1)
尝试下面的代码,等到&#34; Pageloader&#34;消失:
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.presenceOfElementLocated(By.id('loader')));
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id('loader')));