0

enter image description here

I am unable to locate the element having anchor tag with href link(Monthly link in loser tab - check attached image). Getting NoSuchElementFound exception. Already tried using javascript executor and waits .

HTML :

<div style="width:50%;font-size:14px;float:right;">
    <h2 class="f16 bold">Losers</h2>    
    <div class="tabs">
<a href="//money.rediff.com/losers/bse/daily">Daily</a>
</div>
    <div class="tabs">
<a href="//money.rediff.com/losers/bse/weekly">Weekly</a>
</div>
    <div class="tabs">
<a href="//money.rediff.com/losers/bse/monthly">Monthly</a>
</div>
</div>
P K
  • 1
  • 7
  • 2
    Can you post your html? – KunduK Jan 31 '19 at 13:05
  • ...and post the code you have tried. Add a tag for the relevant programming language also. – JeffC Jan 31 '19 at 18:05
  • Possible duplicate of [How to handle iframe in Selenium WebDriver using java](https://stackoverflow.com/questions/9942928/how-to-handle-iframe-in-selenium-webdriver-using-java) – JeffC Jan 31 '19 at 18:06

4 Answers4

0

You can try finding the component using xpath. Try the code below. This might help you

driver.findElement(By.xpath("//div[contains(@id,'leftcontainer')]/div[3]/div[3]/a"));
sp324
  • 285
  • 2
  • 20
0

To click() on the element Losers -> Monthly as the the desired elements are within an <iframe> so you have to:

  • Induce WebDriverWait for the desired frame to be available and switch to it.
  • Induce WebDriverWait for the desired element to be clickable.
  • You can use the following Python based solution:

    • Code Block:

      from selenium import webdriver
      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.support import expected_conditions as EC
      from selenium.webdriver.common.by import By
      
      driver = webdriver.Firefox(executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe')
      driver.get("https://money.rediff.com/losers/bse/monthly")
      WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@id='logwatch']")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//h2[contains(., 'Losers')]//following::div[@class='curLink tabs']/a[contains(., 'Monthly')]"))).click()
      
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

Best practice in selenium for this case, is to use linkText (org.openqa.selenium.By.linkText) which you pass the text inside the a tag, and will return the equals match. For proper usage, you can use chain also, here is a simple example:

import org.openqa.selenium.support.pagefactory.ByChained;
import org.openqa.selenium.By;
import static org.openqa.selenium.By.linkText;
import static org.openqa.selenium.By.cssSelector;

public static By chain(final By... bys) {
        return new ByChained(bys);
    }

public void yourClickMethodOnAElements(final String name) {
        click(chain(cssSelector("div.tabs > a"), linkText(name)));
    }
public WebElement click(final By by) {
        return click(driver.findElement(by));
    }

public WebElement click(WebElement element) {
        Actions action = new Actions(driver);
        action.moveToElement(element).click().perform();
        sleepIfNeeded();
        return element;
    }

This is a simplified version, you can improve it with waiters checks etc etc.

0

Simple xpath example for <a href="foo">

"//a[contains(@href,'foo')]"
pburgr
  • 1,722
  • 1
  • 11
  • 26