我正在尝试从非常具体的href提取网址,该网站有很多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')
如果将其更改为第二个,则应该可以使用
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')
使用“ =”时,它将搜索完全相同的字符串。这使您只能得到一部分
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句