pkk
pkk

Reputation: 379

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

Hi want to Skip the error lines of code as it is Unable to locate element. Piece of code given below , Please find profile_cover line is given error. I have added try except block to handle it still is it giving me elenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element error. Please advise

    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)

Upvotes: 1

Views: 433

Answers (1)

cruisepandey
cruisepandey

Reputation: 29362

try this instead :

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

Make sure to import these :

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

Upvotes: 1

Related Questions