Can I make selenium pause for input and resume on a trigger?

All we need is an easy explanation of the problem, so here it is.

I am wondering if selenium can do below? I want to automate only certain parts of the automation flow:

  1. Load a web page (built with angular), submit the form with some
    predefined inputs
  2. On the next page, automatically fill in some
    data like earlier, but wait for me to fill in some input on specific
    input fields (can’t hard-code this data)
  3. After this, a trigger (like a button
    press or key combination; outside of the web page) should carry on with the rest of the automated flow and land
    in page 3 and 4 and so on.

The only option I am familiar with is to write and run custom JS that modifies form elements, in the browser>inspect>console. For above, I’ll have to run different functions on each page. For doing this, I can comment out all but the required function call and run it. I think I cannot select and run only one part of the code (for page 1 for example) from the console.

PS: If any of the strict SO folks think this is not fitting SO, where else is a good (automation focused?) place to ask for finding the right tools for this kind of stuff?

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

Option 1:

This can be easily achieved using Explicit wait. Suppose you want to manually enter data in some field. You can make the selenium wait till the point where the field contains a value(Its “value” attribute is not empty). Ex:

WebDriverWait wait = new WebDriverWait(driver, 100);
//whatever time you think is sufficient for manually entering the data.
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id(>someid>)));
if(ExpectedConditions.attributeToBeNotEmpty(element,"value"))
{
  //continue with the automation flow
}

Option 2:

It is kinda hacky. What you can do is, At the beginning of the execution open another tab and then switch back to your original one, like this:

driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
driver.switchTo().defaultContent();

Now execution will start and at the point where you want the script to stop for you to manually enter your data, retrieve all the tabs from selenium in an infinite loop like this-

for(int i =1;i>0;i++)
{
 ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
 if(tabs.size()==2)
 {
  //keep checking for the point when the number of tabs becomes 1 again.
  continue;
 }
 else
 {
  break;
 }
}
//your rest of the automation code

The idea is to make selenium pause the execution(since it will be stuck in the loop) till the point where number of tabs again become 1. During this, you enter your data and close the empty tab so that the selenium can continue its execution.

You can also try this.

Method 2

Note: I have used selenium via python, so solution reflects that.

Oh yeah. It’s just a python script. Don’t think of it in terms if selenium script. A python script can be easily made to wait for input.

print("Hi!. Script Started")
# code to load webpage, automatically fill whatever can be entered
x = input("Waiting for manual date to be entered. Enter YES when done.")
# Enter the data on page manually. Then come back to terminal and type YES and then press enter.
if x == 'YES':
    continue_script_here()
else:
    kill_script_or_something_else()

Method 3

There are several waits available in selenium.

Implicit Wait: During Implicit wait if the Web Driver cannot find it immediately because of its availability, it will keep polling (around 250 milli seconds) the DOM to get the element. If the element is not available within the specified Time an NoSuchElementException will be raised. The default setting is zero. Once we set a time, the Web Driver waits for the period of the WebDriver object instance.

Explicit Wait: There can be instance when a particular element takes
more than a minute to load. In that case you definitely not like to
set a huge time to Implicit wait, as if you do this your browser will
going to wait for the same time for every element.

To avoid that situation you can simply put a separate time on the
required element only. By following this your browser implicit wait
time would be short for every element and it would be large for
specific element.

Fluent Wait: Let’s say you have an element which sometime appears in
just 1 second and some time it takes minutes to appear. In that case
it is better to use fluent wait, as this will try to find element
again and again until it find it or until the final timer runs out.

https://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp

http://toolsqa.com/selenium-webdriver/implicit-explicit-n-fluent-wait/

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply