0

I am trying to extract the url from an href that is very specific, this site has many html routes that are VERY! similar and the only way to extract this url is by an XPATH built in the way I am doing it.

But the big issue is the following, it changes all the time, part of the label is static but the other is dynamic and it is kind of random

The html looks like this: NOTE: page_name ="Laura" is a name I can select

# Option 1 <span label="answer by Laura to Charles"> # Option 2 <span label="answer by Laura to Nina"> # Option 3 <span label="answer by Laura to Maria"> <div > <a href="www.thisisawebsite.otherthings.blabla...> # Option n <span label="answer by Laura to THIS COULD BE ANY RANDOM NAME"> <div > <a href="www.thisisawebsite.otherthings.blabla...> 

I have tried different options:

get_comment = WebDriverWait(self.driver, 2).until( EC.presence_of_all_elements_located(( By.XPATH, r'//span[contains(text(), "answer by {}")]/div/a'.format(page_name))) )[0].get_attribute('href') 

Other try:

get_comment = WebDriverWait(self.driver, 2).until( EC.presence_of_all_elements_located(( By.XPATH, r'//span[(@label="answer by {}")]/div/a'.format(page_name))) )[0].get_attribute('href') 

    1 Answer 1

    1

    Second one should work if you change it to

    get_comment = WebDriverWait(self.driver, 2).until( EC.presence_of_all_elements_located(( By.XPATH, r'//span[contains(@label,"answer by {}")]/div/a'.format(page_name))) )[0].get_attribute('href') 

    When using '=', it searches for the exact same string. This allows you to only get part of it

    1
    • Thank you so much, this is so important for Selenium users
      – The Dan
      CommentedFeb 1, 2021 at 11:42

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.