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

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

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();
    WebElement frame = driver.findElement(By.tagName("iframe"));
    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:

     //rest of code

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


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");


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

And for clicking the link you can use,




Clicking the link:


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

  • NoSuchElementException – mike tyson1 Feb 25 at 11:49

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.

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

<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>


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)) {


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.