https://www.espncricinfo.com/player/aamer-jamal-793441这是URL,我正在尝试访问全名“Aamer Jamal”。借助于Selify Web驱动程序。但我不知道为什么它会给
NoSuchElementException
`代码如下:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium_firefox import Firefox
import time
import pandas as pd
driver = webdriver.Firefox()
# Reach to the Landing page
driver.get('https://www.espncricinfo.com/player/aamer-jamal-793441')
driver.maximize_window()
time.sleep(25)
not_now = driver.find_element(By.ID, 'wzrk-cancel')
not_now.click()
fullname = driver.find_element(By.XPATH, '/html/body/div[1]/section/section/div[4]/div[2]/div/div[1]/div/div/div[1]/div[1]/span/h5')
print(fullname.text)`
错误:
NoSuchElementException: Message: Unable to locate element: /html/body/div[1]/section/section/div[4]/div[2]/div/div[1]/div/div/div[1]/div[1]/span
1条答案
按热度按时间3pmvbmvn1#
您必须使用
WebDriverWait
expected_conditions
显式等待,而不是长时间的硬编码暂停。您还必须学习如何创建正确的定位器。长的绝对XPath和CSS选择器是极易破坏的。以下代码可以正常工作:输出为: