The targeted element is not a link, hence you cannot use the locator linkText
. Use XPath instead. Check code below:
Code:
WebDriver driver = new ChromeDriver();
driver.get("https://magento.softwaretestingboard.com/men/tops-men/jackets-men.html");
driver.manage().window().maximize();
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
// Wait for the cookie consent banner to appear and accept cookies. If the banner does not appear to you, remove this line
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//p[text()='Consent']"))).click();
String reqText = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[@data-ui-id='page-title-wrapper']"))).getText();
if(reqText.equals("Jackets")) {
System.out.println("Page loaded successfully with the correct text: " + reqText);
} else {
System.out.println("Text is incorrect");
}
driver.quit();
Imports required:
import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;