Python: Xpath 无法定位元素

Python: Xpath not able to locate element

我正在尝试从网站获取一些数据,但出现以下错误。它昨晚工作,但当我今天重新运行时,它突然无法定位元素。今天试了差不多能解决还是解决不了

工具和语言 - Python、Selenium、Chrome、Chrome驱动程序、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,Xpa​​th,通过 class
  3. 查找
  4. 尝试定位不同的元素。
  5. 这个的不同页面。

参考了各个网站还是无法解决。我是 python.

的新手

试试这个:

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

要打印文本 1st T20I (N), Southampton, Sep 4 2020, Australia tour of England 您可以使用以下任一方法 :

  • 使用 class_nametext 属性:

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

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

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

理想情况下,要打印元素的 innerText,您必须为 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
    

You can find a relevant discussion in


结尾

Link 到有用的文档:

  • get_attribute()方法Gets the given attribute or property of the element.
  • text属性returnsThe text of the element.
  • Difference between text and innerHTML using Selenium