Issue
I am automating a page where I must choose two prices, when choosing the first price the page goes down the scroll and that's where the test fails, I get the error, I've already tried with explicit waits and thread.sleep but it does not work. any ideas?
net.thucydides.core.webdriver.exceptions.ElementShouldBeVisibleException: Expected condition failed: waiting for [[RemoteWebDriver: chrome on XP (e64b73bc32012e0506c1b2816cbc2ac2)] -> xpath: //div[@id='divAvailabilityOut']//span[@data-bind='html: Price']] to be displayed (tried for 50 second(s) with 100 milliseconds interval)
and this is my code:
public void escogerPreioMasCaro() throws InterruptedException {
List<WebElementFacade> listPreciosCaros = findAll(
"//div[@class='availabilityIn box']//span[@data-bind='html: Price']");
String[] strVectorCaros = new String[listPreciosCaros.size()];
int i = 0;
for (WebElementFacade listado : listPreciosCaros) {
System.out.println("Lista Precios Caros" + listado.getText());
strVectorCaros[i] = listado.getText().replaceAll("COP", " ").trim();
i++;
}
System.out.println(Arrays.toString(strVectorCaros).trim());
double[] vec1 = new double[strVectorCaros.length];
for (int g = 0; g < strVectorCaros.length; g++) {
try {
vec1[g] = Double.parseDouble(strVectorCaros[g]);
} catch (NumberFormatException ex) {
System.err.println("Ilegal input");
}
}
// System.out.println(Arrays.toString(vec1));
double precioMayor = vec1[0];
for (int x = 0; x < vec1.length; x++) {
// System.out.println(nombres[i] + " " + sueldos[i]);
if (vec1[x] > precioMayor) { //
precioMayor = vec1[x];
}
}
System.out.println("PrecioCaro" + precioMayor);
String precioMayorString = String.valueOf(precioMayor);
System.out.println("string " + precioMayorString);
for (WebElementFacade listado2 : listPreciosCaros) {
if (listado2.getText().contains(precioMayorString)) {
System.out.println(precioMayorString);
listado2.click();
}
}
Thread.sleep(2000);
}
What I am doing is to go through the series of prices and separate them to choose only the number, then pass them to double type vector to be able to compare their prices and choose the most expensive one
this is the page of the automation, the error presents it after choosing the destination and dates of travel
https://www.vivaair.com/co/flight/reserva
Solution
As I understood you try to find and select ticket with max price.
Here code sample to find and select max price for From and To figths:
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
List<Double> pricesDouble = new ArrayList<>();
//Get all prices without currency of FROM Flight section
List<WebElement> fromPrices = driver.findElements(By.cssSelector("#divAvailabilityOut span[data-bind='html: PriceWithoutCurrencySymbol']"));
//To get all prices without currency of TO Flight section use code below
//List<WebElement> toPrices = driver.findElements(By.cssSelector("#availabilityLoadingIn span[data-bind='html: PriceWithoutCurrencySymbol']"));
fromPrices.forEach(e -> {
try {
pricesDouble.add(Double.parseDouble(e.getAttribute("innerText")));
} catch (NumberFormatException ex) {
System.err.println("Ilegal input");
}
});
//Assert.assertTrue(pricesDouble.size()>0, "Get at least one price");
int indexOfMaxPrice = pricesDouble.indexOf(Collections.max(pricesDouble););
//Get clickable element with max price
fromPrices.get(indexOfMaxPrice).findElement(By.xpath("ancestor::div[@class='totalPrice']")).click();
Here helpful links:
https://www.w3schools.com/cssref/css_selectors.asp https://www.w3schools.com/xml/xpath_intro.asp
Answered By - Sers
Answer Checked By - Robin (JavaFixing Admin)