Home > Software engineering >  Python Selenium How to take grandchild text
Python Selenium How to take grandchild text

Time:03-18

I am working with this website= enter image description here

and the HTML for the price looks like this:

enter image description here

I am looking to store the prices for each product, but it seems like I have to select the parent element based on the text "Optimal Care", "Classic Case", etc.. and go down to the child element to extract the price. I am not sure how to do this, my current code for this specific scenario:

price_optimal = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//form[@ and @text="Optimal Care"]/*/*/'))).get_attribute("text()")

How would I take a text based on a parent element text?

Thanks

CodePudding user response:

If you want to only retrieve price then you can use the below XPath:

//div[@class='summary-table-price price-monthly']//div[2]

with find_elements or with explicit waits.

  1. With find_elements

Code:

for price in driver.find_elements(By.XPATH, "//div[@class='summary-table-price price-monthly']//div[2]"):
    print(price.get_attribute('innerText'))
  1. With ExplicitWaits:

    for price in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@class='summary-table-price price-monthly']//div[2]"))):
        print(price.get_attribute('innerText'))
    

Imports:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

If you want to extract based on the plan name such as Classic Care

You should use the below XPath:

//div[text()='Classic Care']//following-sibling::div[@class='summary-table-price price-monthly']/descendant::div[2]

and use it like this:

print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[text()='Classic Care']//following-sibling::div[@class='summary-table-price price-monthly']/descendant::div[2]"))).get_attribute('innerText'))

Also, for other plans, all you will have to do is to replace Classic care with Optimal Care or Basic care in the XPath.

  • Related