我正在使用下面的代碼來逐個點擊導航鏈接,直到它通過WebDriver到達盡頭,但它會拋出NullPointerException
,因爲我已經初始化並且仍然面臨此問題,請幫助。Selenium WebDriver中的空指針異常
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Exercise_dice {
static WebDriver driver;
public static void main(String[] args) {
WebDriver driver=new FirefoxDriver();
driver.get("http://www.dice.com");
driver.findElement(By.xpath("//*[@id='FREE_TEXT']")).sendKeys("selenium");
driver.findElement(By.xpath("//*[@id='searchSubmit']")).click();
String part1= "//*[@id='yui-main']/div/div[1]/div[1]/div[1]/a[";
String part2= "]";
int i=1;
while(isElementPresent(part1+i+part2)){
String text= driver.findElement(By.xpath(part1+i+part2)).getText();
System.out.println(text);
driver.findElement(By.xpath(part1+i+part2)).click();
i++;
}
}
public static boolean isElementPresent(String element_xpath){
int count=driver.findElements(By.xpath(element_xpath)).size();
if (count == 0)
return false;
else
return true;
}
}
附加堆棧跟蹤.. – xyz