0

So the issue is very simple and straightforward. In the link https://www.nasdaq.com/symbol/iff/revenue-eps

I want to click the link "Previous 3 Years" using selenium, but it just doesnt seem to work.

The code below is what I attempted. It worked for most webpages, but not for this for some reason. I tried to find the element by the text it holds and click it.

link = driver.find_element_by_link_text('Previous 3 Years')
link.click()

And when I did eventually run it, this is the error message I receive:

Traceback (most recent call last):
  File "writeTest.py", line 68, in <module>
    link = driver.find_element_by_link_text('Previous 3 Years')
  File "C:\Users\hp\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 428, in find_element_by_link_text
    return self.find_element(by=By.LINK_TEXT, value=link_text)
  File "C:\Users\hp\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
    'value': value})['value']
  File "C:\Users\hp\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\hp\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"Previous 3 Years"}
  (Session info: headless chrome=76.0.3809.100)

Can anyone please tell me what seems to be the issue here?

2 Answers2

0

Your Object is in iframe so you need to switch it You need To Write You code Like This

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time


WebDriverWait(driver,20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"#frmMain")))

link = driver.find_element_by_link_text('Previous 3 Years')
link.click()

For Switching

driver.switch_to.default_content()
Hamza Lachi
  • 1,046
  • 7
  • 25
0

Because targeted element is inside the iframe. That's why it can't be found, since you are working on the main content. Below code should work for you:

from selenium.webdriver.common.by import By

revenue_table = driver.find_element(By.ID, value="frmMain")
driver.switch_to.frame(revenue_table)
# Your code
link = driver.find_element_by_link_text('Previous 3 Years')
link.click()

# You can switch back to the main content by below code:
driver.switch_to.default_content()
Elgin Cahangirov
  • 1,932
  • 4
  • 24
  • 45