想要跳过代码 selenium python try/except 的错误行

want to Skip the error lines of code selenium python try/except

您好想跳过代码的错误行,因为它无法定位元素。 下面给出的一段代码,请找到 profile_cover 行给出的错误。我已经添加了 try except 块来处理它,它仍然给我 elenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element 错误。请指教

    else:
        body = driver.find_elements_by_xpath("//*[@class='feed']")  
        post_type = 'Personal Profile'
        if len(body) > 0:
            body = body[0]
            post_tile = body.find_elements_by_xpath(".//*[@class='_55wo _5rgr _5gh8 async_like _1tl-']")
            print('its a normal profile page')
            try:
                profile_cover = driver.find_element_by_xpath("//*[@class='_7g4m']//*[@class='_7g2i _48kp']/a/i")
 **// GETTING ERROR HERE AND WANT TO SKIP THIS**

                ilink = profile_cover.get_attribute('style')
                ilink = ilink.split('(')[1]
                ilink = ilink.split(')')[0]
                print(ilink)
                profile_info.append(ilink)
                profile_ph = driver.find_element_by_xpath(
                    "//*[@class='_52jj _42b3']//i[@class='img profpic' or @class='img _1-yc profpic']")
                ilink = profile_ph.get_attribute('style')
                ilink = ilink.split('(')[1]
                ilink = ilink.split(')')[0]
                print(ilink)
                profile_info.append(ilink)
            except ValueError as e:
                print(e)

试试这个:

try:
    profile_cover = driver.find_element_by_xpath("//*[@class='_7g4m']//*[@class='_7g2i _48kp']/a/i")
except NoSuchElementException as exc:
    print(exc)

确保导入这些:

from selenium.common.exceptions import TimeoutException, WebDriverException, NoSuchElementException