Python:Xpath无法定位元素



我试图从网站获取一些数据,但出现以下错误。它昨晚起了作用,但当我今天重新运行时,它突然找不到元素了。今天,我几乎试过了,但没能解决。

工具和语言-Python,Selenium,Chrome,Chromedriver,AWS Cloud 9,EC2

from selenium import webdriver
import time
from selenium.webdriver.chrome.options import Options
options = Options()
options.headless = True
driver = webdriver.Chrome(options=options)

driver.get('https://www.espncricinfo.com/series/19496/scorecard/1198235/england-vs-australia-1st-t20i-england-v-australia-2020')
time.sleep(20)
element_text = driver.find_element_by_xpath('//*[@id="main-container"]/div/div[2]/div[2]/div/div[1]/div[1]/div[1]/div[1]/div[1]/div[2]').text
print(element_text)

错误消息

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="main-container"]/div/div[2]/div[2]/div/div[1]/div[1]/div[1]/div[1]/div[1]/div[2]"}

我试过下面的东西

  1. 添加和删除睡眠时间。睡眠时间增加和减少
  2. 已使用完整Xpath,Xpath,按类查找
  3. 尝试定位不同的元素
  4. 不同的页面

被引用到各个网站仍然无法解决。我是蟒蛇的新手。

试试这个:

import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.headless = True
driver = webdriver.Chrome(options=options)
url = 'https://www.espncricinfo.com/series/19496' 
'/scorecard/1198235/england-vs-australia-1st-t20i-england-v-australia-2020'
driver.get(url)
time.sleep(2)
element = driver.find_element_by_xpath('//div[@class="desc text-truncate"]')
print(element.text)

输出:

1st T20I (N), Southampton, Sep 4 2020, Australia tour of England

要打印文本,请使用以下定位器策略之一:

  • 使用class_name文本属性:

    print(driver.find_element_by_class_name("desc").text)
    
  • 使用css_selectorget_attribute():

    print(driver.find_element_by_css_selector("div.desc").get_attribute("innerHTML"))
    
  • 使用xpath文本属性:

    print(driver.find_element_by_xpath("//div[@class='desc text-truncate']").text)
    

理想情况下,要打印元素的innerText,必须诱导WebDriverWait等待visibility_of_element_located(),并且可以使用以下定位器策略之一:

  • 使用CLASS_NAME:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CLASS_NAME, "desc"))).text)
    
  • 使用CSS_SELECTOR:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.desc"))).get_attribute("innerHTML"))
    
  • 使用XPATH:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='desc text-truncate']"))).text)
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

您可以在"如何使用Selenium-Python 检索WebElement的文本"中找到相关讨论


Outro

有用文档链接:

  • get_attribute()方法Gets the given attribute or property of the element.
  • text属性返回The text of the element.
  • 使用Selenium的文本和innerHTML之间的差异

最新更新