0

[![enter image description here][1]][1]

4
  • Please answer me as quick as possible . I am stuck because of this problem. Feb 25 at 7:01
  • What have you tried? Feb 25 at 7:41
  • Used every method using href xpath , by using text , partial link text, link text. Feb 25 at 8:06
  • What error are you getting? How does your code look like? Feb 25 at 8:10
1

Element is inside an iframe and also there some invisible elements so you need to ensure you are locating the correct element :

    driver = new ChromeDriver();
    driver.manage().window().maximize();
    driver.get("https://frontend.nopcommerce.com/");
    WebElement frame = driver.findElement(By.tagName("iframe"));
    driver.switchTo().frame(frame);
    new WebDriverWait(driver, Duration.ofSeconds(5)).until(ExpectedConditions.elementToBeClickable(By.xpath("//ul[@class=\"top-menu notmobile\"]//a[contains(text(),\"Electronics\")]/parent::li"))).click();

Also now if you want to interact with elements outside iframe then you have to switch back from iframe again:

     driver.switchTo().defaultContent()
     //rest of code

you have to always switch to frame to interact with elements inside it

1

To get the href attribute value you can use this,

WebElement myLink = driver.findElement(By.xpath("//a[contains(@href,'Electronics')]"));
String href = myLink.getAttribute("href");

OR

WebElement myLink = driver.findElement(By.linkText("Electronics"));  
String href = myLink.getAttribute("href");

And for clicking the link you can use,

driver.findElement(By.xpath("//a[contains(@href,'Electronics')]")).click();

OR

driver.findElement(By.linkText("Electronics")).click();
0

Clicking the link:

driver.findElement(By.linkText("Electronics")).click();

Getting the HREF:

WebElement link = driver.findElement(By.linkText("Electronics"));  
String href = link.getAttribute("href");

Common reasons that Selenium does not find the elmement:

  • Test code is to fast, wait for the element to be clickable
  • Element is inside a frame, switch to the frame first

Check out this answer for a better debuging strategy: How to analyse 'Element not Found' exceptions when working with Selenium

1
  • NoSuchElementException Feb 25 at 11:49
0

While working in automation testing services, we encounter such issues quite often. In Selenium WebDriver, We can click "href" links by "linkText" or "partialLinkText" inbuilt methods. These methods are good to use if we know the expected text occurs between the anchor tag tags and the text between tags will not vary.

For example: As we perform international testing and text gets changed in that case for different languages. For an instance if we apply below method in case of English and German Language then text gets changed and link text method will not work for this case.

English:
<a href="https://www.example.com/account/profile.html">View Profile</a>
driver.findElement(By.linkText("View Profile")).click()

German:
<a href="https://www.example.com/account/profile.html">Profil ansehen</a>
driver.findElement(By.linkText("View Profile")).click()

Solution: We can resolve this by getting "href" attribute value as web driver does not have a direct way of clicking a link by its href value.

Suppose we have the below links and we want to click on the "ITprofile" link

<a href="https://example.com/account/profile.html">ITprofile</a>
<a href="https://example.com/account/transactions.html">CSEProfile</a>

Method:

public void clickLinkByHref(String href) {
    List<WebElement> anchors = driver.findElements(By.tagName("a");
    Iterator<WebElement> i = anchors.iterator();

    while(i.hasNext()) {
        WebElement anchor = i.next();
        if(anchor.getAttribute("href").contains(href)) {
            anchor.click();
            break;
        }
    }
}

clickLinkByHref("ITprofile");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.