gpt4 book ai didi

java - Selenium findElements()/findElement(by class) 返回 null

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:04:19 26 4
gpt4 key购买 nike

当我尝试使用 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:

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 函数中使用它时,它没有正确初始化。

希望这对您有所帮助!

关于java - Selenium findElements()/findElement(by class) 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23905162/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com