web交互基本操作

1. 常见操作:右键点击、页面滑动、表单操作

2. 工具:selenium提供的Action Chains(PC端)、Touch Actions(PC端+移动端)

3. ActionChains

原理:在调用ActionChains时,是将所有的操作按顺序存放在一个队列里,调用perform(),队列里的事件才会依次执行

3.1 点击:

#ActionChains-点击操作
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By

class TestActionChains:
    def setup_method(self, method):
        self.driver = webdriver.Chrome()
        self.driver.implicitly_wait(5)

    def teardown_method(self, method):
        self.driver.quit()

    def test_click(self):
        self.driver.get("https://sahitest.com/demo/clicks.htm")
        # 定位元素
        normal_click = self.driver.find_element(By.XPATH, '//*[@value="click me"]')
        double_click = self.driver.find_element(By.XPATH, '//*[@value="dbl click me"]')
        right_click = self.driver.find_element(By.XPATH, '//*[@value="right click me"]')
        #初始化
        action = ActionChains(self.driver)
        #添加动作
        action.click(normal_click)
        action.double_click(double_click)
        action.context_click(right_click)
        #执行动作
        action.perform()

3.2 移动:将鼠标移动到某个元素(element)上(action.move_to_element(element))

#ActionChains
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By

class TestActionChains:
    def setup_method(self, method):
        self.driver = webdriver.Chrome()
        self.driver.implicitly_wait(5)

    def teardown_method(self, method):
        self.driver.quit()

    def test_moveto(self):
        self.driver.get("https://www.baidu.com")
        # 定位元素
        element=self.driver.find_element(By.ID,'s-usersetting-top')
        #初始化
        action = ActionChains(self.driver)
        #添加动作
        action.move_to_element(element)
        #执行动作
        action.perform()
        time.sleep(6)

3.3 拖拽:将element1拖拽到element2上(action.drag_and_drop(element1,element2))

#ActionChains
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By

class TestActionChains:
    def setup_method(self, method):
        self.driver = webdriver.Chrome()
        self.driver.implicitly_wait(5)

    def teardown_method(self, method):
        self.driver.quit()

    def test_dragtodrop(self):
        self.driver.get("https://sahitest.com/demo/dragDropMooTools.htm")
        # 定位元素
        element1=self.driver.find_element(By.ID,'dragger')
        element2=self.driver.find_element(By.XPATH,'/html/body/div[4]')
        #初始化
        action = ActionChains(self.driver)
        #添加动作
        action.drag_and_drop(element1,element2)
        #执行动作
        action.perform()
        time.sleep(6)

底层逻辑,click_and_hold(element1).release(element2)

#ActionChains
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By

class TestActionChains:
    def setup_method(self, method):
        self.driver = webdriver.Chrome()
        self.driver.implicitly_wait(5)

    def teardown_method(self, method):
        self.driver.quit()

    def test_dragtodrop(self):
        self.driver.get("https://sahitest.com/demo/dragDropMooTools.htm")
        # 定位元素
        element1=self.driver.find_element(By.ID,'dragger')
        element2=self.driver.find_element(By.XPATH,'/html/body/div[4]')
        #初始化
        action = ActionChains(self.driver)
        #添加动作
        action.click_and_hold(element1).release(element2)
        #执行动作
        action.perform()
        time.sleep(6)

3.4 键入(action.send_keys())

#ActionChains
import time
from selenium import webdriver
from selenium.webdriver import ActionChains, Keys
from selenium.webdriver.common.by import By

class TestActionChains:
    def setup_method(self, method):
        self.driver = webdriver.Chrome()
        self.driver.implicitly_wait(5)

    def teardown_method(self, method):
        self.driver.quit()

    def test_backkeys(self):
        self.driver.get("https://sahitest.com/demo/label.htm")
        # 定位元素
        element=self.driver.find_element(By.XPATH,'/html/body/label[1]/input')
        #初始化
        action = ActionChains(self.driver)
        #添加动作
        action.click(element)
        action.send_keys("wlina")
        action.send_keys((Keys.BACKSPACE))
        #执行动作
        action.perform()
        time.sleep(6)

4. Touch Actions(ActionChains对H5页面无效、对移动端无效,但是Touch Actions均有效)

4.1 示例:场景:打开chrome,打开百度,向搜索框中输入"selenium测试",点击搜索框,滑动到底部(需要注意:selenium4.版本与TouchAction不兼容,需要将selenium的版本降级)

#TouchAction
import time
from selenium import webdriver
from selenium.webdriver import TouchActions, Keys
from selenium.webdriver.common.by import By

class TestTouchAction:
    def setup_method(self, method):
        self.driver = webdriver.Chrome()
        option = webdriver.ChromeOptions()
        option.add_experimental_option('w3c', False)
        self.driver.implicitly_wait(5)

    def teardown_method(self, method):
        self.driver.quit()

    def test_touchaction(self):
        self.driver.get("http://www.baidu.com")
        # 定位元素
        element1=self.driver.find_element(By.ID,'kw')
        element2 = self.driver.find_element(By.ID, 'su')
        element1.send_keys("selenium测试")
        #初始化
        action = TouchActions(self.driver)
        #添加动作
        action.tap(element2)
        #执行动作
        action.perform()
        # 滑动窗口,从搜索框到到底,x轴不需要偏移,设置为0,y轴需要偏移一个足够大的值即可
        action.scroll_from_element(element1,0,10000).perform()
        

5. 表单操作

表单:前端显示为<form></form>的部分

#提交表单操作
import time
from selenium import webdriver
from selenium.webdriver.common.by import By

class TestTouchAction:
    def setup_method(self, method):
        self.driver = webdriver.Chrome()
        self.driver.implicitly_wait(5)

    def teardown_method(self, method):
        self.driver.quit()

    def test_form(self):
        self.driver.get("https://testerhome.com/account/sign_in")
        # 定位元素
        username = self.driver.find_element(By.ID,'user_login')
        password = self.driver.find_element(By.ID, 'user_password')
        rememberme = self.driver.find_element(By.XPATH, '/html/body/div[2]/div/div[2]/div/div/form/div[3]/div')
        login=self.driver.find_element(By.XPATH, '/html/body/div[2]/div/div[2]/div/div/form/div[4]')
        #初始化
        username.send_keys("123")
        password.send_keys("12345")
        rememberme.click()
  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值