@FreezkoSS

Почему автотест не видит элементы на странице?

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

Использовал паттерн Base object, структура кода следующая:

conf файл:

MarketPage = https://market.yandex.ru/
ChromeDriver = chromedriver.exe
product = Xiaomi


класс для подключения conf файла:
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class ConfProperties {
    protected static FileInputStream fileInputStream;
    protected static Properties PROPERTIES;
    static {
        try {
            //указание пути до файла с настройками
            fileInputStream = new FileInputStream("src/main/resources/conf.properties");
            PROPERTIES = new Properties();
            PROPERTIES.load(fileInputStream);
        } catch (IOException e) {
            e.printStackTrace();
            //обработка возможного исключения (нет файла и т.п.)
        } finally {
            if (fileInputStream != null)
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace(); } } }
    /**
     * метод для возврата строки со значением из файла с настройками
     */
    public static String getProperty(String key) {
        return PROPERTIES.getProperty(key);
    }
}


Класс YandexMarketTest, там описаны локаторы и методы:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

public class MarketPage {
    public WebDriver driver;
    public MarketPage(WebDriver driver) {
        PageFactory.initElements(driver, this);
        this.driver = driver;
    }

    //локатор поля поиска
    @FindBy(xpath = "//*[@id=\"header-search\"]")
    private WebElement searchField;

    @FindBy(xpath = "/html/body/div[1]/header/div/div/div/div/div/div/div/form/div/button")
    private WebElement searchButton;

    @FindBy(xpath = "//*[@id=\"page-db449q5aa64\"]/div/div/div/div/div/div[2]/div/div/div/article/div[2]/div[1]/h3/a")
    private WebElement firstProductLink;

    public void inputSearch(String product){
        searchField.sendKeys(product);
    }

    public void clickSearchButton(){
        searchButton.click();
    }

    public void clickFirstProduct(){
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        wait.until(ExpectedConditions.visibilityOf(firstProductLink));
        firstProductLink.click();
    }
}


Класс YandexMarketTest - тут вызов тестов, подключение web драйвера и закрытие браузера после тестов
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

public class MarketPage {
    public WebDriver driver;
    public MarketPage(WebDriver driver) {
        PageFactory.initElements(driver, this);
        this.driver = driver;
    }

    //локатор поля поиска
    @FindBy(xpath = "//*[@id=\"header-search\"]")
    private WebElement searchField;

    @FindBy(xpath = "/html/body/div[1]/header/div/div/div/div/div/div/div/form/div/button")
    private WebElement searchButton;

    @FindBy(xpath = "//*[@id=\"page-db449q5aa64\"]/div/div/div/div/div/div[2]/div/div/div/article/div[2]/div[1]/h3/a")
    private WebElement firstProductLink;

    public void inputSearch(String product){
        searchField.sendKeys(product);
    }

    public void clickSearchButton(){
        searchButton.click();
    }

    public void clickFirstProduct(){
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        wait.until(ExpectedConditions.visibilityOf(firstProductLink));
        firstProductLink.click();
    }
}


Вот сама ошибка, пишет не найден элемент, хотя xpath самой ссылки и он один и тот же:
6435c5ef8ca08469513705.png

в Чем может быть проблема?
  • Вопрос задан
  • 159 просмотров
Решения вопроса 1
@artas0004
Скорее всего проблема в динамической странице.
Попробуй вместо стандартных методов для поиска и взамодействия обращатся к элементам через джавакрипт.
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы