【问题标题】:Python: Xpath not able to locate elementPython:Xpath 无法定位元素
【发布时间】:2020-12-24 13:26:57
【问题描述】:

我正在尝试从网站获取一些数据,但出现以下错误。它昨晚工作,但是当我今天重新运行时,它突然无法找到元素。今天,我尝试了几乎我可以但无法解决它。

工具和语言 - 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,Xpa​​th,按类查找
  3. 试图定位不同的元素。
  4. 这个不同的页面。

参考了各种网站仍然无法解决。我是 python 新手。

【问题讨论】:

  • from selenium import webdriver from selenium.webdriver.chrome.options import Options import time options = Options() options.headless = True driver = webdriver.Chrome(options=options) driver.get("https://en.wikipedia.org/wiki/Amazon_Web_Services") time.sleep(20) element_text = driver.find_element_by_id("firstheading").text print(element_text) ---这个最简单的代码不起作用

标签: python-3.x selenium xpath css-selectors webdriverwait


【解决方案1】:

试试这个:

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

【讨论】:

  • 感谢您提供新代码。是否可以解释我的代码有什么问题?
  • Xpath 似乎有问题。我已经对其进行了测试,并且遇到了与您相同的错误。
【解决方案2】:

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

  • 使用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() 诱导WebDriverWait,您可以使用以下Locator Strategies 之一:

  • 使用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
    

您可以在How to retrieve the text of a WebElement using Selenium - Python找到相关讨论


结尾

链接到有用的文档:

【讨论】:

    猜你喜欢
    • 2012-01-11
    • 2022-01-06
    • 2021-06-21
    • 2021-07-30
    • 2021-08-01
    相关资源
    最近更新 更多