1. 如下图的slider拖动,如何做?
2.分析:拖动滚动条可以看到对应的元素值在对应的变化,先取到对应的元素
elSlider = driver.find_element_by_xpath("//form[@class='el-form demo-ruleForm el-form--label-left']//div[@class='el-form-item weight-pack']/div/div/div/div")
3.接下来需要导入ActionChains这个类,可以点击并保持。
在源元素上的按住鼠标左键,然后移动到目标偏移量并释放鼠标按钮。不知道xoffset,yoffset可以都设1为默认起点。
from selenium.webdriver.common.action_chains import ActionChains ActionChains(driver).move_to_element_with_offset(elSlider,1,1).click_and_hold().perform()
4.整体源码如下:
#-*- coding:utf-8 -*- # __author__ = 'a1' import os from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains import time def setUp(): chromedriver = "D:/Python/Python36-32/chromedriver.exe" os.environ["webdriver.chrome.driver"] = chromedriver driver = webdriver.Chrome(chromedriver) driver.get("http://192.168.2.12") test_login(driver) def test_login(driver): username = "13500010001" password = "12345678" driver.find_element_by_id("Login_Name_Input").clear() driver.find_element_by_id("Login_Name_Input").send_keys(username) #密码的定位 driver.find_element_by_id("Login_Password_Input").clear() driver.find_element_by_id("Login_Password_Input").send_keys(password) # 点击登录 driver.find_element_by_id("Login_Login_Btn").click() time.sleep(3) driver.find_element_by_id("BaseFramework_Menu_Strategy").click() time.sleep(1) driver.find_element_by_id("StrategyList_Btn_NewClick").click() time.sleep(3) driver.find_element_by_id("StrategyNewInfo_Input_Name").send_keys("myStrategy") time.sleep(2)
#找到元素对象 elSlider = driver.find_element_by_xpath("//form[@class='el-form demo-ruleForm el-form--label-left']//div[@class='el-form-item weight-pack']/div/div/div/div") #移动到源元素就保持按鼠标左键 ActionChains(driver).move_to_element_with_offset(elSlider,elSlider.size['width'],6).click_and_hold().perform() time.sleep(1)
#移动到目标位置,x,y坐标,根据像素设置。本例只用到x可以最大设置到380像素为100%。 ActionChains(driver).move_by_offset(35.2,6).perform() time.sleep(1)
#松开鼠标 ActionChains(driver).release().perform() time.sleep(3) driver.close() setUp()
5.可以查看最大像素是多少,按比例移动。如380移到51则为190
ActionChains(driver).move_by_offset(190,1).perform()