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

时间:2018-06-29 17:18:24

标签: java selenium selenium-webdriver selenium-chromedriver

  1. 在将findElements方法与列表一起使用时获取NullPointerException。尝试使用POM模型也得到相同的错误。评论了代码的findelements部分,并对其进行了测试。我不确定我的代码有什么问题。

  2. 在代码中定义的警报未处理位置警报,没有发现alter异常。

我的代码需要解决什么?

代码:

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 个答案:

答案 0 :(得分:1)

#1: 您正在初始化所有实例Web元素,然后再添加driver变量,因此它将引发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

    }

方法2:仅在需要的方法内部初始化实例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;

    }

    public void travelDetails(){
        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

   }

#2 关于警报,您会收到特定于Chrome的通知。因此,您需要在驱动程序初始化部分中禁用以下通知选项

    ChromeOptions options = new ChromeOptions();
    options.addArguments("--disable-notifications");
    WebDriver driver=new ChromeDriver(options);

编辑:

要解决后续问题:

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

关于Web元素声明,如果您也想通过不同的方法访问相同的WebElement,则可以将WebElement声明为实例。如果List WebElement范围仅在searchFlights方法中,则建议在内部进行声明。 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));


}