0
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

1
  • I think you want to run wait until on a separate line and then afterwards run des = driver.find_element(By.XPATH)... Commented Jun 29, 2022 at 5:37

3 Answers 3

1
  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 '] 
Sign up to request clarification or add additional context in comments.

4 Comments

Shuo, I have another link ('modernhousevn.com/collections/new-arrival/products/…), but I can't get data by your way, please help !
404 Page Not Found. Link maybe wrong or page has been delete.
Because there is a <em> between <p> and <strong>, I add a while loop to find p.
0

Replace below lines of code.

des = wait.until(EC.visibility_of_element_located((By.XPATH, "//strong[text()='Chất liệu :']/.."))).Text; print(des) 

2 Comments

Thanks for your reply, but it's not worked !
You have to use FindElements method to get all webelements Test and use below xpath to get all elements //strong[text()='Chất liệu :']/../following-sibling::ul/li
0

Try this xpath to find the text which contains it

(By.XPATH,"//strong[contains(text(),'Chất liệu')]"); hope it will work!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.