在Selenium Java上找到一个元素

时间:2017-07-21 15:39:25

标签: java selenium

我一直在寻找这个问题的解决方案。 我无法获得用户电子邮件字段的元素。我试过等待元素,页面工厂和其他解决方案,但没有任何对我有用,Selenium仍然抛出两个错误:

 the element is not visible OR stable element problem.

如果有人可以帮助我,我认为更容易直接尝试original page

非常感谢你。

CLASSES:

public class LoginPageWithFactory {
        WebDriver driver;

        @FindBy(xpath= "//input[@type='email']")
        WebElement userNameField;

 @FindBy(xpath = "//input[@type='password']")
    WebElement passwordField;

    @FindBy(id = "btn-login")
    WebElement loginButton;

    public LoginPageWithFactory(WebDriver pDriver) {
        this.driver = pDriver;
        AjaxElementLocatorFactory factory = new AjaxElementLocatorFactory(driver,100);
        PageFactory.initElements(factory, this);
        //PageFactory.initElements(driver, this);
    }
    public void writeUserName(String username) {
        userNameField.sendKeys(username);
    }
    public void writePassword(String password){
        passwordField.sendKeys(password);
    }
    public void clickOnLogin(){
        loginButton.click();
    }
}

public class LoginTestCases {
    WebDriver webDriver;
    LandingPage landingPage;
    LoginPageWithFactory loginPage;

    public LoginTestCases(){
        webDriver = new StartDriver("http://tmpangular.theamalgama.com").getWebDriver();
        landingPage = new LandingPage(webDriver);
    }
    @Test
    public void successfulLogin(){
        //WebDriverWait wait = new WebDriverWait(webDriver, 10);
        landingPage.goToLoginPage();
        loginPage = new LoginPageWithFactory(webDriver);
        loginPage.writeUserName("julieta@theamalgama.com");
        loginPage.writePassword("sinclair");
        loginPage.clickOnLogin();
    }
}

public class LandingPage {
    WebDriver driver;
    By loginButton = By.id("btn-header-login");
    By registerButton = By.id("btn-get-started");

    public LandingPage(WebDriver mdriver){
        this.driver = mdriver;
    }

    public void goToLoginPage(){
        driver.findElement(loginButton).click();
    }

    public void goToCreateNewAccount(){
        driver.findElement(registerButton).click();
    }
}

public class StartDriver {

    WebDriver webDriver;

    public StartDriver(String url){
        System.setProperty("webdriver.chrome.driver","/home/julieta/Apps/IntelliJ Idea/chromedriver");
        webDriver = new ChromeDriver();
        webDriver.navigate().to(url);
        webDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        webDriver.manage().window().maximize();
    }

    public WebDriver getWebDriver() {
        return webDriver;
    }
}

ERROR

/usr/lib/jvm/java-8-oracle/bin/java -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:/home/julieta/Apps/IntelliJ Idea/lib/idea_rt.jar=34246:/home/julieta/Apps/IntelliJ Idea/bin" -Dfile.encoding=UTF-8 -classpath "/home/julieta/Apps/IntelliJ Idea/lib/idea_rt.jar:/home/julieta/Apps/IntelliJ Idea/plugins/junit/lib/junit-rt.jar:/home/julieta/Apps/IntelliJ Idea/plugins/junit/lib/junit5-rt.jar:/usr/lib/jvm/java-8-oracle/jre/lib/charsets.jar:/usr/lib/jvm/java-8-oracle/jre/lib/deploy.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/cldrdata.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/dnsns.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/jaccess.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/jfxrt.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/localedata.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/nashorn.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/sunec.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/sunjce_provider.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/sunpkcs11.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/zipfs.jar:/usr/lib/jvm/java-8-oracle/jre/lib/javaws.jar:/usr/lib/jvm/java-8-oracle/jre/lib/jce.jar:/usr/lib/jvm/java-8-oracle/jre/lib/jfr.jar:/usr/lib/jvm/java-8-oracle/jre/lib/jfxswt.jar:/usr/lib/jvm/java-8-oracle/jre/lib/jsse.jar:/usr/lib/jvm/java-8-oracle/jre/lib/management-agent.jar:/usr/lib/jvm/java-8-oracle/jre/lib/plugin.jar:/usr/lib/jvm/java-8-oracle/jre/lib/resources.jar:/usr/lib/jvm/java-8-oracle/jre/lib/rt.jar:/home/julieta/workspace/tmp Automated Test/out/production/tmp Automated Test:/home/julieta/Documents/librerias selenium appium/selenium/selenium-java-3.4.0/client-combined-3.4.0-nodeps.jar:/home/julieta/Documents/librerias selenium appium/selenium/selenium-java-3.4.0/lib/sac-1.3.jar:/home/julieta/Documents/librerias selenium appium/selenium/selenium-java-3.4.0/lib/jna-4.1.0.jar:/home/julieta/Documents/librerias selenium appium/selenium/selenium-java-3.4.0/lib/gson-2.8.0.jar:/home/julieta/Documents/librerias selenium appium/selenium/selenium-java-3.4.0/lib/guava-21.0.jar:/home/julieta/Documents/librerias selenium appium/selenium/selenium-java-3.4.0/lib/junit-4.12.jar:/home/julieta/Documents/librerias selenium appium/selenium/selenium-java-3.4.0/lib/xalan-2.7.2.jar:/home/julieta/Documents/librerias selenium appium/selenium/selenium-java-3.4.0/lib/htmlunit-2.26.jar:/home/julieta/Documents/librerias selenium appium/selenium/selenium-java-3.4.0/lib/commons-io-2.5.jar:/home/julieta/Documents/librerias selenium appium/selenium/selenium-java-3.4.0/lib/httpcore-4.4.6.jar:/home/julieta/Documents/librerias selenium appium/selenium/selenium-java-3.4.0/lib/httpmime-4.5.3.jar:/home/julieta/Documents/librerias selenium appium/selenium/selenium-java-3.4.0/lib/xml-apis-1.4.01.jar:/home/julieta/Documents/librerias selenium appium/selenium/selenium-java-3.4.0/lib/commons-exec-1.3.jar:/home/julieta/Documents/librerias selenium appium/selenium/selenium-java-3.4.0/lib/cssparser-0.9.22.jar:/home/julieta/Documents/librerias selenium appium/selenium/selenium-java-3.4.0/lib/httpclient-4.5.3.jar:/home/julieta/Documents/librerias selenium appium/selenium/selenium-java-3.4.0/lib/serializer-2.7.2.jar:/home/julieta/Documents/librerias selenium appium/selenium/selenium-java-3.4.0/lib/cglib-nodep-3.2.4.jar:/home/julieta/Documents/librerias selenium appium/selenium/selenium-java-3.4.0/lib/commons-lang3-3.5.jar:/home/julieta/Documents/librerias selenium appium/selenium/selenium-java-3.4.0/lib/hamcrest-core-1.3.jar:/home/julieta/Documents/librerias selenium appium/selenium/selenium-java-3.4.0/lib/xercesImpl-2.11.0.jar:/home/julieta/Documents/librerias selenium appium/selenium/selenium-java-3.4.0/lib/commons-codec-1.10.jar:/home/julieta/Documents/librerias selenium appium/selenium/selenium-java-3.4.0/lib/jna-platform-4.1.0.jar:/home/julieta/Documents/librerias selenium appium/selenium/selenium-java-3.4.0/lib/neko-htmlunit-2.25.jar:/home/julieta/Documents/librerias selenium appium/selenium/selenium-java-3.4.0/lib/commons-logging-1.2.jar:/home/julieta/Documents/librerias selenium appium/selenium/selenium-java-3.4.0/lib/htmlunit-driver-2.26.jar:/home/julieta/Documents/librerias selenium appium/selenium/selenium-java-3.4.0/lib/htmlunit-core-js-2.26.jar:/home/julieta/Documents/librerias selenium appium/selenium/selenium-java-3.4.0/lib/phantomjsdriver-1.4.0.jar:/home/julieta/Documents/librerias selenium appium/selenium/selenium-java-3.4.0/lib/javax.servlet-api-3.1.0.jar:/home/julieta/Documents/librerias selenium appium/selenium/selenium-java-3.4.0/lib/jetty-io-9.4.1.v20170120.jar:/home/julieta/Documents/librerias selenium appium/selenium/selenium-java-3.4.0/lib/jetty-util-9.4.1.v20170120.jar:/home/julieta/Documents/librerias selenium appium/selenium/selenium-java-3.4.0/lib/websocket-api-9.4.3.v20170317.jar:/home/julieta/Documents/librerias selenium appium/selenium/selenium-java-3.4.0/lib/websocket-client-9.4.3.v20170317.jar:/home/julieta/Documents/librerias selenium appium/selenium/selenium-java-3.4.0/lib/websocket-common-9.4.3.v20170317.jar:/home/julieta/Documents/librerias selenium appium/selenide-4.5.1.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 Test.LoginTestCases

在端口9472上启动ChromeDriver 2.30.477691(6ee44a7247c639c0703f291d320bdf05c1531b57) 仅允许本地连接。 2017年7月21日下午1:51:10 org.openqa.selenium.remote.ProtocolHandshake createSession 信息:检测到的方言:OSS

org.openqa.selenium.ElementNotVisibleException:元素不可见   (会议信息:chrome = 58.0.3029.110)   (驱动程序信息:chromedriver = 2.30.477691(6ee44a7247c639c0703f291d320bdf05c1531b57),platform = Linux 4.4.0-79-generic x86_64)(警告:服务器未提供任何堆栈跟踪信息) 命令持续时间或超时:10.32秒 构建信息:版本:'未知',修订版:'未知',时间:'未知' 系统信息:主机:'julieta-comp',ip:'127.0.1.1',os.name:'Linux',os.arch:'amd64',os.version:'4.4.0-79-generic',java .version:'1.8.0_131' 驱动程序信息:org.openqa.selenium.chrome.ChromeDriver Capabilities [{applicationCacheEnabled = false,rotate = false,mobileEmulationEnabled = false,networkConnectionEnabled = false,chrome = {chromedriverVersion = 2.30.477691(6ee44a7247c639c0703f291d320bdf05c1531b57),userDataDir = / tmp / .org.chromium.Chromium.lDUW7C},takesHeapSnapshot = true, pageLoadStrategy = normal,databaseEnabled = false,handlesAhell = true,version = 58.0.3029.110,platform = LINUX,browserConnectionEnabled = false,nativeEvents = true,acceptSslCerts = true,locationContextEnabled = true,webStorageEnabled = true,browserName = chrome, takesScreenshot = true,javascriptEnabled = true,cssSelectorsEnabled = true,unexpectedAlertBehaviour =}] 会议ID:420607562e9c49a3d9a03809a02731a6

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:423)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:215)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:167)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:671)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:272)
at org.openqa.selenium.remote.RemoteWebElement.sendKeys(RemoteWebElement.java:96)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:51)
at com.sun.proxy.$Proxy5.sendKeys(Unknown Source)
at PageObject.LoginPageWithFactory.writeUserName(LoginPageWithFactory.java:28)
at Test.LoginTestCases.successfulLogin(LoginTestCases.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

处理完成,退出代码为255

2 个答案:

答案 0 :(得分:0)

这可能是各种各样的事情。

  1. 元素不在您的视口中
  2. 你可以这样解决:

    driver.maximize_window()
    
    1. 尝试通过xpath搜索
    2. 有时,您尝试搜索同一属性下的多个元素,而您只是使用了错误的元素。您可以通过谷歌浏览器或任何其他网络浏览器上的检查元素获取xpath。

      1. 很可能元素尚未加载
      2. 您可以使用隐式等待来确保元素已加载:

        Driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
        
          

        隐式等待是告诉WebDriver在尝试查找一个或多个元素(如果它们不是立即可用)时轮询DOM一段时间。默认设置为0.设置后,将为WebDriver对象实例的生命周期设置隐式等待。

答案 1 :(得分:0)

以下是您的问题的答案:

LoginPageWithFactory班级中,要识别Email字段,您提供的xpath//input[@type='email']不是唯一的。您可以考虑以下代码行来标识Email字段:

@FindBy(xpath= "//form[@id='log-in-form']/input[@type='email']")

如果这回答你的问题,请告诉我。