如何查找文字'通过使用硒?

发布于 2025-02-11 14:19:00 字数 570 浏览 2 评论 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)

我的预期结果是['khung:khunggỗtthông -vạtgiườnggỗthông','da puhoặcvảibốCaocấpnhậpkhẩu','usse:d40/35 theotiêuchuẩnxuchuẩnxutkhẩu'theotiênxutkhẩu'

] //i.sstatic.net/myzhk.png“ rel =“ nofollow noreferrer”>

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笑看君怀她人 2025-02-18 14:19:00
  1. 位于< p>包含'chấtliệu',回到父母,
  2. 而标签不是< p>,而父母的类不是面板 - 体,回到父母
  3. 查找第一个< ul>,在< 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])

输出

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

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

◇流星雨 2025-02-18 14:19:00

替换下面的代码行。

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

Replace below lines of code.

des = wait.until(EC.visibility_of_element_located((By.XPATH, "//strong[text()='Chất liệu :']/.."))).Text;
print(des)
寄意 2025-02-18 14:19:00

尝试此XPath以找到包含它的文本

(by.xpath,“ // strong [包含(text(),'chấtliệu')]”);
希望它能起作用!

Try this xpath to find the text which contains it

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文