Home > database >  Selenium how to get text only from <b> tag inside class
Selenium how to get text only from <b> tag inside class

Time:05-08

I am trying to get all product prices from a site into a list using python and selenium, but I am facing a problem when the price is discounted and looks like this on the site:

discounted price

HTML code for this site:

<span >
  <b>
    79,00
    ::after
  </b>
  <del>
    89,00
    ::after
  </del>
</span>

My selenium code for getting prices into list:

def test5():
    sort = driver.find_element(By.CSS_SELECTOR, ".sorter-button").click()
    sort2 = driver.find_element(By.CSS_SELECTOR, ".sorter-listing > span:nth-child(3) > label:nth-child(2)").click()
    prices = driver.find_elements(By.CLASS_NAME, "product-price ")
    a = []
    for i in prices:
        a.append(i.text)
    print(a)

The output of this code is:

['19,90', '50,00', '78,99', '79,00\n89,00', '95,00\n128,00', '135,00', '137,95', '149,00\n158,00', '191,00', '199,00', '199,99\n267,50', '200,00\n300,00']

The problem is getting both discounted and normal values, for example - 79,00\n89,00

How can I get only discounted prices so list would look like this:

['19,90', '50,00', '78,99', '79,00', '95,00', '135,00', '137,95', '149,00', '191,00', '199,00', '199,99', '200,00']

If I do it, for example by XPATH, then I can get only one price from site, not all.

CodePudding user response:

 a.append(i.text.splitlines[1])
  • Related