Java 当我对前面代码中执行的同一WebElement执行click()时,我遇到了nullpointer异常
我的Java ans Selenium有一些不错的手感。当我使用相同的“Input\u Search\u Box”Webelement来执行click方法时,它会抛出一个nullpointer异常。我在谷歌上搜索并尝试了一些工作,比如添加线程、添加显式等待,但仍然不知道我错过了什么。如有任何建议,将不胜感激 这是我的密码:Java 当我对前面代码中执行的同一WebElement执行click()时,我遇到了nullpointer异常,java,selenium,nullpointerexception,Java,Selenium,Nullpointerexception,我的Java ans Selenium有一些不错的手感。当我使用相同的“Input\u Search\u Box”Webelement来执行click方法时,它会抛出一个nullpointer异常。我在谷歌上搜索并尝试了一些工作,比如添加线程、添加显式等待,但仍然不知道我错过了什么。如有任何建议,将不胜感激 这是我的密码: public class Testclass { WebElement Input_Search_Box; WebDriver driver; @T
public class Testclass {
WebElement Input_Search_Box;
WebDriver driver;
@Test
public void openBrowser() throws Exception{
System.setProperty("webdriver.chrome.driver","E:\\Ecilpse\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.get("https://en.wikipedia.org/wiki/Main_Page");
driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
driver.manage().window().maximize();
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollBy(0,500)");
WebElement Click_Create_Book = driver.findElement(By.xpath(".//*[@id='coll-create_a_book']/a"));
Click_Create_Book.click();
WebElement Start_Book_Creator_Btn = driver.findElement(By.xpath(".//*[@id='mw-content-text']/form/div/div[1]/button"));
Start_Book_Creator_Btn.click();
Input_Search_Box = driver.findElement(By.xpath(".//*[@id='searchInput']"));
Input_Search_Box.click();
Input_Search_Box.sendKeys("Selenium",Keys.ENTER);
for(int i =0;i<=8;i++){
try{
if(driver.findElement(By.xpath(".//*[@id='siteNotice']/div[2]/div[2]/div")).isDisplayed())
break;
}
catch(Exception e){
jse.executeScript("window.scrollBy(0,2500)");
}
}
for(int j=0;j<=5;j++){
if(driver.findElement(By.id("coll-add_article")).isDisplayed()) {
System.out.println("If Executed");
break;
}else
{
WebElement Book_Remove = driver.findElement(By.xpath(".//*[@id='coll-remove_article']"));
Book_Remove.click();
}
}
WebElement Add_This_Book = driver.findElement(By.xpath(".//*[@id='coll-add_article']"));
Add_This_Book.click();
Thread.sleep(3000);
for(int k =0;k<=6;k++){
jse.executeScript("window.scrollBy(0,-2500)");
Thread.sleep(3000);
}
Thread.sleep(4000);
System.out.println("Sctipr on hold for 4k seconds");
//Here is the Nullpointer error occuring
Input_Search_Box.click();
Input_SearchBox.sendKeys("JSCRIPT",Keys.ENTER);
}
}
公共类Testclass{
WebElement输入搜索框;
网络驱动程序;
@试验
public void openBrowser()引发异常{
System.setProperty(“webdriver.chrome.driver”,“E:\\Ecilpse\\chromedriver\u win32\\chromedriver.exe”);
驱动程序=新的ChromeDriver();
驱动程序。获取(“https://en.wikipedia.org/wiki/Main_Page");
driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
driver.manage().window().maximize();
JavascriptExecutor jse=(JavascriptExecutor)驱动程序;
jse.executeScript(“window.scrollBy(0500)”;
WebElement单击\u Create\u Book=driver.findElement(By.xpath(“./*[@id='coll-Create\u Book']/a”);
单击创建书本。单击();
WebElement Start\u Book\u Creator\u Btn=driver.findElement(By.xpath(“./*[@id='mw-content-text']/form/div/div[1]/button”);
启动书籍创建者。单击();
Input_Search_Box=driver.findElement(By.xpath(“./*[@id='searchInput']);
输入搜索框。单击();
输入搜索框。sendKeys(“Selenium”,Keys.ENTER);
对于(int i=0;i如果页面已更改/重新加载,则需要再次使用“查找”
有时,在用户操作上,页面可能会触发调用,从而更改页面,从而更改页面的状态,并且当前找到的对象丢失。这可能会导致过时元素异常或空异常。如果页面已更改/重新加载,则需要再次使用“查找”
有时,在用户操作上,页面会触发调用,从而更改页面,从而更改页面状态,当前找到的对象会丢失,这可能会导致过时元素异常或空异常。如果页面已更改/重新加载,则需要再次使用“查找”。@lauda:非常感谢您的快速响应。page没有被重新加载。我只是向上滚动到顶部并单击一个搜索文本框。我甚至尝试再次添加find元素并进行了检查,但没有成功。@劳达:再次添加find元素后代码工作正常,非常感谢。这是我的sinppet:WebElement fFind_Search=driver.findElement(By.xpath(“)//*[@id='searchInput']”);fFind\u Search.click();fFind\u Search.sendKeys(“JUnit”);如果页面已更改/重新加载,则您需要再次使用“查找”。@劳达:非常感谢您的快速响应。页面没有重新加载。我只是向上滚动到顶部并单击搜索文本框。我甚至尝试再次为此添加“查找”元素,并进行了选中,但没有成功。@劳达:添加“查找”后代码工作正常再次,非常感谢。这是我的单张:WebElement fFind_Search=driver.findElement(By.xpath(“./*[@id='searchInput']));fFind_Search.click();fFind_Search.sendKeys(“JUnit”);非常感谢。非常感谢。