Selenium action chain replicates mouse movements

Hishan_98 :

when I pass text="one", it works fine for the first time but it replicates my action twice the second time. How can I resolve this?

This is my code,

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 )")

Action chain:

from selenium.webdriver.common.action_chains import ActionChains
Sers :

There's the issue already fixed, actions not clear after perform and repeat all actions previously added. For now, you have to init ActionChains inside the loop and use 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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=16612&siteId=1