package PageFactory;
import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.By;
public class Add{
public static WebDriver driver;
public static void main (String args[]){
try
{
WebElement popup=driver.findElement(By.xpath("/html/body/div/div[2]/a[1]"));
WebElement name=driver.findElement(By.xpath("//*[@id='name']"));
WebElement submit=driver.findElement(By.xpath("//*[@id='submit']"));
WebElementproceed=driver.findElement(By.xpath("/html/body/div/div[2]/a[2]"));
System.setProperty("webdriver.chrome.driver","E:\\Chrome\\chromedriver.exe");
driver=new ChromeDriver();
driver.navigate().to("testurl");
popup.click();
String oldTab = driver.getWindowHandle();
ArrayList<String> newTab = new ArrayList<String>(driver.getWindowHandles());
newTab.remove(oldTab);
driver.switchTo().window(newTab.get(0));
name.sendKeys("neha");
submit.click();
driver.close();
driver.switchTo().window(oldTab);
proceed.click();
}catch (Exception e) {
e.printStackTrace();
}
}
}
我正在尝试使用硒webdriver
我为解决这个问题做了很多尝试,但没有成功 Java 1.8 铬67 网络驱动 硒服务器standalone-3.12.0.jar
尝试使用tesng也会出现相同的错误
答案 0 :(得分:1)
在创建驱动程序初始化之前,您已经找到了元素,因此将抛出null指针异常。 请通过将驱动程序初始化部分移至webelement上方来更改代码,如下所示:
System.setProperty("webdriver.chrome.driver","E:\\Chrome\\chromedriver.exe");
driver=new ChromeDriver();
WebElement popup=driver.findElement(By.xpath("/html/body/div/div[2]/a[1]"));
WebElement name=driver.findElement(By.xpath("//*[@id='name']"));
WebElement submit=driver.findElement(By.xpath("//*[@id='submit']"));
WebElementproceed=driver.findElement(By.xpath("/html/body/div/div[2]/a[2]"));