获取具有相同类名的多个元素的列表
Get the List of Multiple Element with Same ClassName
我想从WTO官方页面抓取每一个专家组报告已经组成的案例。
和
如上图所示(或参考
https://www.wto.org/english/tratop_e/dispu_e/dispu_status_e.htm,
每个案例都用 "DS XXX" 索引,在下面它表示是 "Panel Composed" 还是 "in Consultation"。
如果我检查,它们都共享相同的
<p class = "panel-text-simple">
所以我尝试了以下两个命令:
elem_info = driver.find_element_by_class_name("panel-title-simple")
elem_info = driver.find_element_by_xpath("//p[@class='panel-title-simple']");
但他们每个人都只给我最上面的情况,最近的一个。
我必须找到每个案例的信息,然后应该做一个for循环来检查面板是否组成。
我该怎么做?
您可以使用下面的 XPath 获取当前状态为 'Panel composed'
的所有 LI
//li[.//p[contains(.,'Panel composed')]]
从那里您可以获得 DS 编号
.//small
或详情
./p
等等。
使用find_elements(注意's')。这个 returns 一个你可以循环遍历的列表:
documents = driver.find_elements_by_class_name("panel-title-simple");
for document in documents
# continue with your code
我想从WTO官方页面抓取每一个专家组报告已经组成的案例。
和
如上图所示(或参考
https://www.wto.org/english/tratop_e/dispu_e/dispu_status_e.htm,
每个案例都用 "DS XXX" 索引,在下面它表示是 "Panel Composed" 还是 "in Consultation"。
如果我检查,它们都共享相同的
<p class = "panel-text-simple">
所以我尝试了以下两个命令:
elem_info = driver.find_element_by_class_name("panel-title-simple")
elem_info = driver.find_element_by_xpath("//p[@class='panel-title-simple']");
但他们每个人都只给我最上面的情况,最近的一个。
我必须找到每个案例的信息,然后应该做一个for循环来检查面板是否组成。
我该怎么做?
您可以使用下面的 XPath 获取当前状态为 'Panel composed'
的所有LI
//li[.//p[contains(.,'Panel composed')]]
从那里您可以获得 DS 编号
.//small
或详情
./p
等等。
使用find_elements(注意's')。这个 returns 一个你可以循环遍历的列表:
documents = driver.find_elements_by_class_name("panel-title-simple");
for document in documents
# continue with your code