如果您访问此网站,https://www.premierleague.com/match/66686,然后按统计选项卡,您将看到有关比赛的一些信息。我该如何为两队争取控球权?
这不起作用。
stats = driver.find_element(By.XPATH, '//*[@id="mainContent"]/div/section[2]/div[2]/div[2]/div[1]/div/div/ul/li[3]')
stats.click()
posHome = driver.find_element(By.XPATH,'//body[1]/main[1]/div[1]/section[2]/div[2]/div[2]/div[2]/section[3]/div[2]/div[2]/table[1]/tbody[1]/tr[1]/td[1]')
print(posHome.text)
posAway = driver.find_element(By.XPATH,'//*[@id="mainContent"]/div/section[2]/div[2]/div[2]/div[2]/section[3]/div[2]/div[2]/table/tbody/tr[1]/td[3]')
print(posAway.text)
请告诉我如何解决这个问题,谢谢!
要打印两队的 Possession,您需要为 visibility_of_element_ located() 引入 WebDriverWait,并且您可以使用以下 定位器策略:
代码块:
driver.get("https://www.premierleague.com/match/66686")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Accept All Cookies']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//li[text()='Stats']"))).click()
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "tbody.matchCentreStatsContainer>tr>td>p"))).text)
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "tbody.matchCentreStatsContainer>tr>td:nth-child(3)>p"))).text)
driver.quit()
控制台输出:
33.9
66.1
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
中找到相关讨论