Here is the link of website from where I want to extract data,
I'm trying to get all text of href
attribute under anchor tag.
Here is the sample html:
<div id="borderForGrid" class="border">
<h5 class="">
<a href="/products/product-details/?prod=30AD">A/D TC-55 SEALER</a>
</h5>
<div id="borderForGrid" class="border">
<h5 class="">
<a href="/products/product-details/?prod=P380">Carbocrylic 3356-1</a>
</h5>
I want to extract all text values like ['A/D TC-55 SEALER','Carbocrylic 3356-1']
.
I tried with:
target = driver.find_element_by_class_name('border')
anchorElement = target.find_element_by_tag_name('a')
anchorElement.text
but it gives ''
(empty) string.
Any suggestion on how can it be achieved?
PS - Select first value of radio button under PRODUCT TYPE
If you need all links values you should be using
find_elements_....
functions, notfind_element_...
functions as the latter one will return you first single match.Recommended update for your code:
More information:
To extract all the text values within the
<a>
tags e.g. ['A/D TC-55 SEALER','Carbocrylic 3356-1'], you have to induce WebDriverWait for thevisibility_of_all_elements_located()
and you can use either of the following solutions:Using
CSS_SELECTOR
:Using
XPATH
:Note : You have to add the following imports :
Looks like when the website is first loaded all products are loaded as well. The pagination at the bottom does not actually change to different pages. Therefore you are able to extract all products on the very first request of
http://www.carboline.com/products/
. I usedpython requests
to fetch the websitesHTML
andlxml html
to parse theHTML
.I would stay away from selenium, etc.. if possible (sometimes you have no choice). But if the website is super simple like the one in your question. Then I would recommend just making a
request
. This avoids having to use a browser with all the extra overhead because you are only requesting what you need.**I updated my answer to also show you how you can extract the
href
andtext
at the same time.