NullpointerException 在 Selenium 和 alter 中使用 findElements 不被处


  1. 当我将findElements方法与list一起使用时获取NullPointerException。尝试使用POM模型也得到相同的错误。注释了代码的 findelements 部分并测试它工作正常。我不确定我的代码出了什么问题。

  2. 位置警报不由代码中定义的警报处理,未找到更改异常。

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

法典:

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:在启动driver变量之前,您正在初始化所有实例 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
}

方法 2:仅在需要的方法中初始化实例 WebElement。假设您只想在某个方法中访问优惠券和 airname 元素列表,那么您可以单独在该特定方法中初始化

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关于警报,您会收到特定于铬的通知。因此,您需要在驱动程序初始化部分中禁用通知选项,如下所示

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

编辑:

要解决后续问题:

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

关于 Web 元素声明,如果您还想以不同的方法访问相同的 WebElement,那么您可以将 WebElement 声明为实例。如果列表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));

}

相关内容

  • 没有找到相关文章

最新更新