0

I'm new to Automation and I'm trying to test this site: https://magento.softwaretestingboard.com/

and on this page driver.get("https://magento.softwaretestingboard.com/men/tops-men/jackets-men.html"); I want to use the "Jackets" text on the upper left to validate that the site has loaded to the appropriate page.

However, I get the NO SUCH ELEMENT EXCEPTION after using By.LinkText. Here's what I did to locate the element:

WebElement jackets = driver.findElement(By.linkText("Jackets"));
Boolean jacketStatus = jackets.isDisplayed();
New contributor
Nicole is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
2
  • 1
    "Jacket" is not a link (you cannot click on it).
    – Glorfindel
    Commented 4 hours ago
  • It's not supposed to be clicked. Just a basis to validate that the site has loaded to the page containing the products under the Jacket Category.
    – Nicole
    Commented 2 hours ago

2 Answers 2

0

"link text" implies it only looks for links, e.g. <a> elements. The element you're looking for is a <span>, which is not a link.

<h1 class="page-title" id="page-title-heading" aria-labelledby="page-title-heading toolbar-amount">
        <span class="base" data-ui-id="page-title-wrapper">Jackets</span>
</h1>

I don't know Selenium, but if I look at this documentation page, I'd suggest checking for By.id("page-title-heading"). HTML IDs should be unique on a single page, unlike link texts (note that the breadcrumb also contains a "Jackets" item).

1
  • ohhhh ok, I got it. Thank you!
    – Nicole
    Commented 1 hour ago
0

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;
2
  • Hey, thanks, it works! Another thing.. the first time I run my code, there are no issues with locating the elements but once I immediately run the same exact code again, I'm getting the NO SUCH ELEMENT EXCEPTION. I usually get this exception unless I wait for a period of time before running the code again.
    – Nicole
    Commented 1 hour ago
  • @Nicole At what line are you getting that exception second time? And share your full code in the question.
    – Shawn
    Commented 24 mins ago

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.