2 回答
TA贡献1789条经验 获得超8个赞
您正在寻找的定位器是
//div[contains(@class,'event__participant--home')][text()='Manchester Utd']//following-sibling::span[1]
^ find a DIV that contains the class indicating a home game
^ that also contains the team name
^ then find the first sibling SPAN that follows
该定位器将找到仅包含主场比赛的 L、W、D 等元素。
如果您要等待元素,您将需要等待可见,而不是存在。存在是指元素仅位于 DOM 中但不一定可见。如果要从页面上刮掉文本,则需要等待可见。您可以使用EC.visibility_of_all_elements_located(). 请参阅文档。如果您尝试在它们存在但不可见时抓取页面,则会引发异常。
您更新的代码如下
driver = webdriver.Chrome()
url = "https://www.flashscore.com/team/manchester-united/ppjDR086/results/"
driver.get(url)
Team = 'Manchester Utd'
results = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH,"//div[contains(@class,'event__participant--home')][text()='" + Team + "']//following-sibling::span[1]")))
print(len(results))
TA贡献1835条经验 获得超7个赞
试试这个 xpath-
//div[contains(text(),'Manchester Utd')]/following-sibling::span
添加回答
举报