51Testing软件测试论坛
标题:
滑块验证显示查找不到滑块元素
[打印本页]
作者:
测试积点老人
时间:
2022-5-12 10:30
标题:
滑块验证显示查找不到滑块元素
###### 问题遇到的现象和发生背景
滑块验证总是说滑块元素查找不到 不知道是为什么
###### 问题相关代码,请勿粘贴截图
import requests
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
import time
#创建谷歌浏览器操作对象
path = 'chromedriver.exe'
driver = webdriver.Chrome(path)
# 这是登陆页面的url地址
url = 'https://login.taobao.com/member/login.jhtml?spm=a21bo.jianhua.754894437.1.5af911d9fe3dHP&f=top&redirectURL=https%3A%2F%2Fwww.taobao.com%2F'
headers = {
'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'
}
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.'
line = cookies.split(';')
cookies ={}
for i in line:
key,value = i.split('=',1)
cookies[key] = value
# 获取页面的源码
response = requests.get(url = url,headers = headers,cookies = cookies)
content = response.text
soup = BeautifulSoup(content,'lxml')
driver.get(url)
# 获取文本框的对象
ID = driver.find_element(By.ID,'fm-login-id')
KEY = driver.find_element(By.ID,'fm-login-password')
# 在文本框中输入用户名及密码
ID.send_keys('xxxxxx')
time.sleep(2)
KEY.send_keys('xxxxxx')
time.sleep(2)
#获取登录按钮对象并点击
button = driver.find_element(By.XPATH,value='//*[@id="login-form"]/div[4]/button')
button.click()
time.sleep(3)
#设置等待
wait = WebDriverWait(driver,3,0.5)
#使用匿名函数
wait.until(lambda diver:driver.find_element(By.ID,'nc_1_n1z'))
print('找到滑块了!')
#获取查询滑块对象
huakuai = driver.find_element(By.ID,'nc_1_n1z')
#将滑块拖动到另一位置
actions = ActionChains(driver) #拿到动作链对象
actions.move_to_element(huakuai).perform()
actions.click_and_hold(huakuai).perform() #在元素huakuai位置左键单击并保持
#time.sleep(1)
actions.move_by_offset(675,0).perform() #x为移动水平距离,y为移动垂直距离,正负代表左右或者上下
actions.release().perform() #松开
print('登录成功!')
###### 运行结果及报错内容
Traceback (most recent call last):
File "D:\pycharm\pythonProject\实验报告二\模拟登录taobao.py", line 63, in <module>
actions.move_to_element(huakuai).perform()
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable: [object HTMLSpanElement] has no size and location
(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