如何解决net.thucydides.core.webdriver.exceptions.ElementShouldBeVisibleException-硒

时间:2018-08-25 18:45:04

标签: java selenium testing junit automation

我正在自动化一个页面,其中我必须选择两个价格,当选择第一个价格时,页面沿滚动方向滚动,这就是测试失败的地方,我得到了错误,我已经尝试了显式等待和thread.sleep但它不起作用。有什么想法吗?

这是错误:

  

net.thucydides.core.webdriver.exceptions.ElementShouldBeVisibleException:预期条件失败:等待[[RemoteWebDriver:XP上的chrome(e64b73bc32012e0506c1b2816cbc2ac2)]-> xpath:// div [@ id ='divAvailpanOut'] //要显示的[@ data-bind ='html:价格']](尝试50秒,间隔100毫秒)

这是我的代码:

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);
    }

我正在做的是浏览一系列价格并将它们分开以仅选择数字,然后将它们传递给double类型向量,以便能够比较其价格并选择最昂贵的价格。

这是自动化系统的页面,选择目的地和旅行日期后,错误会显示出来

https://www.vivaair.com/co/flight/reserva

1 个答案:

答案 0 :(得分:0)

据我了解,您尝试查找并选择价格最高的机票。

这里的代码示例为“从”和“到”图查找并选择最高价格:

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();

以下有用的链接:

https://www.w3schools.com/cssref/css_selectors.asp https://www.w3schools.com/xml/xpath_intro.asp