Skip to content
Advertisement

Python selenium: finding multiple elements with partially different names

I have a webpage full of elements that consists below example (gave 2, the webpage consists of around >10), I want to search for all of the below elements containing ‘suda-data’ and click on all of them. Howvever I am unable to define the finding of all the elements properly.

Notes:

a. cannot search by class=”S_txt2″ (will include elements that is out of the criteria)

b.The numbers at the back of ‘suda-data’ changes everytime the page is refreshed.

<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>

Any way that i can find all the elements containing this? Thanks for the help.

Advertisement

Answer

Try this on your actual html and see if it works:

targets = browser.find_elements_by_xpath('//a[contains(@suda-data,"smart_feed")]')
for target in targets:
    print(target.get_attribute('suda-data'))

For a page containing just the two <a> elements in your question, the output should be:

key=smart_feed&value=time_sort_comm:4611520921076523
key=smart_feed&value=time_sort_comm:4612135415451073
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement