gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 11:04:07 24 4
gpt4 key购买 nike

  1. 当我将 findElements 方法与列表一起使用时,出现 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 元素初始化需要移至构造函数内,否则需要移至需要该特定元素的方法内。

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

例如:

方法一:在构造函数中初始化实例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));


}

关于java - 在 Selenium 中使用 findElements 的 NullpointerException 和 alter 未处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51106678/

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