当我尝试使用webdriver搜索一个或多个元素时,它不会返回任何内容。
操作系统:ubuntu 14.04,框架: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 = myWebDriver.findElements(By.className(myClass));
myelements是空的,因为findelements似乎失败了,还有findelement(webelement)(对于后者,我将列表更改为常规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
最佳答案
这是你的问题:
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
函数中使用它时,它没有正确初始化。希望这有帮助!