Python selenium:查找名称部分不同的多个元素

Python selenium:查找名称部分不同的多个元素,python,selenium,web-scraping,xpath,Python,Selenium,Web Scraping,Xpath,我有一个充满元素的网页,包含以下示例(给出2个,该网页包含大约>10个元素),我想搜索包含“suda数据”的以下所有元素,然后单击所有元素。然而,我无法正确定义所有元素的查找 注: a。无法按class=“S_txt2”搜索(将包括超出条件的元素) b、 每次刷新页面时,“suda data”后面的数字都会更改 <a suda-data="key=smart_feed&amp;value=time_sort_comm:4611520921076523" href

我有一个充满元素的网页,包含以下示例(给出2个,该网页包含大约>10个元素),我想搜索包含“suda数据”的以下所有元素,然后单击所有元素。然而,我无法正确定义所有元素的查找

注:

a。无法按class=“S_txt2”搜索(将包括超出条件的元素)

b、 每次刷新页面时,“suda data”后面的数字都会更改

<a suda-data="key=smart_feed&amp;value=time_sort_comm:4611520921076523" href="javascript:void(0);" class="S_txt2" action-type="fl_comment" action-data="ouid=6430256035&amp;location=page_100808_super_index"><span class="pos"><span class="line S_line1" node-type="comment_btn_text"><span><em class="W_ficon ficon_repeat S_ficon"></em><em>14</em></span></span></span></a>

<a suda-data="key=smart_feed&amp;value=time_sort_comm:4612135415451073" href="javascript:void(0);" class="S_txt2" action-type="fl_comment" action-data="ouid=7573331386&amp;location=page_100808_super_index"><span class="pos"><span class="line S_line1" node-type="comment_btn_text"><span><em class="W_ficon ficon_repeat S_ficon"></em><em> 183</em></span></span></span></a>


我能找到包含这个的所有元素吗?谢谢你的帮助。

有一种更简捷的方法,我可以马上想到

首先,获取带有“a”标记的所有元素的列表

其次,通过过滤只查看包含“suda数据”的数据,并将其保存在列表中,使其在每次刷新网站时随数字动态变化

<a suda-data="key=smart_feed&amp;value=time_sort_comm:4611520921076523" href="javascript:void(0);" class="S_txt2" action-type="fl_comment" action-data="ouid=6430256035&amp;location=page_100808_super_index"><span class="pos"><span class="line S_line1" node-type="comment_btn_text"><span><em class="W_ficon ficon_repeat S_ficon"></em><em>14</em></span></span></span></a>

第三,做一个soup.find(“a”,{“suda data”:suda data instance}),其中suda data instance是步骤2中列表的一个元素。

在实际html上尝试一下,看看它是否有效:

targets = browser.find_elements_by_xpath('//a[contains(@suda-data,"smart_feed")]')
for target in targets:
    print(target.get_attribute('suda-data'))
对于仅包含问题中两个
元素的页面,输出应为:

key=smart_feed&value=time_sort_comm:4611520921076523
key=smart_feed&value=time_sort_comm:4612135415451073