2

.Такое вот задание надо на сайте https://www.slivki.by/sushi-2 выбрать суши с самой большой скидкой. Покажите что я неправильно написал? Вот мой неудачный код.

  public class Firstcase {
    @Test
    public void test2() {
        FirefoxDriver driver = new FirefoxDriver();
        driver.get("https://www.slivki.by/sushi-2");
        List<WebElement> discont = driver.findElements(By.className("discount-label"));
        List<WebElement> sortedDiscont = driver.findElements(By.className("discount-label"));
        Collections.sort(sortedDiscont, comparator);
        System.out.println(sortedDiscont.get(0).getText() + " index= " + discont.indexOf(sortedDiscont.get(0)));discont.get(discont.indexOf(sortedDiscont.get(0))).click();
      int k=discont.indexOf(sortedDiscont.get(0));
     String sw=Integer.toString(k);
   String Str="//img[contains(@src,'http://store.akamai.steamstatic.com/public/images/blank.gif')])[30]";
      String fin=Str.replace("30", sw);
      System.out.println(fin);
     // driver.findElementByXPath(fin);
        for (WebElement element : discont) {
            System.out.println(element.getText() + "\n");
        }
        discont.get(discont.indexOf(sortedDiscont.get(0))).click();
        driver.quit();
    }


Comparator<WebElement> comparator = new Comparator<WebElement>() {
    @Override
    public int compare(WebElement o1, WebElement o2) {
        int firstValue = 0;
        int secondValue = 0;
        try {
            firstValue = Integer.parseInt(o1.getText().replace("%", "").replace("-", ""));
            secondValue = Integer.parseInt(o2.getText().replace("%", "").replace("-", ""));
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (firstValue == 0 && secondValue == 0) {
            return 0;
        }
        return firstValue > secondValue ? -1 : 1;
    }
};}

1 ответ 1

3

Можно пойти другим путем, после того как мы нашли элемент с самой большой скидкой берем его родителя и у него уже ищем элемент с тегом img:

WebElement result= discont.get(0).findElement(By.xpath("..//img"));
result.click();

Советую вам изучить XPath, это как регулярки для разбора строк, имеет мощный механизм поиска.

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.