我正在尝试捕获列表中的价格并打印它们。但是,执行会在搜索结果页面中停止,并且不会打印价格。我认为这是因为Xpath的水平(可能我不是从上层选择?)。我很困惑,因为我创建的Xpath,当我在Firepath中使用它时,它选择了39个匹配的节点。
提前感谢您的时间和建议。
代码:
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Flight {
public static WebDriver driver;
//This following section is for browser and getting the url
public static WebDriver browser(){
driver= new FirefoxDriver();
driver.get("https://www.orbitz.com/Flights");
return driver;
}
//this following section is getting the properties of the page
public static void getPageProperties(String ff,String ft, String fd, String rd){
WebElement flyFrom= driver.findElement(By.id("flight-origin"));
WebElement flyTo= driver.findElement(By.id("flight-destination"));
WebElement flyDate= driver.findElement(By.id("flight-departing"));
WebElement returnDate= driver.findElement(By.id("flight-returning"));
WebElement flight_search_btn= driver.findElement(By.id("search-button"));
flyFrom.sendKeys(ff);
flyTo.sendKeys(ft);
flyDate.sendKeys(fd);
returnDate.sendKeys(rd);
flight_search_btn.click();
}
// this following section will have the arguments that we will provide for flight search
public static void testFligthSearch(){
Flight f= new Flight();
f.browser();
f.getPageProperties("MSP", "SEA", "05/01/2017", "05/05/2017");
List<WebElement> pricelist= driver.findElements(By.xpath("//span[contains(@class,'dollars')]"));
for(WebElement e: pricelist){
System.out.println("The prices are: " + e.getText());
}
}
public static void main (String [] args){
Flight f= new Flight();
f.testFligthSearch();
}
}
问题:没有打印价格。
答案 0 :(得分:0)
您遇到此问题,因为尚未加载所有结果,因此您必须等到所有搜索结果都已加载,因此您需要等到搜索结果进度条达到100%,如下所示:
WebElement progressBar = driver.findElement(By.cssSelector(“#acol-interstitial&gt; div&gt; div”));
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.attributeContains(progressBar, "style", "width: 100%;"));
List<WebElement> pricelist= driver.findElements(By.cssSelector("ul#flightModuleList div.offer-price.urgent > span.visuallyhidden"));
for(WebElement e: pricelist){
System.out.println("The prices are: " + e.getText());
}