鱼C论坛

 找回密码
 立即注册
查看: 1582|回复: 2

关于selenium定位元素的问题

[复制链接]
发表于 2021-8-8 15:07:34 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
我试着运行下面的代码:
  1. from selenium import webdriver
  2. from selenium.webdriver import ActionChains
  3. from selenium.webdriver.common.by import By
  4. from selenium.webdriver.support import expected_conditions as EC
  5. from selenium.webdriver.support.ui import WebDriverWait
  6. from selenium.common.exceptions import StaleElementReferenceException
  7. import time

  8. def login_fx(username,password):
  9.     ......      # 此处是用登录bilbili的代码,最终返回一个已经登录的浏览器(B站首页)

  10. def comment_fx():
  11.     browser.get(F"ht请tps:/把/sp我ace.bil去ibili.co掉m/119/dyn!amic")    # 访问个人空间
  12.     actions2 = ActionChains(browser)   # 重新实例化ActionChains对象
  13.     time.sleep(1)
  14.     browser.find_element_by_class_name("comment").click()    # 点击以展开评论区
  15.     time.sleep(3)
  16.     browser.find_element_by_class_name("ipt-txt").send_keys("123")     # 查找评论框


  17. """
  18. username = str(input("用户名:"))
  19. password = str(input("密码:"))
  20. """

  21. login_fx(username,password)
  22. comment_fx()
复制代码


报错如下:
  1. Traceback (most recent call last):
  2.   File "E:\002.py", line 55, in <module>
  3.     comment_fx()
  4.   File "E:\002.py", line 41, in comment_fx
  5.     browser.find_element_by_class_name("ipt-txt").send_keys("123")
  6.   File "D:\Python\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 564, in find_element_by_class_name
  7.     return self.find_element(by=By.CLASS_NAME, value=name)
  8.   File "D:\Python\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in find_element
  9.     return self.execute(Command.FIND_ELEMENT, {
  10.   File "D:\Python\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
  11.     self.error_handler.check_response(response)
  12.   File "D:\Python\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
  13.     raise exception_class(message, screen, stacktrace)
  14. selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".ipt-txt"}
  15.   (Session info: chrome=91.0.4472.114)
复制代码

XPath表达式和class属性均无法使用,我该怎么办?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-8-8 16:01:29 | 显示全部楼层
  1. browser.find_element_by_class_name("comment").click()    # 点击以展开评论区
复制代码


改成

  1. btn = browser.find_element_by_class_name("comment")

  2. browser.execute_script("arguments[0].click()",btn)
复制代码


就好了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-8-9 15:04:41 | 显示全部楼层
向下方滚动也无法加载
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-20 14:27

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表