【发布时间】:2021-01-29 19:04:19
【问题描述】:
我正在尝试从一个非常具体的 href 中提取 url,这个站点有许多非常非常的 html 路由!类似的,提取此 url 的唯一方法是通过我这样做的方式构建的 XPATH。
但最大的问题是,它一直在变化,标签的一部分是静态的,另一部分是动态的,有点随机
html 如下所示:
注意:page_name ="Laura" 是我可以选择的名称
# 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...>
我尝试了不同的选择:
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')
其他尝试:
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')
【问题讨论】:
标签: python html python-3.x selenium selenium-webdriver