Selenium не может найти элемент, несмотря на прокрутку в поле зренияJAVA

Программисты JAVA общаются здесь
Ответить
Anonymous
 Selenium не может найти элемент, несмотря на прокрутку в поле зрения

Сообщение Anonymous »

Я пытаюсь скопировать кое-что с веб-сайта (URL-адрес в коде). Я могу очистить торговую марку и SDR, но кажется, что все, что ниже SDR, я не могу очистить. Я тестирую это только на первом результате, как только мне удастся это выяснить, я сделаю его динамическим. Надеюсь, нужно просто иметь селен в своем проекте и производный от Chrome, и затем они смогут скопировать/вставить этот код.
Приведенный ниже код выдает следующую ошибку:

Код: Выделить всё

Exception in thread "main" org.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of element located by By.xpath: /html/body/app-root/ecl-app/div[2]/app-search-page/app-search-container/div/div/section/div/app-elec-display-search-result/app-search-result/eui-block-content/div/app-search-result-item[1]/article/div[2]/div/app-elec-display-search-result-parameters/app-search-parameter-item[4]/div[2]/div/div[2]/div/div[1]/span (tried for 10 second(s) with 500 milliseconds interval)
Код:

Код: Выделить всё

public void scrape() throws InterruptedException {
System.out.println("Starting Scrape!");
String url = "https://eprel.ec.europa.eu/screen/product/electronicdisplays";

WebDriver driver = new ChromeDriver();
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
driver.get(url);
driver.manage().window().maximize();
WebElement until = wait.until(ExpectedConditions.presenceOfElementLocated(By.className("eui-block-content__wrapper")));
//The results have been loaded now

//Click on accept cookie page:
new WebDriverWait(driver, Duration.ofSeconds(3
)).until(ExpectedConditions.elementToBeClickable(By.linkText("Accept all cookies"))).click();

String moreButton = "/html/body/app-root/ecl-app/div[2]/app-search-page/app-search-container/div/div/section/div/app-elec-display-search-result/app-search-result/eui-block-content/div/app-search-result-item[1]/article/div[3]/div/a";
String xPathBrandName =     "/html/body/app-root/ecl-app/div[2]/app-search-page/app-search-container/div/div/section/div/app-elec-display-search-result/app-search-result/eui-block-content/div/app-search-result-item[1]/article/div[1]/div/div/div[1]/span[1]";
String xPathSDR =           "/html/body/app-root/ecl-app/div[2]/app-search-page/app-search-container/div/div/section/div/app-elec-display-search-result/app-search-result/eui-block-content/div/app-search-result-item[1]/article/div[2]/div/app-elec-display-search-result-parameters/app-search-parameter-item[3]/div[1]/div/div[2]/div/div[1]/span";
String energyRatingString = "/html/body/app-root/ecl-app/div[2]/app-search-page/app-search-container/div/div/section/div/app-elec-display-search-result/app-search-result/eui-block-content/div/app-search-result-item[1]/article/div[2]/div/app-elec-display-search-result-parameters/app-search-parameter-item[4]/div[2]/div/div[2]/div/div[1]/span";

//Clicking on more button to load more results to be visible
driver.findElement(By.xpath(moreButton)).click();

WebElement SDR = driver.findElement(By.xpath(xPathSDR));

//Using this logic to scroll to each of the result so it's visible on the web-page
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].scrollIntoView();", SDR);

WebElement brandName = driver.findElement(By.xpath(xPathBrandName));
WebElement energyRating = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(energyRatingString)));

System.out.println("Brand name: " + brandName.getText());
System.out.println("SDR name: " + SDR.getText());
System.out.println("energyRating: " + energyRating.getText());
}
Но перехожу на замену

Код: Выделить всё

WebElement energyRating = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(energyRatingString)));
в

Код: Выделить всё

WebElement energyRating = driver.findElement(By.xpath(energyRatingString ));
Выдает следующий результат:

Код: Выделить всё

Starting Scrape!
Brand name: Samsung
SDR name: 63
energyRating:
Я так озадачен тем, почему EnergyRating отсутствует и не выдает исключение NoSuchElementException

Подробнее здесь: https://stackoverflow.com/questions/782 ... -into-view
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «JAVA»