Search code examples
pythonseleniumtags

How to get acces to the second <p> in selenium (Python)?


I'm new in Python and Selenium. I have this code:

<div class="Product_ProductInfo__23DMi">
   <p style="font-weight: bold;">4.50</p>
   <p>Bread</p>
   <p>390 g</p>
</div>

I want to get access to the second <p> tag and get its value (I mean Bread).

For the first <p> tag, I used:

self.driver.find_element_by_xpath('//div[@class="Product_ProductInfo__23DMi"]/p')

But I don't know how to get to the other one.

Thanks.


Solution

  • You can do that using find_elements_by_css_selector() function and then selecting the second element of it.

    a = self.webdriver.find_element_by_css_selector('div[class="Product_ProductInfo__23DMi"]')
    second_p = a.find_elements_by_css_selector('p')[1]