I'm trying to use Selenium to click a button that loads more news on the page, but I always end up facing an org.openqa.selenium.ElementClickInterceptedException
or org.openqa.selenium.TimeoutException
. I feel like I'm lacking more knowledge about frontend, and I'm not sure if it's a problem in the code or a site protection mechanism. I have already managed to use Selenium to enter one of the news articles, but I can never scroll down to the button or identify it. Could someone help clarify my doubts?
The code:
import org.openqa.selenium.By;import org.openqa.selenium.JavascriptExecutor;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.chrome.ChromeDriver;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 Main { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\\ChromeD\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get("https://www.infomoney.com.br/mercados/"); WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(30)); // Maximum wait time of 30 seconds wait.until((ExpectedCondition<Boolean>) wd -> ((JavascriptExecutor) wd).executeScript("return document.readyState").equals("complete")); // Locate the "Load more" button WebElement loadMoreButton = wait.until(ExpectedConditions.visibilityOfElementLocated( By.xpath("//button[contains(@class, 'text-white') and contains(@class, 'bg-wl-neutral-950')]"))); // Scroll to the button using JavaScript JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("arguments[0].scrollIntoView(true);", loadMoreButton); loadMoreButton.click(); }}
I've tried scrolling and making various manipulations, such as forcing Selenium to wait a certain amount of time until the page loads, but no success.