Selenium 动作链复制鼠标动作

Posted

技术标签:

【中文标题】Selenium 动作链复制鼠标动作【英文标题】:Selenium action chain replicates mouse movements 【发布时间】:2020-06-11 22:54:03 【问题描述】:

当我传递 text="one" 时,它第一次运行良好,但第二次复制了我的操作两次。我该如何解决这个问题?

这是我的代码,

def err(wait, driver):
    text = ""
    print("Please say right to move element , say ok to place element.")
    wait.until(EC.element_to_be_clickable((By.XPATH,'//*[@id="captcha-box"]/div/div[2]/div[1]/div[3]'))).click()
    element = wait.until(EC.element_to_be_clickable((By.XPATH,'//*[@id="body"]/div[6]/div[2]/div[1]/div/div[1]/div[2]/div[2]')))
    action= ActionChains(driver)
    action.click_and_hold(element)
    while text != "exit":

        r = sr.Recognizer()
        with sr.Microphone() as source:
            audio = r.listen(source)
            try:
                text = r.recognize_google(audio)
                print("You said : ".format(text))

                if "one" in text:                           
                    action.move_by_offset(10, 0)
                    action.perform()
                elif "two" in text:                           
                    action.move_by_offset(20, 0)
                    action.perform()
                elif "back" in text:                           
                    action.move_by_offset(-10, 0)
                    action.perform()
                elif "ok" in text:
                    action.release(element)
                    action.perform()
                    break
                else:
                    print("Not a valid command (Error)")

            except:
                print("Sorry could not recognize what you said ( error page )")

动作链:

from selenium.webdriver.common.action_chains import ActionChains

【问题讨论】:

【参考方案1】:

issue 已经修复,perform 之后的操作不清楚,并重复之前添加的所有操作。现在,您必须在循环内初始化 ActionChains 并使用 reset_actions

def err(wait, driver):
    text = ""
    print("Please say right to move element , say ok to place element.")
    wait.until(EC.element_to_be_clickable((By.XPATH,'//*[@id="captcha-box"]/div/div[2]/div[1]/div[3]'))).click()
    element = wait.until(EC.element_to_be_clickable((By.XPATH,'//*[@id="body"]/div[6]/div[2]/div[1]/div/div[1]/div[2]/div[2]')))
    while text != "exit":

        action= ActionChains(driver)
        action.click_and_hold(element)

        r = sr.Recognizer()
        with sr.Microphone() as source:
            audio = r.listen(source)
            try:
                text = r.recognize_google(audio)
                print("You said : ".format(text))

                if "one" in text:                           
                    action.move_by_offset(10, 0)
                    action.perform()
                elif "two" in text:                           
                    action.move_by_offset(20, 0)
                    action.perform()
                elif "back" in text:                           
                    action.move_by_offset(-10, 0)
                    action.perform()
                elif "ok" in text:
                    action.release(element)
                    action.perform()
                    break
                else:
                    print("Not a valid command (Error)")

            except:
                print("Sorry could not recognize what you said ( error page )")

        action.reset_actions() #Fixed error

【讨论】:

谢谢@Sers。但它不会解决我的问题。现在它忘记了当前位置并从头开始【参考方案2】:

这在一定程度上解决了我的问题

我使用 location ( int ) 来记住之前的位置。

def err(wait, driver):
    text = ""
    print("Please say right to move element , say ok to place element.")
    wait.until(EC.element_to_be_clickable(
        (By.XPATH, '//*[@id="captcha-box"]/div/div[2]/div[1]/div[3]'))).click()
    element = wait.until(EC.element_to_be_clickable(
        (By.XPATH, '//*[@id="body"]/div[6]/div[2]/div[1]/div/div[1]/div[2]/div[2]')))
    location=10
    while text != "exit":

        action = ActionChains(driver)
        action.click_and_hold(element)

        r = sr.Recognizer()
        with sr.Microphone() as source:
            audio = r.listen(source)
            try:
                text = r.recognize_google(audio)
                print("You said : ".format(text))

                if "one" in text:
                    action.move_by_offset(location, 0)
                    action.perform()
                    location=location+10

                elif "two" in text:
                    location=location+10
                    action.move_by_offset(location, 0)
                    action.perform()
                    location=location+20

                elif "tree" in text:
                    location=location+20
                    action.move_by_offset(location, 0)
                    action.perform()
                    location=location+30

                elif "back" in text:
                    action.move_by_offset(-10, 0)
                    action.perform()

                elif "ok" in text:
                    action.release(element)
                    action.perform()
                    break
                else:
                    print("Not a valid command (Error)")

            except:
                print("Sorry could not recognize what you said ( error page )")
        action.reset_actions()

【讨论】:

以上是关于Selenium 动作链复制鼠标动作的主要内容,如果未能解决你的问题,请参考以下文章

selenium ActionChains(动作链常用API)

Python爬虫编程思想(98):使用Selenium实现动作链(组合拳控制浏览器)

selenium处理iframe和动作链

Selenium-Selenium处理iframe处理及动作链

selenium自动化鼠标操作

爬虫04 /asyncioselenium规避检测动作链无头浏览器