How to find 'Text' by using selenium?

Issue

driver = webdriver.Chrome()
URL= 'https://modernhousevn.com/collections/new-arrival/products/giuong-ngu-mh-21'
driver.get(URL)
sleep(1)
des = wait.until(EC.visibility_of_element_located((By.XPATH, "//strong[text()='Chất liệu :']/.."))).get_attribute('innerText')
print(des)

My expected result is [‘Khung : Khung Gỗ thông – Vạt giường gỗ thông’, ‘Da PU hoặc vải bố cao cấp nhập khẩu’, ‘Mousse : D40/35 theo tiêu chuẩn xuất khẩu’]

enter image description here

Solution

  1. Located <p> contain 'Chất liệu', back to parent
  2. While tag is not <p> and parent’s class is not panel-body, back to parent
  3. Find first <ul>, find all <li> in the <ul>
sleep(1)
target = driver.find_element(By.XPATH, "//strong[contains(text(),'Chất liệu')]/..")
while target.tag_name != 'p' and target.find_element(By.XPATH, "./..").get_attribute("class") != 'panel-body':
    target = target.find_element(By.XPATH, "./..")
des = target.find_elements(By.XPATH, './following-sibling::ul[1]/li')
print([d.text for d in des])

output

['Khung : Khung Gỗ thông - Vạt giường gỗ thông', 'Da PU hoặc vải bố cao cấp nhập khẩu', 'Mousse : D40/35 theo tiêu chuẩn xuất khẩu ']

Answered By – Shuo

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published