Reputation: 21
I need to get all the text inside the <a>
tags, but I can get only the text of the first <a>
tag.
Here is my code:
table_page = driver2.find_elements_by_class_name('paging')
for tn in table_page:
num = tn.find_element_by_class_name('paper.page_link')
print(num.text)
and this is the HTML code:
<div class="paging">
<em class=" link_page" style="cursor:pointer"> 1 </em>
<a class="paper page_link" href="javascript:" url="./detail_nbid" page_num="2">2</a>
<a class="paper page_link" href="javascript:" url="./detail_nbid" page_num="3">3</a>
</div>
what I expected is 2, 3
but I get only 2
.
Upvotes: 1
Views: 112
Reputation: 5237
If you select by class name, then you can only specify a class name. This doesn't give you the required flexibility you need here.
Try selecting by CSS Selector.
Here, you are interested in a
tags with the class
's paper
and page_link
.
For example:
for a in driver.find_elements_by_css_selector('a.paper.page_link'):
print(a.text)
Output:
2
3
Upvotes: 1
Reputation: 9969
If you want to print all a tags in all paging classes.
table_page = driver2.find_elements_by_class_name('paging')
for tn in table_page:
nums = tn.find_elements_by_class_name('paper.page_link')
for num in nums:
print(num.text)
Otherwise
table_page = driver2.find_element_by_class_name('paging')
nums = table_page.find_elements_by_class_name('paper.page_link')
for num in nums:
print(num.text)
Upvotes: 0
Reputation: 693
There is typo in your html code. in href your " are not matching.
<div class="paging">
<em class="link_page" style="cursor:pointer;"> 1 </em>
<a class="paper page_link" href="javascript:url='./detail_nbid'" page_num="2">2</a>
<a class="paper page_link" href="javascript:url='./detail_nbid'" page_num="3">3</a>
</div>
Upvotes: 0