selenium中的Java nullpoint错误

时间:2017-04-22 14:32:28

标签: java selenium selenium-webdriver

下面是我的代码但是它给了我零点错误。请帮我解决一下是什么错误

package path1;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;

public class New1 {
  public static void main(String[] args) {          
    System.setProperty("webdriv`enter code here`er.gecko.driver", "C://Users/Ramesh/Desktop/Udemy/Selenium/geckodriver.exe");      
    ProfilesIni profile = new ProfilesIni();

    FirefoxProfile testprofile = profile.getProfile("default");
    testprofile.setAcceptUntrustedCertificates(true);
    testprofile.setAssumeUntrustedCertificateIssuer(true);

    WebDriver driver = new FirefoxDriver(testprofile);
    driver.get("https://www.w3schools.com/");
  }    
}

2 个答案:

答案 0 :(得分:0)

这是因为这一行:

     FirefoxProfile testprofile = profile.getProfile("default");

你不能得到那个特定的个人资料。尝试检查您的个人资料并在那里进行更改。你的设置也错了。用这个改变你的第一行:

 System.setProperty("webdriver.gecko.driver", "C:\\Selenium\\geckodriver.exe");

答案 1 :(得分:-1)

以下是您需要解决的一些要点:

  1. System.setProperty - 您需要正确提及webdriver.gecko.driver
  2. FirefoxProfile testprofile = profile.getProfile("default"):值得一提的是,默认的Firefox配置文件不是非常自动化。如果要在Firefox浏览器上可靠地运行自动化,建议创建单独的配置文件。自动化配置文件应轻松加载,并具有特殊的代理和其他设置以运行良好的测试。您应该与在所有开发和测试执行计算机上使用的配置文件保持一致。如果您在任何地方使用不同的配置文件,您接受的SSL证书或您安装的插件会有所不同,这会使测试在计算机上的行为不同。

    • 有几次您需要在配置文件中使用特殊功能才能使测试执行可靠。最常见的示例是SSL证书设置或处理自签名证书的浏览器插件。创建一个处理这些特殊测试需求并将其与测试执行代码一起打包和部署的配置文件是很有意义的。
    • 您应该使用非常轻量级的配置文件,只需要执行所需的设置和插件。每次Selenium启动一个驱动Firefox实例的新会话时,它会将整个配置文件复制到一些临时目录中,如果配置文件很大,它就会使它变得缓慢但不可靠。
    • 创建一个新的Firefox配置文件并在Test脚本中使用相同的三个步骤。首先,您需要启动配置文件管理器,第二个是创建新配置文件,第三个是在测试脚本中使用相同的配置文件。让我们假设我们创建了一个名为" debanjan"的新FirefoxProfile。
  3. 使用以下代码使用新的FirefoxProfile" debanjan"打开https://www.w3schools.com/

    System.setProperty("webdriver.gecko.driver", "C:/Utility/BrowserDrivers/geckodriver.exe");
    ProfilesIni profile = new ProfilesIni();
    FirefoxProfile testprofile = profile.getProfile("debanjan");
    testprofile.setAcceptUntrustedCertificates(true);
    testprofile.setAssumeUntrustedCertificateIssuer(true);
    WebDriver driver = new FirefoxDriver(testprofile);
    driver.get("https://www.w3schools.com/");