最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Can I make selenium pause for input and resume on a trigger? - Stack Overflow

programmeradmin3浏览0评论

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?

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?

Share Improve this question edited Jun 21, 2018 at 7:13 Gunnar Thoreson 3131 silver badge7 bronze badges asked Jun 13, 2018 at 19:50 dbzadbza 3261 gold badge5 silver badges19 bronze badges 6
  • What's the point of testing if captcha works? That's an external library tested externally ... you are meant to test your own stuff. You should mock the captcha thing imo and fully automate your tests – Protozoid Commented Jun 13, 2018 at 20:30
  • @protozoid This is for automation, not testing. – dbza Commented Jun 13, 2018 at 21:23
  • So is another browser with a webpage which has buttons like Action 1, Action 2 work for you? – Tarun Lalwani Commented Jun 17, 2018 at 7:39
  • What are you using selenium with?? Python java etc?? – Sahil Agarwal Commented Jun 20, 2018 at 13:18
  • @SahilAgarwal Python tarun it would work too – dbza Commented Jun 21, 2018 at 20:09
 |  Show 1 more comment

3 Answers 3

Reset to default 9

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

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.

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/

发布评论

评论列表(0)

  1. 暂无评论