astonle
astonle

Reputation: 103

How to find 'Text' by using selenium?

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

Upvotes: 0

Views: 87

Answers (3)

Shuo
Shuo

Reputation: 2538

  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 ']

Upvotes: 1

B&#226;l&#224;
B&#226;l&#224;

Reputation: 51

Try this xpath to find the text which contains it

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

Upvotes: 0

Ketan Pardeshi
Ketan Pardeshi

Reputation: 812

Replace below lines of code.

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

Upvotes: 0

Related Questions