【问题标题】:Selenium action chain replicates mouse movementsSelenium 动作链复制鼠标动作
【发布时间】: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

【问题讨论】:

    标签: python selenium selenium-webdriver selenium-chromedriver


    【解决方案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()
    

    【讨论】:

      猜你喜欢
      • 2016-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-10
      • 1970-01-01
      • 1970-01-01
      • 2017-04-26
      • 1970-01-01
      相关资源
      最近更新 更多