要先看好有没有ifram,有的话要切换才可以实现。比如我这里做的是阿里巴巴国际站的登录,要逐级依次切入。注册页面反而没有ifram。
driver.switch_to_frame("alibaba-login-box")#切进大的ifram
driver.switch_to_frame("baxia-dialog-content")#切进小的ifram
#这里写操作代码
driver.switch_to_default_content()#切出来
1、滑动滑块
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
# 定位第一个滑块
dragger = driver.find_elements_by_id("nc_1_n1z")[-1]
action = ActionChains(driver)
# 对滑块按下鼠标左键不放
action.click_and_hold(dragger).perform()
for index in range(20):
try:
# X每次移动2个像素,Y为0。平行移动鼠标
action.move_by_offset(71, 0).perform()
except UnexpectedAlertPresentException:
break
draggable = driver.find_element_by_id('nc_1__scale_text')
# 滚动指定元素位置
driver.execute_script("arguments[0].scrollIntoView();", draggable)
time.sleep(2)
ActionChains(driver).click_and_hold(draggable).perform()
# 拖动
ActionChains(driver).move_by_offset(xoffset=247, yoffset=0).perform()
ActionChains(driver).release().perform()
time.sleep(3)
2、登录后要验证涂抹元素使其显示出两只小鸡
想了一个晚上,发现用一个for循环,去滑动n次,随机取滑动范围值,滑它100次200次我就不信涂不出两只小鸡。注意要看元素尺寸大小,取值范围太小滑不出来
# 鼠标往右上方横向纵向各随机移动
element = driver.find_element_by_css_selector("[id='nc_1_cover']")
ActionChains(driver).click_and_hold(element)
for dd in range(100):
print(dd)
result1 = random.randint(-300, 300)
result2 = random.randint(-50, 50)
ActionChains(driver).drag_and_drop_by_offset(element, result1, result2).perform()