0

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?

1
  • please share a link to that page or at least HTML of the relevant elements containing both the selected element and not selected element Commented Oct 13, 2021 at 15:50

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.