Python Selenium使用xpath和for循环提取元素 [英] Python selenium to extract elements with xpath and for loop

查看:474
本文介绍了Python Selenium使用xpath和for循环提取元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python/Selenium从网站中提取一些文本,以便在Google表格中对其进行进一步的排序.

I am using Python/Selenium to extract some text from a website to further sort it in Google Sheets.

我需要提取15个标题.文本位于标签h5的每个标题下.

There are 15 headers for which I need to extract text. The text is found under each header in tag h5.

这是标题的一部分:

<tr class="dayHeader">
 <td colspan="7" style="padding:10px 0;">
  <hr>
  <h5>&nbsp;&nbsp;Tuesday - 02 February 2021</h5>
 </td>
</tr>

我要做的是以下事情:

headers = driver.find_elements_by_tag_name('h5')
results = []

for header in headers:
    result = header.text
    results.append(result)

我宁愿从h5处获取该标签上方的类的文本,如下所示:

I'd prefer fetching the text from h5 going by the class above this tag, like so:

headers = driver.find_element(By.XPATH,"//tr[@class='dayHeader']/h5")

并将其添加到上述的for循环中,但是我似乎无法使这一行正常工作.我该怎么办?

and add it to the mentioned for loop, but I can't seem to get this line to work. How can I do this?

推荐答案

您快到了.的问题中的/第一个孩子.但是< h5> 不是//tr [@ class ='dayHeader'] 的第一个子代.

You were almost there. / in xpath indicates first child. But the <h5> isn't the first child of //tr[@class='dayHeader'].

您需要将双正斜杠(即//)替换为单斜杠(即/),以表示后代.因此,您的有效代码行将是:

You need to replace the single forward slash i.e. / with a double forward slash i.e. // which will indicate a descendant. So your effective line of code will be:

print([my_elem.text for my_elem in driver.find_elements(By.XPATH, "//tr[@class='dayHeader']//h5")])


理想情况下,您需要诱使 WebDriverWait 用于 visibility_of_all_elements_located(),您可以使用以下定位器策略:


Ideally you need to induce WebDriverWait for visibility_of_all_elements_located() and you can use the following Locator Strategy:

print([my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//tr[@class='dayHeader']//h5")))])

这篇关于Python Selenium使用xpath和for循环提取元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆