Selenium findElements()/ findElement(按类)返回null

时间:2014-05-28 07:22:42

标签: java selenium intellij-idea

当我尝试使用webDriver搜索一个或多个元素时,它什么都不返回。 操作系统:Ubuntu 14.04,FrameWork:Intellij Idea,语言:Java。

我有这些陈述:

WebDriver myWebDriver = new FirefoxDriver();
String myClass = "hid";

然后转到:

myWebDriver.navigate().to("https://www.youtube.com/");

所有这些都有效。但是当我宣布:

 private List <WebElement> myElements = new ArrayList<WebElement>();

然后按原样访问myElements:

myElements = myWebDriver.findElements(By.className(myClass));

myElements为null,因为findElements似乎失败,以及findElement(webElement)(后者,我将List更改为常规WebElement)。这在以前有效。

以及所有代码:

package com.search.mywork;
        import org.junit.Test;
        import org.openqa.selenium.*;
        import org.openqa.selenium.chrome.ChromeDriver;
        import org.openqa.selenium.firefox.FirefoxDriver;
        import java.util.ArrayList;
        import java.util.List;
        import java.util.Set;
        /**
         * Created by aby on 5/27/14.
         */

        public class searchWork {
            private long time = 5000;
            private String experience = "1-2 שנים";

            private WebDriver myWebDriver ;
            private WebElement mywebelement ;

            private List <WebElement> jobList = new  ArrayList<WebElement>();
            private List <WebElement> basketList = new  ArrayList<WebElement>();
            private List <WebElement> experienceList = new  ArrayList<WebElement>();
            private List <WebElement> myElements = new  ArrayList<WebElement>();

            private int i=0;
            private int t=0;

            String myClass=  "hid" ;

            private String myLastJobClicked = null;
            private String myLastJobClickedSearch = null;

            private void logIn(){
                //Create Driver and log in
                WebDriver myWebDriver = new FirefoxDriver();
        //        myWebDriver.navigate().to(" http://www.drushim.co.il/Enter.aspx");
        //        myWebDriver.findElement(By.id("MainContent_Login_EmailText")).sendKeys("");
        //        myWebDriver.findElement(By.id("MainContent_Login_PasswordText")).sendKeys("");
        //        myWebDriver.findElement(By.id("MainContent_SubmitLogin")).click();
        //        myWebDriver.navigate().to("http://www.drushim.co.il/jobs/subcat/68/?area=1");
                myWebDriver.navigate().to("https://www.youtube.com/");

            }

            private void searchForWeb(){
                //Go to web Search
                myWebDriver.navigate().to("http://www.drushim.co.il/jobs/subcat/68/?area=1");

                //Wait if needed

                synchronized (myWebDriver){
                    try {
                        myWebDriver.wait(time);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            } //Func not used

            private Boolean chkExperienceIsOk(String experienceFromWeb){
                //Check if Experience is ok
        //        if(experience == myWebDriver.findElement(By.className("additionalText")).getText()){
        //            myWebDriver.findElement(By.className("addToBaseketLink")).click();
        //        }
                return experience == experienceFromWeb ? true : false;
            }

            private void findelements(){
                try {
                    //jobList = myWebDriver.findElements(By.className("yt-dialog-loading"));
                    //jobList = myWebDriver.findElements(By.className("yt-dialog-loading"));
                    //mywebelement = myWebDriver.findElement(By.className("yt-dialog-loading"));
                    myElements = myWebDriver.findElements(By.className(myClass));
                    int i =0;
                    //basketList = myWebDriver.findElements(By.className("addToBaseketLink"));
                    //experienceList =  myWebDriver.findElements(By.className("additionalText"));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            private void clickOnModaa(){

                for (WebElement jobElement : jobList){
                    jobElement.click();
                    for (WebElement basketElement : basketList){
                        for (WebElement experienceElement :experienceList)
                        if (chkExperienceIsOk(experienceElement.getText())){
                            basketElement.click();
                        }
                    }
                }
            }

            private void closeWebDriver(){
                myWebDriver.close();
            }

            @Test
            public void search (){

                logIn();
                //searchForWeb();
                findelements();


                }//end sear fucn

            }//end class

1 个答案:

答案 0 :(得分:1)

这是你的问题:

private void logIn(){
     //Create Driver and log in

    // myWebDriver is a Class field already
    // Remove WebDriver declaracion

    this.myWebDriver = new FirefoxDriver();

    myWebDriver.navigate().to("https://www.youtube.com/");
}

您正在创建一个新的WebDriver,因此当您在findElements函数中使用它时,它未正确初始化。

希望这有帮助!