Java 未处理在Selenium和alter中使用findElements的NullpointerException

Java 未处理在Selenium和alter中使用findElements的NullpointerException,java,selenium,selenium-webdriver,selenium-chromedriver,Java,Selenium,Selenium Webdriver,Selenium Chromedriver,在将findElements方法与list一起使用时获取NullPointerException。尝试了POM模型,也得到了同样的错误。对代码的findelements部分进行了注释,并对其进行了测试。我不确定我的代码中有什么错误 代码中定义的警报未处理位置警报,未发现异常 我的代码中需要修复什么 代码: import java.util.ArrayList; import java.util.List; import org.apache.poi.xssf.usermodel.XSSFShee

在将findElements方法与list一起使用时获取NullPointerException。尝试了POM模型,也得到了同样的错误。对代码的findelements部分进行了注释,并对其进行了测试。我不确定我的代码中有什么错误

  • 代码中定义的警报未处理位置警报,未发现异常

  • 我的代码中需要修复什么

    代码:

    import java.util.ArrayList;
    import java.util.List;
    import org.apache.poi.xssf.usermodel.XSSFSheet;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    import org.openqa.selenium.By;
    import org.openqa.selenium.Keys;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.interactions.Action;
    import org.openqa.selenium.interactions.Actions;
    import org.openqa.selenium.support.FindBy;
    import org.openqa.selenium.support.How;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.Select;
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    class CleartripPOM1 {
        WebDriver driver;
        private String url="https://www.cleartrip.com/";
        public CleartripPOM1(WebDriver driver) {
            this.driver=driver;
    }
    
    By from=By.xpath("//input[@title='Any worldwide city or airport' and @etitle='From']");
    By to=By.xpath("//input[@title='Any worldwide city or airport' and @etitle='To']");
    By date=By.xpath("//input[@id='DepartDate']");
    By adults=By.id("Adults");
    By childerns=By.cssSelector("select[name='childs'][id='Childrens']" );
    By infants=By.name("infants");
    By search=By.xpath("//input[@id='SearchBtn']");
    By airline=By.xpath("//li[@class='vendor']/a");
    By depart=By.xpath("//li[@class='depart']/a");
    By duration=By.xpath("//li[@class='duration']/a");
    By price=By.xpath("//li[@class='price']/a");
    private List <WebElement>coupon =driver.findElements(By.xpath("//ul[@class='listView flights']/li/table/tbody/tr/td/span"));
    private List <WebElement> airName=driver.findElements(By.xpath("//ul[@class='listView flights']/li/table/tbody/tr/th/small"));
    private List <WebElement> departure=driver.findElements(By.xpath("//ul[@class='listView flights']/li/table/tbody/tr/th[@class='depart']"));
    private List <WebElement> arrival=driver.findElements(By.xpath("//ul[@class='listView flights']/li/table/tbody/tr/th[@class='arrive']"));
    private List <WebElement> travelTime=driver.findElements(By.xpath("//ul[@class='listView flights']/li/table/tbody/tr/th[@class='duration']"));
    private List <WebElement> ticketFare=driver.findElements(By.xpath("//ul[@class='listView flights']/li/table/tbody/tr/th[@class='price']"));
    
    public ArrayList<String> airlineNames(List<WebElement>el) {
        ArrayList<String> names= new ArrayList<String>();
        for(WebElement e:el) {
            names.add(e.getText());
        }
        return names;
    }
    
    public void url_launch() {
        driver.get(url);
        driver.manage().window().maximize();
    //  WebDriverWait wait= new WebDriverWait(driver,20);
    //  wait.until(ExpectedConditions.alertIsPresent());
    //  Alert alert = driver.switchTo().alert();
    //  alert.dismiss();
    
    }
    public void actions(By e, String location) {
        Actions act = new Actions(driver);
        Action action= act.click(driver.findElement(e)).sendKeys(location).build();
        action.perform();
    }
    
    public void selection(By e,String selection) {
        Select select = new Select(driver.findElement(e));
        select.selectByValue(selection);
    
    }
    public void searchFlights() {
        actions(from,"Chennai");
        actions(to,"Madurai");
        actions(date,"25/08/2018");
        driver.findElement(date).sendKeys(Keys.TAB);
        selection(adults,"2");
        selection(childerns,"3");
        selection(infants,"1");
        driver.findElement(search).click();
        WebDriverWait wait= new WebDriverWait(driver,20);
        wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//li[@class='vendor']/a")));
        System.out.println(airlineNames(coupon));
        System.out.println(airlineNames(airName));
        System.out.println(airlineNames(departure));
        System.out.println(airlineNames(arrival));
        System.out.println(airlineNames(travelTime));
        System.out.println(airlineNames(ticketFare));
    
    public class TestRunner {
    
        public static void main(String[] args) {
            WebDriver driver = new ChromeDriver();
            // TODO Auto-generated method stub
            CleartripPOM1 ct= new CleartripPOM1(driver);
            ct.url_launch();
            ct.searchFlights();
        }
    }
    
    #1: 在初始化
    驱动程序
    变量之前,您正在初始化所有实例Web元素,因此它将抛出
    NullPointerException
    。实例Web元素初始化需要在构造函数内部移动,否则需要在需要特定元素的方法一侧移动

    我用两个元素的样本给出了两种方法。请使用以下任一方法更改所有实例元素初始化

    例如:

    方法1:初始化构造函数中的实例webelement。这样,一个类中的所有方法都可以访问所有webelement

        WebDriver driver;
    
        private List <WebElement>coupon;
        private List <WebElement> airName;
        private String url="https://www.cleartrip.com/";
        public CleartripPOM1(WebDriver driver) {
            this.driver=driver;
            coupon=driver.findElements(By.xpath("//ul[@class='listView flights']/li/table/tbody/tr/td/span"));
            airName=driver.findElements(By.xpath("//ul[@class='listView flights']/li/table/tbody/tr/th/small"));
            -----------
            ----------- and so on
    
        }
    
    编辑:

    解决后续问题:


    仅执行搜索操作后,您将获得结果部分,并且所有列表WebElement都将从结果部分中标识。因此,您需要在执行搜索操作后初始化所有列表WebElement,如下所示

    关于Web元素声明,如果您想在不同的方法中访问相同的WebElement,那么您可以将WebElement声明为实例。如果仅在searchFlights方法中列出WebElement范围,那么我建议在searchFlights方法中声明,而不是在实例声明中声明

    需要更改搜索方法:

    public void searchFlights() {
        actions(from,"Chennai");
        actions(to,"Madurai");
        actions(date,"25/08/2018");
        driver.findElement(date).sendKeys(Keys.TAB);
        selection(adults,"2");
        selection(childerns,"3");
        selection(infants,"1");
        driver.findElement(search).click();
        WebDriverWait wait= new WebDriverWait(driver,20);
        wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//li[@class='vendor']/a")));
    
        coupon =driver.findElements(By.xpath("//ul[@class='listView flights']/li/table/tbody/tr/td/span"));
        airName=driver.findElements(By.xpath("//ul[@class='listView flights']/li/table/tbody/tr/th/small"));
        departure=driver.findElements(By.xpath("//ul[@class='listView flights']/li/table/tbody/tr/th[@class='depart']"));
        arrival=driver.findElements(By.xpath("//ul[@class='listView flights']/li/table/tbody/tr/th[@class='arrive']"));
        travelTime=driver.findElements(By.xpath("//ul[@class='listView flights']/li/table/tbody/tr/th[@class='duration']"));
        ticketFare=driver.findElements(By.xpath("//ul[@class='listView flights']/li/table/tbody/tr/th[@class='price']"));
    
    
        System.out.println(airlineNames(coupon));
        System.out.println(airlineNames(airName));
        System.out.println(airlineNames(departure));
        System.out.println(airlineNames(arrival));
        System.out.println(airlineNames(travelTime));
        System.out.println(airlineNames(ticketFare));
    
    
    }
    

    谢谢您能告诉我为什么在代码的这一部分中不处理此警报吗//WebDriverWait wait=new WebDriverWait(driver,20); // wait.until(ExpectedConditions.alertIsPresent());//警报警报=驱动程序。切换到()。警报();//警惕。解散()@Subburaj我面临的另一个问题是,我为下面的函数调用获取空列表,但它应该返回xpath的字符串值。我已经验证了xpath的正确性,请您在这里帮助System.out.println(airlineNames(优惠券));System.out.println(airlineNames(airlname));系统输出打印LN(航空公司(起飞));系统输出打印LN(航班名称(到达));系统输出打印LN(航空线名称(旅行时间));系统输出打印LN(airlineNames(ticketFare));仅执行搜索操作后,您将获得结果。因此,您需要初始化这两行之间的所有List WebElement wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(“//li[@class='vendor']/a”);系统输出打印LN(航空公司(优惠券));。我也更新了第二个问题的答案。请检查更新后的答案。我已经用上述问题编辑了答案。请检查更新后的答案。如果您对上述答案感到满意,请不要忘记放弃投票,并考虑接受答案。
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--disable-notifications");
        WebDriver driver=new ChromeDriver(options);
    
    public void searchFlights() {
        actions(from,"Chennai");
        actions(to,"Madurai");
        actions(date,"25/08/2018");
        driver.findElement(date).sendKeys(Keys.TAB);
        selection(adults,"2");
        selection(childerns,"3");
        selection(infants,"1");
        driver.findElement(search).click();
        WebDriverWait wait= new WebDriverWait(driver,20);
        wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//li[@class='vendor']/a")));
    
        coupon =driver.findElements(By.xpath("//ul[@class='listView flights']/li/table/tbody/tr/td/span"));
        airName=driver.findElements(By.xpath("//ul[@class='listView flights']/li/table/tbody/tr/th/small"));
        departure=driver.findElements(By.xpath("//ul[@class='listView flights']/li/table/tbody/tr/th[@class='depart']"));
        arrival=driver.findElements(By.xpath("//ul[@class='listView flights']/li/table/tbody/tr/th[@class='arrive']"));
        travelTime=driver.findElements(By.xpath("//ul[@class='listView flights']/li/table/tbody/tr/th[@class='duration']"));
        ticketFare=driver.findElements(By.xpath("//ul[@class='listView flights']/li/table/tbody/tr/th[@class='price']"));
    
    
        System.out.println(airlineNames(coupon));
        System.out.println(airlineNames(airName));
        System.out.println(airlineNames(departure));
        System.out.println(airlineNames(arrival));
        System.out.println(airlineNames(travelTime));
        System.out.println(airlineNames(ticketFare));
    
    
    }