`# Generated by Selenium IDE
import pytest
import time
import json
from selenium import webdriver
from selenium.webdriver import TouchActions
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
class TestA():
def setup_method(self, method):
option=webdriver.ChromeOptions()
option.add_experimental_option(‘w3c’,False)
self.driver = webdriver.Chrome(options=option)
self.driver.implicitly_wait(3)
self.vars = {}
self.driver.maximize_window()
self.driver.implicitly_wait(5)
def teardown_method(self, method):
self.driver.quit()
#def wait_for_window(self, timeout=2):
time.sleep(round(timeout / 1000))
# wh_now = self.driver.window_handles
# wh_then = self.vars["window_handles"]
# if len(wh_now) > len(wh_then):
# return set(wh_now).difference(set(wh_then)).pop()
def test_a(self):
self.driver.get("https://www.baidu.com/")
self.driver.set_window_size(550, 694)
#self.vars["window_handles"] = self.driver.window_handles
#time.sleep(10)
#强制等待会持续执行
#def wait(x):
# return len(self.driver.find_element(By.ID,"s-top-left")) >=1
#如下两句的效果是一样的
#WebDriverWait(self.driver,10).until(wait)
WebDriverWait(self.driver, 10).until(expected_conditions.element_to_be_clickable(By.ID,"s-top-left"))
self.driver.find_element(By.LINK_TEXT, "hao123").click()
#self.vars["win4637"] = self.wait_for_window(2000)
#self.vars["root"] = self.driver.current_window_handle
#self.driver.switch_to.window(self.vars["win4637"])
#self.driver.switch_to.window(self.vars["root"])
#self.driver.close()
#self.driver.switch_to.window(self.vars["win4637"])
#self.driver.close()
def test_web_control(self):
self.driver.get('http://sahitest.com/demo/clicks.htm')
# element_clck=self.driver.find_element_by_xpath('/html/body/form/input[3]')
# 两句话一样的意思
#element_clck=self.driver.find_element_by_xpath(“//input[@value='click me']”)
element_doubleclck = self.driver.find_element_by_xpath('/html/body/form/input[2]')
element_clck = self.driver.find_element_by_xpath('/html/body/form/input[3]')
element_rightclck = self.driver.find_element_by_xpath('/html/body/form/input[4]')
action=ActionChains(self.driver)
action.click(element_clck)
action.context_click(element_rightclck)
action.double_click(element_doubleclck)
time.sleep(3)
action.perform()
time.sleep(300)
def test_move_able(self):
self.driver.get("http://www.baidu.com")
ele=self.driver.find_element_by_xpath('//*[@id="s-usersetting-top"]')
action=ActionChains(self.driver)
action.move_to_element(ele)
action.perform()
time.sleep(30)
def test_drag_drop(self):
self.driver.get('http://sahitest.com/demo/dragDropMooTools.htm')
drag_element= self.driver.find_element_by_id("dragger")
drop_element1= self.driver.find_element_by_xpath("/html/body/div[2]")
drop_element2 = self.driver.find_element_by_xpath("/html/body/div[3]")
drop_element3 = self.driver.find_element_by_xpath("/html/body/div[4]")
drop_element4 = self.driver.find_element_by_xpath("/html/body/div[5]")
action=ActionChains(self.driver)
action.drag_and_drop(drag_element,drop_element1).perform()
action.click_and_hold(drag_element).release(drop_element1).perform()
action.click_and_hold(drag_element).move_to_element(drop_element2).perform()
action.perform()
time.sleep(20)
def test_key(self):
self.driver.get('http://sahitest.com/demo/label.htm')
ele=self.driver.find_element_by_xpath("/html/body/label[1]/input")
ele.click()
action=ActionChains(self.driver)
action.send_keys(Keys.SPACE).pause(1)
action.send_keys("tom").pause(1)
action.send_keys(Keys.BACK_SPACE).perform()
time.sleep(300)
def test_Touch(self):
self.driver.get('http://www.baidu.com')
el_search=self.driver.find_element_by_id("su")
el=self.driver.find_element_by_id("kw")
el.send_keys("selenium测试")
action=TouchActions(self.driver)
action.scroll_from_element(el,0,10000).perform()
action.perform()
time.sleep(30)
def test_form(self):
#表单的操作和正常的操作都是一样的。
self.driver.get('https://testerhome.com/account/sign_in')
self.driver.find_element_by_xpath('// *[ @ id = "user_login"]').send_keys('123')
#self.driver.find_element_by_xpath('//*[@id="s-usersetting-top"]')
self.driver.find_element_by_id('user_password').send_keys('password')
self.driver.find_element_by_xpath('//*[@id="new_user"]/div[3]/label').click()
self.driver.find_element_by_xpath('//*[@id="new_user"]/div[4]/input').click()
time.sleep(30)
#多窗口处理与网页frame
#通过句柄来标识某一个窗口
def test_windwon(self):
self.driver.get('https://www.baidu.com')
#点击登录
self.driver.find_elements_by_link_text('登录')
print(self.driver.current_window_handle)
#点击立即注册
`官方文档;https://selenium-python.readthedocs.io/api.html
ActionChains :执行PC端的鼠标点击,右键,拖拽等事件
TouchActions:模拟pc和移动端的点击,滑动,拖拽,多点触控等多种手势操作。
click_and_hold
double_click
drag_and_drop 拖拽
还有坐标偏移的操作
还有模拟键盘的操作。全选之类的操作。
还有执行释放的操作。
actionChains的执行原理
调用actionchains的方法时,不会立即执行,而是将所有的操作,按照书序存放在一个队列里面,当你调用performf方法时,队列中的事件会依次执行
actionchains有两种写法