2 回答

TA貢獻1789條經(jīng)驗 獲得超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(). 請參閱文檔。如果您嘗試在它們存在但不可見時抓取頁面,則會引發(fā)異常。
您更新的代碼如下
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條經(jīng)驗 獲得超7個贊
試試這個 xpath-
//div[contains(text(),'Manchester Utd')]/following-sibling::span
添加回答
舉報