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..
NoSuchElementExceptionexception 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.followbutton = driver.find_element_by_xpath('//button[text()="Message"]')can throw an exception again.exceptblock to catch exceptions from the first?exceptblocks don't chain like that. Multipleexcepts on the sametryare for catching different kinds of exceptions.