0

I'm trying to get this conditional working but I'm getting traceback. I want to look if the element is present on the website and if it is refresh and execute redeem_func() and if the element is not present at all I want to not execute err_reddem_func() and move on to this By the way, I don't know if it's relevant but if there was no error on the webpage it redirects to the last step website and saves URL to txt.

Exception in thread Thread-6:
Traceback (most recent call last):
  File "C:\Users\lulu\AppData\Local\Programs\Python\Python37-32\lib\threading.py", line 917, in _bootstrap_inner
    self.run()
  File "C:\Users\lulu\AppData\Local\Programs\Python\Python37-32\lib\threading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
  File "c:/Users/lulu/Desktop/s/s/threaddo.py", line 352, in execute_chrome
    s(elem[0], elem[1], elem[2])
  File "c:/Users/lulu/Desktop/s/s/threaddo.py", line 323, in s
    err_redeem_func()
  File "c:/Users/lulu/Desktop/s/s/threaddo.py", line 314, in err_redeem_func
    err_redeem = driver.find_element_by_class_name('error')
  File "C:\Users\lulu\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 564, in find_element_by_class_name    return self.find_element(by=By.CLASS_NAME, value=name)
  File "C:\Users\lulu\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
    'value': value})['value']  File "C:\Users\lulu\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\lulu\AppData\Local\Programs\Python\Python37-32\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":"class name","selector":"error"}  (Session info: chrome=74.0.3729.131)
  (Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}),platform=Windows NT 10.0.17763 x86_64)

My Code

def redeem_func():
        capHome = cap_home()
        print("redeem_func")
        driver.execute_script("redemptionValidation(\"" + capHome + "\")")
        time.sleep(10)


    redeem_func()
    time.sleep(1)
    print(driver.current_url)

    def err_redeem_func():
        err_redeem = driver.find_element_by_class_name('error')
        try:
            if err_redeem.is_displayed() and err_redeem.is_enabled():
                driver.refresh()
                redeem_func()
        except NoSuchElementException:
            pass


    err_redeem_func()

    print(driver.current_url)
    print("SAVING TO TXT")
    finalCode = driver.current_url
    f = open('t.txt','a')
    f.write('\n' + finalCode)
    f.close()

1 Answer 1

1

Replace the below line

if err_redeem.is_displayed() and err_redeem.is_enabled():

with

if (len(driver.find_elements_by_class_name('error'))>0):

You are getting NoSuchElement exception because the script is trying to check if the element is enabled when the element is not there on the page.

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

1 Comment

But the new code doesn't check if the element is visible. You can add that after the len() check and it won't throw if the element isn't there.

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.