51Testing软件测试论坛

标题: 滑块验证显示查找不到滑块元素 [打印本页]

作者: 测试积点老人    时间: 2022-5-12 10:30
标题: 滑块验证显示查找不到滑块元素
  1. ###### 问题遇到的现象和发生背景
  2. 滑块验证总是说滑块元素查找不到 不知道是为什么

  3. ###### 问题相关代码,请勿粘贴截图
  4. import requests
  5. from bs4 import BeautifulSoup
  6. from selenium import webdriver
  7. from selenium.webdriver import ActionChains
  8. from selenium.webdriver.common.by import By
  9. from selenium.webdriver.support.wait import WebDriverWait
  10. import time

  11. #创建谷歌浏览器操作对象
  12. path = 'chromedriver.exe'
  13. driver = webdriver.Chrome(path)

  14. # 这是登陆页面的url地址
  15. url = 'https://login.taobao.com/member/login.jhtml?spm=a21bo.jianhua.754894437.1.5af911d9fe3dHP&f=top&redirectURL=https%3A%2F%2Fwww.taobao.com%2F'
  16. headers = {
  17.     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36'
  18. }
  19. cookies = 't=2de9399f572cbaac83dea679890bfe6f; _bl_uid=Lbld00q4oX10gF195j06fnFfIzzs; _m_h5_tk=86de938f16366037f99fa94a3f092d5f_1651046569898; _m_h5_tk_enc=0ebdbd588371a9146d6b3af126ef7bd5; XSRF-TOKEN=030dbccf-5f5f-4c8b-995b-2e9bfd9a11df; _samesite_flag_=true; cookie2=1ed0c46602ee18b9d4f7caead9c7fd6f; _tb_token_=e613a5edbb8e; xlly_s=1; sgcookie=E100BBC2DA1NqmvzdzAv7nR0AJ0UWMBhke9oD40wJ/5MOaK3cG1597/19NyR+WvsHaDbvnICIEWgyDVZ8DsGEDN16xc5Y0hbRkObcfTGiUGMdJiemyG9+JDG0+xtXx15iIug; thw=cn; mt=ci=0_0; tracknick=; cna=SqmzGoLQCWQCAd9JcbSbctl0; l=eBjQXvKgLrgufbvjBO5Cnurza7PEuCOXGKVzaNbMiInca6Ch13hB-NC3XNyXRdtYgt5j5LtyhayveRFe-7zU-x1Hrt7APlUOrvJwRe1..; isg=BMzMnw0lwnfwRdbPbTcEHOF_nSr-BXCv3s67sCaIwHcHsWC7ZhRxPmJHUPks16gH; tfstk=cQrlB7Yxjzu5tlu05gi5A7Dd8mCOaCLINDou0T2UWlKKhBUrUsDHuFSFFKcf1_8C.'
  20. line = cookies.split(';')
  21. cookies ={}
  22. for i in line:
  23.     key,value = i.split('=',1)
  24.     cookies[key] = value
  25. # 获取页面的源码
  26. response = requests.get(url = url,headers = headers,cookies = cookies)
  27. content = response.text
  28. soup = BeautifulSoup(content,'lxml')

  29. driver.get(url)
  30. # 获取文本框的对象
  31. ID = driver.find_element(By.ID,'fm-login-id')
  32. KEY = driver.find_element(By.ID,'fm-login-password')
  33. # 在文本框中输入用户名及密码
  34. ID.send_keys('xxxxxx')
  35. time.sleep(2)
  36. KEY.send_keys('xxxxxx')
  37. time.sleep(2)
  38. #获取登录按钮对象并点击
  39. button = driver.find_element(By.XPATH,value='//*[@id="login-form"]/div[4]/button')
  40. button.click()
  41. time.sleep(3)

  42. #设置等待
  43. wait = WebDriverWait(driver,3,0.5)
  44. #使用匿名函数
  45. wait.until(lambda diver:driver.find_element(By.ID,'nc_1_n1z'))
  46. print('找到滑块了!')

  47. #获取查询滑块对象
  48. huakuai = driver.find_element(By.ID,'nc_1_n1z')
  49. #将滑块拖动到另一位置
  50. actions = ActionChains(driver)    #拿到动作链对象
  51. actions.move_to_element(huakuai).perform()
  52. actions.click_and_hold(huakuai).perform()   #在元素huakuai位置左键单击并保持
  53. #time.sleep(1)
  54. actions.move_by_offset(675,0).perform()     #x为移动水平距离,y为移动垂直距离,正负代表左右或者上下
  55. actions.release().perform()     #松开

  56. print('登录成功!')

  57. ###### 运行结果及报错内容
  58. Traceback (most recent call last):
  59.   File "D:\pycharm\pythonProject\实验报告二\模拟登录taobao.py", line 63, in <module>
  60.     actions.move_to_element(huakuai).perform()
  61. selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable: [object HTMLSpanElement] has no size and location
  62.   (Session info: chrome=101.0.4951.54)
复制代码



作者: qqq911    时间: 2022-5-13 10:30
换个定位方式
作者: 郭小贱    时间: 2022-5-13 14:17
应该是没有定位成功。
作者: jingzizx    时间: 2022-5-13 14:54
先确定能够定位到
作者: bellas    时间: 2022-5-13 16:14
找准定位




欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2