gpt4 book ai didi

java - 使用java的selenium代码中的空指针异常

转载 作者:行者123 更新时间:2023-11-28 16:43:57 25 4
gpt4 key购买 nike

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']");
*/



}

}

最佳答案

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

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

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

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

关于java - 使用java的selenium代码中的空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16589625/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com