硒:让孩子上特定的课

时间:2018-11-17 12:55:03

标签: python selenium

给出一个子元素e,有没有一种好的(或Pythonic的)方法来找到所有带有标签div和一个类cls的孩子?我已经尝试过e.find_element_by_xpath('div[starts-with(@class, "cls")]'),但似乎没有在e中进行搜索。似乎返回了树中的第一个匹配项。

1 个答案:

答案 0 :(得分:1)

尝试以下

children = e.find_elements_by_xpath('./div[contains(@class, "cls")]')

请注意,您需要使用./启动XPath,这意味着当前节点的直接子节点(e

如果元素是由JavaScript动态生成的,请尝试等待它们出现在DOM中:

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

children = wait(e, 10).until(EC.presence_of_all_elements_located((By.XPATH, './div[contains(@class, "cls")]')))
相关问题