第三个异常不执行Python try/except

第三个异常不执行Python try/except,python,exception,Python,Exception,我的函数代码,基本上它通过多个链接运行,并检查各种按钮,直到找到正确的一个。。试试A,除了B,除了C。。。问题是在第二个问题之后,它不承认有第三个问题,除了非接触元素。我把它格式化错了吗 def followerviewer(): user_str = " " followaction = 0 for acc_len in range(len(acc_list)): user_str = f"{acc_list[acc_

我的函数代码,基本上它通过多个链接运行,并检查各种按钮,直到找到正确的一个。。试试A,除了B,除了C。。。问题是在第二个问题之后,它不承认有第三个问题,除了非接触元素。我把它格式化错了吗

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 
我的第三个例外不起作用,我得到了这个错误

selenium.common.exceptions.NoSuchElementException:Message:没有这样的元素:无法找到元素:{“方法”:“xpath”,“选择器”:“//button[text()=”Message“]”} (会话信息:chrome=84.0.4147.105)


我认为这是一个语法问题,但我检查了如何在线处理异常以及它为什么不工作的奇怪原因。

您需要的是嵌套的try/catch块:

    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

这样,当您再次运行
find\u element\u by_xpath
时,它可以捕获
NoSuchElementException
,您需要的是嵌套的try/catch块:

    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

这样,当您再次运行
find_element_by_xpath
时,它可以捕获
NoSuchElementException
,尝试在except中嵌套一个Try。我在这里假设第三个除了在前一个失败的基础上运行。因此,请尝试以下代码:

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 

尝试在“例外”中嵌套一次尝试。我在这里假设第三个除了在前一个失败的基础上运行。因此,请尝试以下代码:

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 

第一个
NoSuchElementException
异常捕获所有“NoSuchElementException”。所以你的第二个异常不会运行。您需要在第一个异常中确定应用哪一个异常(“消息”异常或“跟随”异常。@ewong
followbutton=driver。通过xpath查找元素('//button[text()=“Message”]'))
可以再次抛出异常。您是否希望第二个
except
块从第一个块捕获异常?
except
块不会像那样连锁。同一个
try
上的多个
except
用于捕获不同类型的异常。您的第一个
NoTouchElementException
异常捕获所有“NoSuchElementExceptions”。因此,您的第二个异常将不会运行。您需要在第一个异常中确定应用哪一个异常(“Message”异常或“followbutton”异常。@ewong
followbutton=driver.find_element_by_xpath('//button[text()=“Message”]))
可以再次抛出异常。您是否希望第二个
except
块捕获第一个块中的异常?
except
块不会像那样连锁。同一个
try
上的多个
except
用于捕获不同类型的异常。谢谢,我尝试了try:except:except,但这没有出现罗斯,泰!谢谢,我试过了,除了,但没想到,泰!