【问题标题】:null pointer exception in selenium code using java使用java的selenium代码中的空指针异常
【发布时间】:2013-05-16 14:05:04
【问题描述】:
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;

public class RedBus {
    Selenium selenium;

    @BeforeClass
    public void base()
    {
    Selenium selenium = new DefaultSelenium("localhost",4444,"*firefox","http://www.redbus.in");
    selenium.start();
    selenium.windowMaximize();
    selenium.open("/"); 
    selenium.waitForPageToLoad("10000");
    }       

    @Test
    public void domain() throws InterruptedException
    {           
            selenium.type("//input[@id='DDLSource']","hyde");
            Thread.sleep(5000);

            selenium.waitForPageToLoad("10000");
        if(selenium.isTextPresent("//dt[@value='Hyderabad']"))
        {
            selenium.click("//dt[@value='Hyderabad']");

        }
        else{
            System.out.println("ele not found");
        }
    /*  
        selenium.type("//input[@id='DDLDestination']","pune");

        selenium.click("//img[@alt='Select your date of journey']");
        */



    }

}

【问题讨论】:

  • 请描述您的问题并附上异常的详细信息。
  • 请发布您的 Stacktrace

标签: java javascript css string selenium


【解决方案1】:

虽然您在哪里获得 NullPointerException 并不明显,但我怀疑您需要更改以下行:

Selenium selenium = new DefaultSelenium("localhost",4444,"*firefox","http://www.redbus.in");

selenium = new DefaultSelenium("localhost",4444,"*firefox","http://www.redbus.in");

目前,您正在 setup 方法中初始化一个新的 Selenium 对象,该对象仅在您的 base 方法的范围内,而不是您的类级 Selenium 变量。

【讨论】:

    【解决方案2】:

    您定义了未初始化的类字段Selenium selenium;,因此为null。然后在base() 方法中创建另一个本地 变量Selenium selenium 并初始化它。然后在您的测试中尝试使用未初始化的 selenium 字段。

    要使您的代码正常工作,请从Selenium selenium = new DefaultSelenium... 行中删除Selenium,即:

    @BeforeClass
    public void base() {
        selenium = new DefaultSelenium("localhost",4444,"*firefox","http://www.redbus.in");
        selenium.start();
        selenium.windowMaximize();
        selenium.open("/"); 
        selenium.waitForPageToLoad("10000");
    }    
    

    【讨论】:

      【解决方案3】:

      试试这个

      DefaultSelenium selenium=null;

      //用这个代替Selenium selenium;

      WebDriver driver = new FirefoxDriver(); selenium = new WebDriverBackedSelenium(driver, "http://www.redbus.in");

      // 不需要使用 selenium.start();或者设置一个默认端口

      // 下一行 selenium.type .. 如果您使用的是 firefox 35 或更高版本,仍将返回一个空指针。所以首先尝试firefox 15,然后检查firefox 34,安装34后请不要进行任何firefox更新。

      我用过以下罐子

      selenium-server-coreless-1.0-20081010.060147.jar selenium-java-2.44.0.jar selenium-server-standalone-2.44.0.jar

      //导入语句

       import com.thoughtworks.selenium.DefaultSelenium;
         import com.thoughtworks.selenium.webdriven.WebDriverBackedSelenium;
         import org.openqa.selenium.firefox.FirefoxDriver;
         import org.openqa.selenium.WebDriver;
      

      问候, 拉维纳斯·埃迪林赫

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-30
        • 2011-01-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多