Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在selenium中使用FindBy时出错。错误消息空点异常_Java_Selenium_Selenium Webdriver_Findby - Fatal编程技术网

Java 在selenium中使用FindBy时出错。错误消息空点异常

Java 在selenium中使用FindBy时出错。错误消息空点异常,java,selenium,selenium-webdriver,findby,Java,Selenium,Selenium Webdriver,Findby,在HotelLink.click行上获取错误,并且该HotelLink元素是使用findBy注释定义的,但是获取java.lang.NullPointerException错误由于您使用@findBy注释,您需要在使用它之前初始化所有web元素 为HotelBookingTest类创建构造,并使用PageFactory进行初始化,如下所示: import com.sun.javafx.PlatformUtil; import org.openqa.selenium.By; import org

在HotelLink.click行上获取错误,并且该HotelLink元素是使用findBy注释定义的,但是获取java.lang.NullPointerException错误

由于您使用@findBy注释,您需要在使用它之前初始化所有web元素

为HotelBookingTest类创建构造,并使用PageFactory进行初始化,如下所示:

import com.sun.javafx.PlatformUtil;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.Test;

public class HotelBookingTest {
    WebDriver driver;
    @FindBy(xpath= "//*[@class='hotelApp ']")
    public static WebElement hotelLink;


    @Test
    public void shouldBeAbleToSearchForHotels() {
        setDriverPath();
        driver = new ChromeDriver();
        driver.get("https://www.cleartrip.com/");
        boolean hotelLinkDisplayed = hotelLink.isDisplayed();

       hotelLink.click();
        driver.quit();

    }
}
从相应的包导入PageFactory,并删除`hotelLink'前面的static

我希望它能有所帮助……

因为您正在使用@FindBy注释,所以在使用它之前需要初始化所有web元素

为HotelBookingTest类创建构造,并使用PageFactory进行初始化,如下所示:

import com.sun.javafx.PlatformUtil;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.Test;

public class HotelBookingTest {
    WebDriver driver;
    @FindBy(xpath= "//*[@class='hotelApp ']")
    public static WebElement hotelLink;


    @Test
    public void shouldBeAbleToSearchForHotels() {
        setDriverPath();
        driver = new ChromeDriver();
        driver.get("https://www.cleartrip.com/");
        boolean hotelLinkDisplayed = hotelLink.isDisplayed();

       hotelLink.click();
        driver.quit();

    }
}
从相应的包导入PageFactory,并删除`hotelLink'前面的static


我希望它有帮助…

因为您正在使用@FindBy注释 在使用之前,必须初始化元素

您可以通过创建接受WebDriver类型作为参数的参数化构造函数来实现这一点

import com.sun.javafx.PlatformUtil;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.Test;

public class HotelBookingTest {
    WebDriver driver;

    @FindBy(xpath= "//*[@class='hotelApp ']")
    public WebElement hotelLink;

    public HotelBookingTest(WebDriver driver) {
        PageFactory.initElements(driver, this);
    }

    @Test
    public void shouldBeAbleToSearchForHotels() {
        setDriverPath();
        driver = new ChromeDriver();
        new HotelBookingTest(driver);
        driver.get("https://www.cleartrip.com/");
        boolean hotelLinkDisplayed = hotelLink.isDisplayed();

        hotelLink.click();
        driver.quit();
    }
}

因为您使用的是@FindBy注释 在使用之前,必须初始化元素

您可以通过创建接受WebDriver类型作为参数的参数化构造函数来实现这一点

import com.sun.javafx.PlatformUtil;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.Test;

public class HotelBookingTest {
    WebDriver driver;

    @FindBy(xpath= "//*[@class='hotelApp ']")
    public WebElement hotelLink;

    public HotelBookingTest(WebDriver driver) {
        PageFactory.initElements(driver, this);
    }

    @Test
    public void shouldBeAbleToSearchForHotels() {
        setDriverPath();
        driver = new ChromeDriver();
        new HotelBookingTest(driver);
        driver.get("https://www.cleartrip.com/");
        boolean hotelLinkDisplayed = hotelLink.isDisplayed();

        hotelLink.click();
        driver.quit();
    }
}
对于@FindBy注释,您需要在搜索WebElement之前实现它

您可以添加方法,以简单的方式为您执行此操作:

        PageFactory.initElements(driver, this);```

and call this constructor after opening the browser.
i.e after this line 
```driver = new ChromeDriver();```
对于@FindBy注释,您需要在搜索WebElement之前实现它

您可以添加方法,以简单的方式为您执行此操作:

        PageFactory.initElements(driver, this);```

and call this constructor after opening the browser.
i.e after this line 
```driver = new ChromeDriver();```

您是否尝试过等待页面加载,并在运行selenium时在浏览器中显示您看到的页面来验证此行为?它通常非常有用?另外,你确定你应该在类内部查找链接,而不是类本身吗?请共享它的html源代码。使用错误跟踪日志更新问题。如果你试图等待页面加载,并在selenium运行时在浏览器中查看页面的显示来验证此行为,通常会有很大帮助?另外,你确定你应该在类中查找链接,而不是类本身吗?请分享它的html源代码。用错误跟踪日志更新问题