Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify wait time between Actions when using Selenium Actionchains

The ActionChains is a very handy method when using Selenium. It works really well, only thing i am missing is how to insert wait times between the Actions.

I will take the same example from the official google Selenium Documentation. https://selenium.googlecode.com/git/docs/api/py/webdriver/selenium.webdriver.common.action_chains.html

menu = driver.find_element_by_css_selector(".nav")
hidden_submenu = driver.find_element_by_css_selector(".nav #submenu1")

ActionChains(driver).move_to_element(menu).click(hidden_submenu).perform()

What i am looking for is a way to insert wait times between the two actions

ActionChains(driver).move_to_element(menu)**(..wait some seconds)**.click(hidden_submenu).perform()

Thanks!

like image 542
Jonas Avatar asked Apr 12 '16 11:04

Jonas


People also ask

How to use ActionChains in selenium?

Action chain methods are used by advanced scripts where we need to drag an element, click an element, double click, etc. This article revolves around key_up method on Action Chains in Python Selenium. key_up method is used to release a pressed key using key_down method. value: The modifier key to send.

What is ActionChains?

ActionChains are a way to automate low level interactions such as mouse movements, mouse button actions, key press, and context menu interactions. This is useful for doing more complex actions like hover over and drag and drop. Generate user actions.


3 Answers

I tried this and seems working

from selenium import webdriver 

action = webdriver.ActionChains(driver)

action.pause(3)

action.perform()

driver.close()
like image 126
RobZ Avatar answered Nov 15 '22 08:11

RobZ


Note: Better answer

RobZ's answer is sufficient. The ActionChains class has a pause method that will do what was asked in the original question without requiring any subclassing or custom code.

Original answer

Here's a Python example based on Kim Homann's tip. It extends the ActionChains Selenium class to add a wait action.

import time
from selenium.webdriver import ActionChains

class Actions(ActionChains):
    def wait(self, time_s: float):
        self._actions.append(lambda: time.sleep(time_s))
        return self

Then your test becomes:

Actions(driver) \
    .move_to_element(menu) \
    .wait(2) \
    .click(hidden_submenu) \
    .perform()
like image 43
midopa Avatar answered Nov 15 '22 08:11

midopa


I don't know Python, but I think it's the same as in C#. I hope my code is readable for you.

You can create your own class ActionsEx deriving from Actions. Then you declare a method public Actions Wait(TimeSpan duration). Inside this method, you call AddAction(new SleepAction(duration));. AddAction() is a protected method of Selenium's Actions class, which is accessible only if you derive from this class.

SleepAction is a class implementing the IAction interface, which you have to create. It can look like this example:

public class SleepAction : IAction
{
    public SleepAction(TimeSpan duration)
    {
        _duration = duration;
    }

    private TimeSpan _duration;

    void IAction.Perform()
    {
        ToolBox.Sleep((int) _duration.TotalMilliseconds);
    }
}

ActionsEx class:

public class ActionsEx : Actions
{
    public ActionsEx(IWebDriver driver) : base(driver)
    {
    }

    public Actions Wait(TimeSpan duration)
    {
        AddAction(new SleepAction(duration));

        return this;
    }
}

Then you can call an action chain like this:

var actions = new ActionsEx(driver);
var duration = TimeSpan.FromSeconds(1);

((ActionsEx)actions
    .Wait(duration)
    .MoveToElement(element))
    .Wait(duration)
    .Click()
    .Build()
    .Perform();
like image 40
Kim Homann Avatar answered Nov 15 '22 08:11

Kim Homann