Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x Python:Xpath无法定位元素_Python 3.x_Selenium_Xpath_Css Selectors_Webdriverwait - Fatal编程技术网

Python 3.x Python:Xpath无法定位元素

Python 3.x Python:Xpath无法定位元素,python-3.x,selenium,xpath,css-selectors,webdriverwait,Python 3.x,Selenium,Xpath,Css Selectors,Webdriverwait,我试图从一个网站获得一些数据,但得到以下错误。它昨晚起作用了,但当我今天重新运行时,它突然无法定位元素。今天,我尝试了几乎我可以,但无法解决它 工具和语言-Python、Selenium、Chrome、Chromedriver、AWS Cloud 9、EC2 from selenium import webdriver import time from selenium.webdriver.chrome.options import Options options = Options() opt

我试图从一个网站获得一些数据,但得到以下错误。它昨晚起作用了,但当我今天重新运行时,它突然无法定位元素。今天,我尝试了几乎我可以,但无法解决它

工具和语言-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]"}
我试过下面的东西

  • 增加和减少睡眠时间。增加和减少睡眠时间
  • 使用完整的Xpath、Xpath、按类查找
  • 试图定位不同的元素
  • 不同的页面
  • 提到各种网站仍然无法解决。我是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),南安普顿,2020年9月4日,澳大利亚英格兰巡回赛,您可以使用以下任一选项:

    • 使用
      class\u name
      和文本属性:

      print(driver.find_element_by_class_name("desc").text)
      
      print(driver.find_element_by_xpath("//div[@class='desc text-truncate']").text)
      
    • 使用
      css\u选择器
      get\u属性()

    • 使用
      xpath
      和文本属性:

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

    理想情况下,要打印某个元素的
    innerText
    ,您必须为元素的
    可见性\u located()
    ,并且您可以使用以下任一选项:

    • 使用
      类名

      print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CLASS_NAME, "desc"))).text)
      
    • 使用
      CSS\u选择器

      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从selenium.webdriver.chrome导入webdriver.options导入选项导入时间选项=选项()选项。headless=True驱动程序=webdriver.chrome(选项=选项)驱动程序。get(“https://en.wikipedia.org/wiki/Amazon_Web_Services)time.sleep(20)element_text=driver.find_element_by_id(“firstheading”)。文本打印(element_text)
    ——这个最简单的代码在新代码中无法运行。是否有可能解释我的代码中的错误?这里的Xpath似乎有问题。我已经测试了它,得到了与您相同的错误。非常感谢Debanjan。这很有帮助。