今天写python测试脚本遇到这个问题,问题出现的背景是:登录需要操作滑动条,图片吻合后才验证通过。系统是固定十张图片,随机产生;当验证通过后一直报错selenium.common.exceptions.WebDriverException: Message: TypeError: rect is undefined
贴上代码,因为啥帮其他朋友测试,代码比较简易,没时间整理,将就一下;如果有朋友知道解决办法,麻烦留言下,不胜感激!
from selenium import webdriver
from selenium.webdriver import ActionChains
import time
from selenium.common.exceptions import NoSuchElementException
def is_element_present(driver, how, what):
try:
driver.find_element(by=how, value=what)
except NoSuchElementException:
return False
return True
driver = webdriver.Firefox()
driver.get('https://www.duoduodaizhang.com/login')
time.sleep(2)
phone = driver.find_element_by_xpath("/html/body/div[1]/div/div/div[1]/div/div[2]/div[2]/div/div[1]/input")
phone.click()
phone.send_keys('1897XXXXXXXX')
password = driver.find_element_by_xpath("/html/body/div[1]/div/div/div[1]/div/div[2]/div[2]/div/div[2]/input")
password.click()
password.send_keys('XXXXXXX')
button = driver.find_element_by_xpath("/html/body/div[1]/div/div/div[1]/div/div[2]/div[2]/div/div[3]/button")
button.click()
time.sleep(2)
huadong = driver.find_element_by_xpath("/html/body/div[1]/div/div/div[1]/div[2]/div[3]/div/div/div[2]/div/div[2]/div")
action_chains = ActionChains(driver)
for i in range(0,10):
action_chains.drag_and_drop_by_offset(huadong,200,0).perform()
if is_element_present(driver,'xpath',"/html/body/div[1]/div/div/div[1]/div/div[1]/ul/li[5]/a[2]")==False:
continue
if is_element_present(driver, 'xpath', "/html/body/div[1]/div/div/div[1]/div/div[1]/ul/li[5]/a[2]") == True:
print('success')
break
不稳定的解决办法:
for i in range(0,10): action_chains.drag_and_drop_by_offset(huadong,200,0).perform()
time.sleep(10)
强制休眠十秒,有一定机会成功!