0

Code of my function, basically it runs thru multiple links and checks for various buttons until it finds the right one.. try A, except nosuch B except nosuch C... the issue is after the second one it doesnt recognize that there is a 3rd except NoSuchElement. Did I format it wrong or something?

def followerviewer():

    user_str = " "
    followaction = 0


    for acc_len in range(len(acc_list)):
        user_str = f"{acc_list[acc_len]}"
        driver.get(f"https://instagram.com/{user_str}/")
        try:
            followbutton = driver.find_element_by_xpath('//button[text()="Requested"]')
            followaction = 0
        except NoSuchElementException:

            followbutton = driver.find_element_by_xpath('//button[text()="Message"]')
            followaction = 0

        except NoSuchElementException:
            followbutton = driver.find_element_by_xpath('//button[text()="Follow"]')
            followaction = 1



        if bool(followaction) is True:
            followbutton.click()
        else:
            print("Is already followed")

        time.sleep(0.25)




    return 

My third exception is not working, I'm getting this error..

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//button[text()="Message"]"} (Session info: chrome=84.0.4147.105)

I thought it was a syntax issue but I checked how to handle Exceptions online and its weird why it isnt working..

3
  • your first NoSuchElementException exception catches all "NoSuchElementExceptions". So your second exception won't run. You need to determine in the first exception which of the exception applies (either the "Message" one or the "Follow" one. Commented Aug 3, 2020 at 3:22
  • @ewong followbutton = driver.find_element_by_xpath('//button[text()="Message"]') can throw an exception again. Commented Aug 3, 2020 at 3:29
  • Were you expecting the second except block to catch exceptions from the first? except blocks don't chain like that. Multiple excepts on the same try are for catching different kinds of exceptions. Commented Aug 3, 2020 at 3:29

2 Answers 2

1

Try nesting a try in the except. I'm assuming here that the third except runs on the previous one failing. So try this code:

def followerviewer():
 user_str = " "
 followaction = 0


 for acc_len in range(len(acc_list)):
    user_str = f"{acc_list[acc_len]}"
    driver.get(f"https://instagram.com/{user_str}/")
    try:
        followbutton = driver.find_element_by_xpath('//button[text()="Requested"]')
        followaction = 0
    except NoSuchElementException:
       try
           followbutton = driver.find_element_by_xpath('//button[text()="Message"]')
           followaction = 0

       except NoSuchElementException:

           followbutton = driver.find_element_by_xpath('//button[text()="Follow"]')
           followaction = 1



    if bool(followaction) is True:
        followbutton.click()
    else:
        print("Is already followed")

    time.sleep(0.25)




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

1 Comment

thanks, I tried try: except: try: except, but this didnt come across, ty!
1

What you need is nested try/catch block:

    try:
        followbutton = driver.find_element_by_xpath('//button[text()="Requested"]')
        followaction = 0
    except NoSuchElementException:
        try:
            followbutton = driver.find_element_by_xpath('//button[text()="Message"]')
            followaction = 0
        except NoSuchElementException:
            followbutton = driver.find_element_by_xpath('//button[text()="Follow"]')
            followaction = 1

That way it can catch NoSuchElementException when you run find_element_by_xpath again

Comments

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.