The Dan
The Dan

Reputation: 1690

Selenium Python: Get Xpath value selected

I am looking forward to know what is the XPATH value selected after I locate an element in selenium. I want to know because I will take a different action depending of the case, if no element is located I will rise an exception

With the following code I am locating any of the 3 cases:

get_main_1 = WebDriverWait(self.driver, 10).until(
            EC.presence_of_all_elements_located((
                By.XPATH, '//div[@role="article"] '
                          '| //span[@role="presentation"] '
                          '| //div[@aria-label="Video"] '
                          '')))[0]

If I get //div[@role="article"] I will do some action, and if I get //span[@role="presentation"] other action, and so on...

How can I get to know which of the 3 cases did I found?

Upvotes: 0

Views: 658

Answers (1)

Prophet
Prophet

Reputation: 33371

To know what element was returned you can do something like this:

get_main_1 = WebDriverWait(self.driver, 10).until(
            EC.presence_of_all_elements_located((
                By.XPATH, '//div[@role="article"] '
                          '| //span[@role="presentation"] '
                          '| //div[@aria-label="Video"] '
                          '')))[0]
role = get_main_1.get_attribute("role")
if "article" in role:
    #first element returned actions
elif "presentation" in role:
    #second element returned actions
else:
    #third element returned actions

Upvotes: 1

Related Questions