简体   繁体   English

Selenium 无法找到元素

[英]Selenium having trouble finding elements

I am trying to gather recent NFT transaction activity from an OpenSea profile.我正在尝试从 OpenSea 配置文件中收集最近的 NFT 交易活动。 I am getting an error that states the program is unable to locate the element, however my element has been defined and I copy and pasted the element from the web page.我收到一条错误消息,指出程序无法找到该元素,但是我的元素已被定义,我从 web 页面复制并粘贴了该元素。

What I am trying to do is selecting the class that holds all of the activities on the section, and then using XPath to gather each items info and print the item info.我要做的是选择包含该部分所有活动的 class,然后使用 XPath 收集每个项目信息并打印项目信息。 After looking at guides, videos, and other peoples code, I am pretty lost on what I am doing wrong.在查看了指南、视频和其他人的代码之后,我对自己做错了什么非常迷茫。

Any advice would be greatly appreciated.任何建议将不胜感激。

Here is my code:这是我的代码:

''' '''

from selenium import webdriver from selenium.webdriver.common.by import By从 selenium 导入 webdriver 从 selenium.webdriver.common.by 导入

PATH = ('/Users/chris/Desktop/chromedriver') driver = webdriver.Chrome(PATH) PATH = ('/Users/chris/Desktop/chromedriver') driver = webdriver.Chrome(PATH)

driver.get("https://opensea.io/GaryVee?tab=activity") driver.get("https://opensea.io/GaryVee?tab=activity")

action = driver.find_element(By.CLASS_NAME, 'Blockreact__Block-sc-1xf18x6-0 dBFmez') action = driver.find_element(By.CLASS_NAME, 'Blockreact__Block-sc-1xf18x6-0 dBFmez')

for action in actions:行动中的行动:

nft_name = action.find_element(By.XPATH, './/*[@id="main"]/div/div/div[3]/div/div[3]/div[2]/div/div[2]/div['
                                         '1]/button/div/div[2]/div/div/div/div[2]/span[1]/div/div/a').text

nft_id = action.find_element(By.XPATH, './/*[@id="main"]/div/div/div[3]/div/div[3]/div[2]/div/div[2]/div['
                                       '1]/button/div/div[2]/div/div/div/div[2]/span[2]/a/div').text

price_eth = action.find_element(By.XPATH, './/*[@id="main"]/div/div/div[3]/div/div[3]/div[2]/div/div[2]/div['
                                          '1]/button/div/div[3]/div/div[1]/div/div[2]/text()').text

price_usd = action.find_element(By.XPATH, './/*[@id="main"]/div/div/div[3]/div/div[3]/div[2]/div/div[2]/div['
                                          '1]/button/div/div[3]/div/div[2]/span/div/div').text

sending_user = action.find_element(By.XPATH, './/*[@id="main"]/div/div/div[3]/div/div[3]/div[2]/div/div[2]/div['
                                             '1]/button/div/div[5]/div/a/span').text

recieving_user = action.find_element(By.XPATH, './/*[@id="main"]/div/div/div[3]/div/div[3]/div[2]/div/div['
                                               '2]/div[1]/button/div/div[6]/div/a/span').text

status = action.find_element(By.XPATH, './/*[@id="main"]/div/div/div[3]/div/div[3]/div[2]/div/div[2]/div['
                                       '1]/button/div/div[1]/h6').text

print(nft_name, nft_id, price_eth, price_usd, sending_user, recieving_user, status) 

''' '''

Here is my error code:这是我的错误代码:

''' '''

/Users/chris/PycharmProjects/pythonProject/main.py:6: DeprecationWarning: executable_path has been deprecated, please pass in a Service object driver = webdriver.Chrome(PATH) Traceback (most recent call last): File "/Users/chris/PycharmProjects/pythonProject/main.py", line 11, in action = driver.find_element(By.CLASS_NAME, 'Blockreact__Block-sc-1xf18x6-0 dBFmez') File "/Users/chris/PycharmProjects/pythonProject/venv/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 1244, in find_element return self.execute(Command.FIND_ELEMENT, { File "/Users/chris/PycharmProjects/pythonProject/venv/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 424, in execute self.error_handler.check_response(response) File "/Users/chris/PycharmProjects/pythonProject/venv/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 247, in check_response raise exception_class(message, screen, stacktrace) Z8E00596AD8DE2213FF /Users/chris/PycharmProjects/pythonProject/main.py:6: DeprecationWarning: executable_path has been deprecated, please pass in a Service object driver = webdriver.Chrome(PATH) Traceback (最近一次调用最后): File "/Users/chris /PycharmProjects/pythonProject/main.py”,第 11 行,实际操作 = driver.find_element(By.CLASS_NAME, 'Blockreact__Block-sc-1xf18x6-0 dBFmez') 文件“/Users/chris/PycharmProjects/pythonProject/venv/lib/ python3.9/site-packages/selenium/webdriver/remote/webdriver.py”,第 1244 行,在 find_element 返回 self.execute(Command.FIND_ELEMENT, { File "/Users/chris/PycharmProjects/pythonProject/venv/lib/python3 .9/site-packages/selenium/webdriver/remote/webdriver.py”,第 424 行,在执行 self.error_handler.check_response(response) 文件“/Users/chris/PycharmProjects/pythonProject/venv/lib/python3.9/ site-packages/selenium/webdriver/remote/errorhandler.py",第 247 行,在 check_response 中引发异常类(消息、屏幕、堆栈跟踪)Z8E00596AD8DE2213FF 8F8D8478D5362CZ.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".Blockreact__Block-sc-1xf18x6-0 dBFmez"} (Session info: chrome=127.0.0.1) Stacktrace: 8F8D8478D5362CZ.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法找到元素:{"method":"css selector","selector":".Blockreact__Block-sc-1xf18x6-0 dBFmez"}(会话信息:chrome =127.0.0.1) 堆栈跟踪:

''' '''

To identify the element you can use either of the following Locator Strategies :要识别元素,您可以使用以下任一Locator Strategies

  • Using css_selector :使用css_selector

     action = driver.find_element(By.CSS_SELECTOR, ".Blockreact__Block-sc-1xf18x6-0.Flexreact__Flex-sc-1twd32i-0.FlexColumnreact__FlexColumn-sc-1wwz3hp-0.OpenSeaPagereact__DivContainer-sc-65pnmt-0.dBFmez.jYqxGr.ksFzlZ.iRiGb")
  • Using xpath :使用xpath

     action = driver.find_element(By.XPATH, "//*[@class='Blockreact__Block-sc-1xf18x6-0 Flexreact__Flex-sc-1twd32i-0 FlexColumnreact__FlexColumn-sc-1wwz3hp-0 OpenSeaPagereact__DivContainer-sc-65pnmt-0 dBFmez jYqxGr ksFzlZ iRiGb']")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM