使用-xpath-selenium-python访问标记的内容



我尝试访问页面中某些标记的内容。

但它们是无法访问的。

以下是第一行代码:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
driver = webdriver.Chrome()
url = "https://www.flashscore.com/match/hzciHz2I/#h2h;overall"
driver.get(url)

这是中间的代码(造成问题的代码(:

我做了两个版本,这里是第一个

data = WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.XPATH,
"//table[contains(@class,'h2h_mutual')]/tbody/tr[contains(@class,'highlight')]")))

这是第二个

data = WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.XPATH,
"//table[@class='h2h_mutual']/tbody/tr[@class='highlight']")))

但它没有找到标签,并返回异常"TimeoutException:">

我不明白为什么这不起作用,有人可以帮我吗?

这是代码的末尾,用于显示内容:

for elem in data:
     print(elem.text)

这是完整的第一个版本:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
driver = webdriver.Chrome()
url = "https://www.flashscore.com/match/hzciHz2I/#h2h;overall"
driver.get(url)
data = WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.XPATH,
"//table[contains(@class,'h2h_mutual')]/tbody/tr[contains(@class,'highlight')]")))
for elem in data:
     print(elem.text)

我指定这个页面是一个在点击这个页面中的链接后打开的窗口,我在两个页面之间的切换做得很好,甚至在这个页面之前对它执行了一些操作。(第一行就在那里,你可以快速复制错误(

我是这样做的:

driver.find_element(By.CSS_SELECTOR, "#li2").click()
WebDriverWait(driver, timeout=5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#li2")))
window_first = driver.window_handles[0]
link_page = WebDriverWait(driver, timeout=5).until(EC.element_to_be_clickable((By.XPATH,"//div[text()='Nantes']")))
driver.execute_script("arguments[0].click();", link_page)
Window_second = driver.window_handles[1]
driver.switch_to.window(Window_second)
driver.find_element(By.CSS_SELECTOR, "#a-match-head-2-head").click()
WebDriverWait(driver, timeout=5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#a-match-head-2-head")))

正如注释中所写,XPATH表达式返回的大多数元素都不可见,这可能是因为用户需要单击"显示更多结果",也可能是因为它们位于带有visibility: none的div中。如果您将等待更改为presence_of_all_elements_located,它应该可以工作。

data = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.XPATH,
"//table[contains(@class,'h2h_mutual')]/tbody/tr[contains(@class,'highlight')]")))

print(data[0].text)
>>> '04.02.20 L1 Nantes Paris SG 1 : 2'

一些背景:presence_of_all_elements_located检查元素是否位于网页上,即是否存在,但不需要它们可见。visibility_of_all_elements_located检查存在性和可见性,即可以说它是presence的更严格版本。

参见Selenium文档

最新更新