0

I have the following code:

from selenium.webdriver import Firefox
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains
from selenium.common.exceptions import NoSuchElementException
import time

while True:
    try:
        
        url = 'https://finance.yahoo.com/'
        driver_path = 'C:\\Users\\inder\\geckodriver.exe'
        
        browser = Firefox(executable_path = driver_path)
        browser.get(url)
        
        search_field_id = 'yfin-usr-qry'
        
        element_search_field = browser.find_element_by_id(search_field_id)
        element_search_field.clear()
        element_search_field.send_keys('TSLA')
        element_search_field.send_keys(Keys.ENTER)
        time.sleep(5)
        xpath_string = '/html/body/div[1]/div/div/div[1]/div/div[2]/div/div/div[6]/div/div/section/div/ul/li[2]/a/span'
        element = browser.find_element_by_xpath(xpath_string)
        action_chains = ActionChains(browser)
        action_chains.move_to_element(element).click().perform()
        break
    except NoSuchElementException as e:
        print(e)
        pass
    
    
   

This code works fine what i am trying to do is if for some reason if the page has not loaded correctly, i would like the entire code block to run again. To do this i have identified the search field element_search_field. If the element is not found ordinarily i would get an error:

NoSuchElementException: Unable to locate element: /html/body/div[1]/div/div/div[1]/div/div[2]/div/div/div[6]/div/div/section/div/ul/li[2]/a/span

However when i try to catch this exception with my above code i am getting an error:

NameError: name 'NoSuchElementException' is not defined

Please advise what can I do to resolve this issue.

2
  • Seems like you are missing some imports. Can you check? Commented Aug 5, 2021 at 1:15
  • This is my entire code. Which imports are you referring to? Commented Aug 5, 2021 at 1:18

2 Answers 2

1

Do you use pycharm? If you use pycharm,It will remind you that the code is wrong You should do this.

from selenium.common.exceptions import NoSuchElementException

And i can't find the declare variable action_chains in your code.It's like a mistake

Sign up to request clarification or add additional context in comments.

1 Comment

Are there any other problem?I recommend initializing at the beginning rather than in while. like browser = Firefox(executable_path = driver_path)... you just want to retry the action when it failed. There is another method to do this. def a(): try: print() except Exception as e: print(e) a()
1

I haven't had this problem myself but the Selenium documentation lists the common exceptions. So maybe you can try:

from selenium.common.exceptions import NoSuchElementException

4 Comments

The code did not break and is running in loop. Shoulnt it break ?
Hmm, it looks like you are using the pass keyword in the exception handler which means the loop will keep executing until the exception is no longer raised. Perhaps this means that your code cannot find the element it needs in the try block? What IDE are you using? If you use spyder you can set a breakpoint and step through code to see if you ever reach the break line. I hope this helps!
Spyder. i even introduced sleep for it to load. But still it loads multiple instances before it finally shuts down
I think it may be that the line: xpath_string = '/html/body/div[1]/div/div/div[1]/div/div[2]/div/div/div[6]/div/div/section/div/ul/li[2]/a/span' doesn't work the way you expect? Try setting a breakpoint and stepping through the code. If it continues looping several times and keeps raising the error then it is never able to find the element. Try this approach while also visually referencing the html you are scraping in the Dev console of your browser. If you can confirm the element you need is there but this line still throws the error then you will need to change the xPath

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.