简体   繁体   中英

Selenium & Python: Finding elements with dynamic XPATH

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')

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM