PythonUI 鼠标/键盘/Select操作

一、鼠标操作

百度首页-->点击设置(移动鼠标到设置、鼠标悬停setting元素)-->点击高级搜索

import time
from selenium import webdriver
from selenium.webdriver import ActionChains

driver = webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(5)
driver.get("http://www.baidu.com")
setting = driver.find_element_by_id("s-usersetting-top")

# 鼠标悬停到设置元素(初始化链条,调用里面的方法操作;全局变量)
acting_chains = ActionChains(driver)
acting_chains.move_to_element(setting).perform()
time.sleep(2)
adv_setting = driver.find_element_by_link_text("高级搜索")
adv_setting.click()
time.sleep(2)
driver.quit()

注意:perform()一定不能忘记,否则动作不会生效。

了解ActionChains类,封装了很多鼠标操作的方法

1).click()点击

ActionChains(driver).click(elem).perform()

2).click_and_hold  点击不动

ActionChains(driver).click_and_hold(on_element).perform()

3).context_click  右击

ActionChains(driver).context_click(on_element).perform()

4).double_click 双击

ActionChains(driver).double_click(on_element).perform()

5).drag_and_drop 鼠标拖拽

ActionChains(driver).drag_and_drop(source,target).perform()

其他方法操作类似:先初始化,然后调用方法

二、ActionChains调用(链式调用)

_actions = []

def move_to():
    print("移动指令")

def click():
    print("点击")

_actions.append(click)
_actions.append(move_to)

def perform():
    for action in _actions:
        action()

perform()

 

在perform()函数中,直接调用action(),action里面包含了两个方法

ActionChains类里面,perform()方法是最后释放的,没有返回值。

click(),double_click() 、click_and_hold()、content_click()等,都是返回self,因为只有返回她自己,才能继续调用这个类里面的其他的方法。

重点:链式调用的前提是要返回self,才能继续调用自己的方法

ActionChains类源码:

class ActionChains(object):

    def __init__(self, driver):

        self._driver = driver
        self._actions = []
        if self._driver.w3c:
            self.w3c_actions = ActionBuilder(driver)

    def perform(self):

        if self._driver.w3c:
            self.w3c_actions.perform()
        else:
            for action in self._actions:
                action()

    def reset_actions(self):

        if self._driver.w3c:
            self.w3c_actions.clear_actions()
        self._actions = []

    def click(self, on_element=None):

        if on_element:
            self.move_to_element(on_element)
        if self._driver.w3c:
            self.w3c_actions.pointer_action.click()
            self.w3c_actions.key_action.pause()
            self.w3c_actions.key_action.pause()
        else:
            self._actions.append(lambda: self._driver.execute(
                                 Command.CLICK, {'button': 0}))
        return self

    def click_and_hold(self, on_element=None):

        if on_element:
            self.move_to_element(on_element)
        if self._driver.w3c:
            self.w3c_actions.pointer_action.click_and_hold()
            self.w3c_actions.key_action.pause()
        else:
            self._actions.append(lambda: self._driver.execute(
                                 Command.MOUSE_DOWN, {}))
        return self

    def context_click(self, on_element=None):

        if on_element:
            self.move_to_element(on_element)
        if self._driver.w3c:
            self.w3c_actions.pointer_action.context_click()
            self.w3c_actions.key_action.pause()
            self.w3c_actions.key_action.pause()
        else:
            self._actions.append(lambda: self._driver.execute(
                                 Command.CLICK, {'button': 2}))
        return self

    def double_click(self, on_element=None):

        if on_element:
            self.move_to_element(on_element)
        if self._driver.w3c:
            self.w3c_actions.pointer_action.double_click()
            for _ in range(4):
                self.w3c_actions.key_action.pause()
        else:
            self._actions.append(lambda: self._driver.execute(
                                 Command.DOUBLE_CLICK, {}))
        return self

    def drag_and_drop(self, source, target):
        """
         - source: The element to mouse down.
         - target: The element to mouse up.
        """
        self.click_and_hold(source)
        self.release(target)
        return self

    def drag_and_drop_by_offset(self, source, xoffset, yoffset):
        """
         - source: The element to mouse down.
         - xoffset: X offset to move to.
         - yoffset: Y offset to move to.
        """
        self.click_and_hold(source)
        self.move_by_offset(xoffset, yoffset)
        self.release()
        return self

    def key_down(self, value, element=None):

        if element:
            self.click(element)
        if self._driver.w3c:
            self.w3c_actions.key_action.key_down(value)
            self.w3c_actions.pointer_action.pause()
        else:
            self._actions.append(lambda: self._driver.execute(
                Command.SEND_KEYS_TO_ACTIVE_ELEMENT,
                {"value": keys_to_typing(value)}))
        return self

    def key_up(self, value, element=None):

        if element:
            self.click(element)
        if self._driver.w3c:
            self.w3c_actions.key_action.key_up(value)
            self.w3c_actions.pointer_action.pause()
        else:
            self._actions.append(lambda: self._driver.execute(
                Command.SEND_KEYS_TO_ACTIVE_ELEMENT,
                {"value": keys_to_typing(value)}))
        return self

    def move_by_offset(self, xoffset, yoffset):

        if self._driver.w3c:
            self.w3c_actions.pointer_action.move_by(xoffset, yoffset)
            self.w3c_actions.key_action.pause()
        else:
            self._actions.append(lambda: self._driver.execute(
                Command.MOVE_TO, {
                    'xoffset': int(xoffset),
                    'yoffset': int(yoffset)}))
        return self

    def move_to_element(self, to_element):

        if self._driver.w3c:
            self.w3c_actions.pointer_action.move_to(to_element)
            self.w3c_actions.key_action.pause()
        else:
            self._actions.append(lambda: self._driver.execute(
                                 Command.MOVE_TO, {'element': to_element.id}))
        return self

    def move_to_element_with_offset(self, to_element, xoffset, yoffset):

        if self._driver.w3c:
            self.w3c_actions.pointer_action.move_to(to_element, xoffset, yoffset)
            self.w3c_actions.key_action.pause()
        else:
            self._actions.append(
                lambda: self._driver.execute(Command.MOVE_TO, {
                    'element': to_element.id,
                    'xoffset': int(xoffset),
                    'yoffset': int(yoffset)}))
        return self

    def pause(self, seconds):

        if on_element:
            self.move_to_element(on_element)
        if self._driver.w3c:
            self.w3c_actions.pointer_action.release()
            self.w3c_actions.key_action.pause()
        else:
            self._actions.append(lambda: self._driver.execute(Command.MOUSE_UP, {}))
        return self

    def send_keys(self, *keys_to_send):

        typing = keys_to_typing(keys_to_send)
        if self._driver.w3c:
            for key in typing:
                self.key_down(key)
                self.key_up(key)
        else:
            self._actions.append(lambda: self._driver.execute(
                Command.SEND_KEYS_TO_ACTIVE_ELEMENT, {'value': typing}))
        return self

    def send_keys_to_element(self, element, *keys_to_send):

        self.click(element)
        self.send_keys(*keys_to_send)
        return self

    def __enter__(self):
        return self  # Return created instance of self.

    def __exit__(self, _type, _value, _traceback):
        pass  # Do nothing, does not require additional cleanup.

三、键盘操作

1)提交数据 elem.send_keys()

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(5)
driver.get("http://www.baidu.com")
e = driver.find_element_by_id("kw")
e.send_keys("zjx")

# 导入Keys,封装了键盘的按键,如下发送回车按键
e.send_keys(Keys.ENTER)

发送ctrl+c

e.send_keys(Keys.CTRL,'c')  

提交数据的三种方式:

--定位提交按钮,点击

--发送回车按钮

--数据在form表单中使用submit

import timefrom selenium import webdriver

driver = webdriver.Chrome()
driver.get("http://www.baidu.com")
elem= driver.find_element_by_id("kw")
elem.send_keys("zjx")
time.sleep(2)

#submit提交的数据要在form表单当中
elem.submit()

四、select操作

方法1:点击option元素(先定位到option元素再click)

方法2:使用Select类(先定位select元素,再进行选择value)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div>
    <p id="hello" onclick="alertMe()"> hello world </p>
    <P> select下拉框 </P>
    <script value="myselect">
    function alertMe(){
        alert("hello world")
    }
</script>
    <select name="myselect">
        <option value="xx">小雪</option>
        <option value="xy">小样儿</option>
        <option value="xz">小章</option>
        <option value="xll"> 小丽丽</option>
    </select>
</div>
</body>
</html>

 

import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.select import Select

driver = webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(5)
driver.get("http://localhost:63342/study/Lemon/demohtml.html?_ijt=sae0jk3u4qid1qlmev846b2mka")
# 第一种:直接定位option元素
xy = driver.find_element_by_xpath('//option[@value="xy"]')
xy.click()

# 第二种:使用Select
# 先定位到Select元素,在选择选项
select_elem = driver.find_element_by_name("myselect")
s = Select(select_elem)
# 通过value选择 s.select_by_value(
'xy')
# 通过可见文本选择 s.select_by_visible_text(
"小章")

 

posted @ 2020-10-16 16:15  尐樣ル~  阅读(261)  评论(0编辑  收藏  举报