Ad

How To Get The Elements Inside Of The Anchor Tag?

Pardon me i am very new to web scraping in Selenium and python. I am trying to scrape the contents of supermarket website which has the following part in html

<div class="itemDescription">
            <meta itemprop="priceCurrency" content="INR">
            <meta itemprop="price" content="23.00">
        <h4 class=""><strong class="price js-effective-mrp" data-currency="₹">₹ 23.00 </strong>
                                    <s class="js-actual-mrp" style="display:none;"></s>
                                <br><a target="_blank" rel="nofollow noreferrer" href="/fresh-onion-red-v-1-kg-p.php" class="">Fresh Onion Red <span class="item-quantity">1 Kg</span></a></h4>
                    </div>

I needed the prices, quantity and name of the product.

Below is the code that i have written but it doesnt parse the elements correctly.

div = driver.find_element_by_class_name('itemDescription')
sname =div.find_element_by_css_selector('a').get_attribute('href')
squantity =driver.find_elements_by_class_name('item-quantity')
sprice = driver.find_elements_by_xpath('//*[contains(concat( " ", @class, " " ), concat( " ", "js-effective-mrp", " " ))]')

Please help

Ad

Answer

Try this xPath fro price:

//strong[@class='price js-effective-mrp' and @data-currency='₹']

or if you want all currencies:

//strong[@class='price js-effective-mrp']

This for link:

//div[@class='itemDescription']//a

And this for quantity:

//span[@class = 'item-quantity']

Example:

sname = driver.find_element_by_xpath("//div[@class='itemDescription']//a")
squantity = driver.find_element_by_xpath("//span[@class = 'item-quantity']")
sprice = driver.find_element_by_xpath("//strong[@class='price js-effective-mrp' and @data-currency='₹']")

print(squantity.text) # prints quantity
print(sname.text) # prints name
print(sprice.text) # prints price

According your feedback, you cannot get text from a list, but you can get a text from every element in the list like this:

sname_list = driver.find_elements_by_xpath("//div[@class='itemDescription']//a")
for sname in sname_list:
    print(sname.text) # print the text of every element in the list
Ad
source: stackoverflow.com
Ad