当我对之前在代码中执行的同一个 WebElement 执行 click() 时出现空指针异常 [英] I am getting nullpointer exception when I perform click() for the same WebElement which I have performed earlier in my Code

查看:12
本文介绍了当我对之前在代码中执行的同一个 WebElement 执行 click() 时出现空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 Java ans Selenium 上得到了一些很好的实践.当我使用相同的Input_Search_Box"Webelement 执行单击方法时,它会引发空指针异常.我用谷歌搜索并尝试了一些解决方法,例如添加线程,添加显式等待,但仍然不知道我错过了什么.任何建议将不胜感激.

I am getting some good handson on my Java ans Selenium. When I use the same "Input_Search_Box" Webelement to perform click method it throws a nullpointer exception. I have googled and tried few work around like adding Thread, adding Explicit wait but still no clue where i miss. Any suggestion would be greatly appreciated.

这是我的代码:

 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);      

    }   

    }

推荐答案

如果页面已更改/重新加载,则需要再次使用 find.

If the page has changed/reloaded then you need to use find again.

有时在用户操作时,页面可能会触发可以更改页面的调用,从而可以更改页面的状态并且当前找到的对象丢失,这可能导致过时元素异常或空异常.

Sometimes on user actions the page can trigger calls that can change the page that can change the state of the page and the current found objects are lost this can results in a stale element exception or null exception.

这篇关于当我对之前在代码中执行的同一个 WebElement 执行 click() 时出现空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆