Selenium Webdriver-Java、PageObject、PageFactory-

Selenium Webdriver-Java、PageObject、PageFactory-,java,selenium-webdriver,Java,Selenium Webdriver,我使用SeleniumWebDriver-Page对象模型和页面工厂实现自动化 问题:点击Submit后,控件保持在同一页上,不会加载下一页,因为我在下一页上检查的元素显示为代理元素 问题: 我的代码有错误吗? 我等待元素出现的时间不够长吗? 我是否在仪表板上使用了错误的定位器?--我最初使用div classname,但是firebug无法用它定位元素,所以切换到span(span.user name display),这对firebug很好 我在stackoverflow上发现了很多问题,并

我使用SeleniumWebDriver-Page对象模型和页面工厂实现自动化

问题:点击Submit后,控件保持在同一页上,不会加载下一页,因为我在下一页上检查的元素显示为代理元素

问题: 我的代码有错误吗? 我等待元素出现的时间不够长吗? 我是否在仪表板上使用了错误的定位器?--我最初使用div classname,但是firebug无法用它定位元素,所以切换到span(span.user name display),这对firebug很好

我在stackoverflow上发现了很多问题,并参考了许多关于page factory的教程和博客,我发现我的代码没有任何错误,但是一双新的眼睛可能会指出我遗漏的东西

我非常感谢任何关于如何解决这个问题的建议

LoginPage.java

package pages;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.CacheLookup;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;

public class LoginPage extends BasePage {

    private WebDriver driver;
    private WebDriverWait wait;

    public LoginPage(WebDriver driver) {
        this.driver = driver;
    }

    //ELEMENTS
    @FindBy(id = "user_session_user_name")
    @CacheLookup
    private WebElement usernameField;

    @FindBy(id = "user_session_password")
    @CacheLookup
    private WebElement passwordField;

    @FindBy(id = "user_session_submit")
    @CacheLookup
    private WebElement signInBtn;

    //METHODS

    public void provideUsername(String email) {
        System.out.println("Waiting for visibility of the element: " + usernameField + "...");
        explicitWait(driver, usernameField);
        System.out.println("Providing the username...");
        usernameField.sendKeys(email);
    }

    public void providePassword(String password) {
        System.out.println("Waiting for visibility of the element: " + passwordField + "...");
        explicitWait(driver, passwordField);
        System.out.println("Providing the password...");
        passwordField.sendKeys(password);
    }

    public UberDashboard clickSignInBtnExistingUser() {
        System.out.println("Clicking on Sign In button...");
        signInBtn.click();
        return PageFactory.initElements(driver, UberDashboard.class);
    }
}
UberDashboard.java

package pages;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.CacheLookup;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

public class UberDashboard extends BasePage {

    private WebDriver driver;

    public UberDashboard(WebDriver driver) {
        //PageFactory.initElements(driver, this);
        this.driver = driver;
    }

    // ELEMENTS

    @FindBy(css = "span.user-name-display")
    @CacheLookup
    private WebElement username;

    // METHODS
    public String getUsername() {
        System.out.println("Waiting for visibility of the element: " + username
                + "...");
        explicitWait(driver, username);
        System.out.println("Getting the username...");
        return username.getText();
    }
}
LoginTest.java

package tests;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.WebDriver.Navigation;
import org.openqa.selenium.WebDriver.Options;
import org.openqa.selenium.WebDriver.TargetLocator;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.PageFactory;

import pages.LoginPage;
import pages.UberDashboard;

public class LoginTest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        WebDriver driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get(URL); //points to login page
        String email = "abc@xyz.com";
        String password = "password";

        LoginPage loginPage = PageFactory.initElements(driver, LoginPage.class);

        loginPage.getWelcomeBackMsg();
        loginPage.provideUsername(email);
        loginPage.providePassword(password);

        UberDashboard uberDashboard = loginPage.clickSignInBtnExistingUser();

        uberDashboard.getUsername();

        driver.close();
        driver.quit();

    }

}
输出:

Waiting for visibility of the element: [[FirefoxDriver: firefox on MAC (f5b40b1b-9a77-5746-b294-82472cfbfda8)] -> css selector: #main-content-container div div h2]...
Getting the Welcome message...
Waiting for visibility of the element: [[FirefoxDriver: firefox on MAC (f5b40b1b-9a77-5746-b294-82472cfbfda8)] -> id: user_session_user_name]...
Providing the username...
Waiting for visibility of the element: [[FirefoxDriver: firefox on MAC (f5b40b1b-9a77-5746-b294-82472cfbfda8)] -> id: user_session_password]...
Providing the password...
Clicking on Sign In button...
Waiting for visibility of the element: Proxy element for: org.openqa.selenium.support.pagefactory.DefaultElementLocator@7205765b...
Exception in thread "main" org.openqa.selenium.TimeoutException: Timed out after 60 seconds waiting for visibility of Proxy element for: org.openqa.selenium.support.pagefactory.DefaultElementLocator@7205765b
Driver info: org.openqa.selenium.firefox.FirefoxDriver
    at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:80)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:229)
    at pages.BasePage.explicitWait(BasePage.java:25)
    at pages.UberDashboard.getUsername(UberDashboard.java:51)
    at tests.LoginTest.main(LoginTest.java:49)
Caused by: org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"css selector","selector":"span.user-name-display"}
Command duration or timeout: 10.72 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{applicationCacheEnabled=true, rotatable=false, handlesAlerts=true, databaseEnabled=true, version=42.0, platform=MAC, nativeEvents=false, acceptSslCerts=true, webStorageEnabled=true, locationContextEnabled=true, browserName=firefox, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
*** Element info: {Using=css selector, value=span.user-name-display}
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:595)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:348)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByCssSelector(RemoteWebDriver.java:437)
    at org.openqa.selenium.By$ByCssSelector.findElement(By.java:426)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:340)
    at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)
    at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38)
    at com.sun.proxy.$Proxy3.isDisplayed(Unknown Source)
    at org.openqa.selenium.support.ui.ExpectedConditions.elementIfVisible(ExpectedConditions.java:302)
    at org.openqa.selenium.support.ui.ExpectedConditions.access$100(ExpectedConditions.java:41)
    at org.openqa.selenium.support.ui.ExpectedConditions$10.apply(ExpectedConditions.java:288)
    at org.openqa.selenium.support.ui.ExpectedConditions$10.apply(ExpectedConditions.java:285)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:209)
    ... 3 more
Caused by: org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"css selector","selector":"span.user-name-display"}
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Driver info: driver.version: unknown
    at <anonymous class>.FirefoxDriver.prototype.findElementInternal_(file:///var/folders/wb/r1svxhyj6j50lf0301z84tcr0000gn/T/anonymous6146796425192466959webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js:10667)
    at <anonymous class>.fxdriver.Timer.prototype.setTimeout/<.notify(file:///var/folders/wb/r1svxhyj6j50lf0301z84tcr0000gn/T/anonymous6146796425192466959webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js:623)
等待元素的可见性:[[FirefoxDriver:MAC上的firefox(f5b40b1b-9a77-5746-b294-82472cfbfda8)]->css选择器:#主内容容器div h2]。。。
正在收到欢迎信息。。。
正在等待元素的可见性:[[FirefoxDriver:MAC上的firefox(f5b40b1b-9a77-5746-b294-82472cfbfda8)]->id:user\u session\u user\u name]。。。
正在提供用户名。。。
正在等待元素的可见性:[[FirefoxDriver:MAC上的firefox(f5b40b1b-9a77-5746-b294-82472cfbfda8)]->id:user\u session\u password]。。。
正在提供密码。。。
点击登录按钮。。。
正在等待元素的可见性:org.openqa.selenium.support.pagefactory的代理元素。DefaultElementLocator@7205765b...
线程“main”org.openqa.selenium.TimeoutException中的异常:在等待org.openqa.selenium.support.pagefactory的代理元素可见性60秒后超时。DefaultElementLocator@7205765b
驱动程序信息:org.openqa.selenium.firefox.FirefoxDriver
位于org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:80)
位于org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:229)
在pages.BasePage.explicitWait(BasePage.java:25)
位于pages.UberDashboard.getUsername(UberDashboard.java:51)
main(LoginTest.java:49)
原因:org.openqa.selenium.NoSuchElementException:无法定位元素:{“方法”:“css选择器”,“选择器”:“span.user name display”}
命令持续时间或超时:10.72秒
有关此错误的文档,请访问:http://seleniumhq.org/exceptions/no_such_element.html
驱动程序信息:org.openqa.selenium.firefox.FirefoxDriver
功能[{applicationCacheEnabled=true,rotatable=false,handlesAlerts=true,databaseEnabled=true,version=42.0,platform=MAC,nativeEvents=false,acceptSslCerts=true,WebStorage Enabled=true,locationContextEnabled=true,browserName=firefox,takesScreenshot=true,javascriptEnabled=true,CSSSelectorEnabled=true}]
***元素信息:{Using=css选择器,value=span.user name display}
位于sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)
位于sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
在sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
位于java.lang.reflect.Constructor.newInstance(Constructor.java:422)
位于org.openqa.selenium.remote.ErrorHandler.CreateTrowable(ErrorHandler.java:206)
位于org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
位于org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:595)
位于org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:348)
位于org.openqa.selenium.remote.RemoteWebDriver.findElementByCssSelector(RemoteWebDriver.java:437)
位于org.openqa.selenium.By$ByCssSelector.findelelement(By.java:426)
位于org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:340)
位于org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)
位于org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38)
在com.sun.proxy.$Proxy3.isDisplayed上显示(未知源)
位于org.openqa.selenium.support.ui.ExpectedConditions.elementIfVisible(ExpectedConditions.java:302)
位于org.openqa.selenium.support.ui.ExpectedConditions.access$100(ExpectedConditions.java:41)
位于org.openqa.selenium.support.ui.ExpectedConditions$10.apply(ExpectedConditions.java:288)
位于org.openqa.selenium.support.ui.ExpectedConditions$10.apply(ExpectedConditions.java:285)
位于org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:209)
... 3个以上
原因:org.openqa.selenium.NoSuchElementException:无法定位元素:{“方法”:“css选择器”,“选择器”:“span.user name display”}
有关此错误的文档,请访问:http://seleniumhq.org/exceptions/no_such_element.html
驱动程序信息:驱动程序。版本:未知
at.FirefoxDriver.prototype.findElementInternal_(file:///var/folders/wb/r1svxhyj6j50lf0301z84tcr0000gn/T/anonymous6146796425192466959webdriver-profile/extensions/fxdriver@googlecode.com/components/driver component.js:10667)

在.fxdriver.Timer.prototype.setTimeout/我可以看到您的目标站点是Uber。但这个
URL
指向的确切位置?driver.get(URL)中的URL指向的是登录页面。您可以直接在这里共享它吗。可能是我看错了页面。很抱歉,我不能在这里共享URL,但是,输入用户名、密码和单击“工作正常”的脚本。但单击后,控件仍停留在同一页面上。发布仪表板页面的html代码(包含用户名)和方法
明确显示(驱动程序、元素)
将帮助人们调查问题。我可以看到,您的目标站点是优步。但是这个
URL
指向的确切位置是什么呢?driver.get(URL)中的URL是p