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.
JavaScript
x
2
1
<a suda-data="key=smart_feed&value=time_sort_comm:4611520921076523" href="javascript:void(0);" class="S_txt2" action-type="fl_comment" action-data="ouid=6430256035&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>
2
…….
JavaScript
1
2
1
<a suda-data="key=smart_feed&value=time_sort_comm:4612135415451073" href="javascript:void(0);" class="S_txt2" action-type="fl_comment" action-data="ouid=7573331386&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>
2
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:
JavaScript
1
4
1
targets = browser.find_elements_by_xpath('//a[contains(@suda-data,"smart_feed")]')
2
for target in targets:
3
print(target.get_attribute('suda-data'))
4
For a page containing just the two <a>
elements in your question, the output should be:
JavaScript
1
3
1
key=smart_feed&value=time_sort_comm:4611520921076523
2
key=smart_feed&value=time_sort_comm:4612135415451073
3