Integrating UiPath with Webdriver

February 21, 2018 | Robotics

Integrating UIPath

The time may come where you need some functionalities that UiPath does not have yet or requires a high level of effort to implement – like scrolling to the end of a growing webpage.

We will use scraping Twitter followers as a working example.

First, scrolling to the end of a page after attaching to a web browser window is solved simply by sending the <End> hotkey to a browser window. But how many times you must send the <End> key, and how long after then with the key be ignored before the next chunk of data is loaded?

Since 2017 W3C published new recommendations for remote control interfaces for browsers: WebDriver W3C Candidate Recommendation. Google integrated a remote-control mechanism in their Chrome browser: ChromeDriver – WebDriver for Chrome. And last piece of the puzzle is Selenium’s WebDriver for Chrome that can also be downloaded from NuGet website. Having all those tools is now extremely easy to implement a sample UiPath custom activity that make use of web driver capabilities embedded into Chrome web browser.

Before starting coding we need to download ChromeDriver and extract it in a path that will later be used by our custom activity / robot.

After creating new project in Visual Studio we have to install the  Selenium.WebDriver.ChromeDriver NuGet (e.g. type in Package Manager Console Install-Package Selenium.WebDriver.ChromeDriver -Version 2.35.0) and then we can use the web driver to control our browser.

Open browser:

IWebDriver driver = new ChromeDriver(ExeAppPath);

driver.Navigate().GoToUrl(“https://www.twitter.com/login”);

driver.Manage().Window.Maximize();

Type the UserName (variable _username):

IWebElement email = driver.FindElement(By.ClassName(“js-username-field”));

Actions builder = new Actions(driver);

Actions seriesOfActions = builder.MoveToElement(email).Click().SendKeys(email, _username);

seriesOfActions.Perform();

Type the Password (variable _password):

IWebElement password = driver.FindElement(By.ClassName(“js-password-field”));

builder = new Actions(driver);

seriesOfActions = builder.MoveToElement(password).Click().SendKeys(password, _password);

seriesOfActions.Perform();

Press <Login> Button:

IWebElement buttonLogIn = driver.FindElement(By.Id(“page-outer”)).FindElement(By.ClassName(“submit”));

buttonLogIn.Click();

Navigate to <Followers> page:

driver.Navigate().GoToUrl(“https://www.twitter.com/followers”);

Please note that the ExeAppPath variable has already been initialized with path where ChromeDriver was extracted.

The solution that we selected for this example for scrolling the page to the end is to run JavaScript code remotely in the controlled browser instance (https://stackoverflow.com/a/41335111). The delay in the function can be extracted as parameter for the custom activity for now.

private void ScrollToBottom(IWebDriver driver, Int32 scrolldelay)

{

long scrollHeight = 0;

do

{

       IJavaScriptExecutor js = (IJavaScriptExecutor)driver;

       var newScrollHeight = (long)js.ExecuteScript(“window.scrollTo(0, document.body.scrollHeight); return document.body.scrollHeight;”);

       if (newScrollHeight == scrollHeight)

       {

              break;

}

       else

       {

              scrollHeight = newScrollHeight;

              Thread.Sleep(scrolldelay);

}

      } while (true);

}

We call <ScrollToBottom> function after navigating to Followers page and then extract all followers also using web driver functionalities. At the end we close the browser and release the driver:

ScrollToBottom(driver, _scrolldelay);

IList<IWebElement> profiles = driver.FindElements(By.ClassName(“ProfileCard-screennameLink”));

DataRow dr;

foreach (IWebElement profile in profiles)

{

dr = dt.NewRow();

       dr[“account”] = profile.Text;

       dt.Rows.Add(dr);

}

driver.Close();

driver.Quit();

The DataTable produced in this example can be used as <Out> parameter for the custom activity so we can use the followers list in our robot. So the activity will receive UserName, Password and ScrollDelay as input arguments and will produce a DataTable containing all followers for that Twitter account.

Defensive programming: Feel free to put together all these pieces in a custom activity while not forgetting to gracefully end the activity execution in case of an error; Going further you can have an output parameter that robot can check as soon as activity terminates to determine if an error happened.

Related Articles

IBM & UiPath partner to offer the ultimate automation solution

May 15, 2019 | Digital Transformation | Robotics

IBM provides the world’s leading workflow solutions. Their business process management suite (BPMS) solution enables organisations to manage complex processes that span many organisations and IT systems. It supports processes that can run for long periods of time and provides dashboards to monitor and track processes, which is especially useful when there are numerous instances […]

Introducing Our Robots

January 02, 2019 | Robotics

Good morning and a Happy New Year to all our readers. T-Impact are back in 2019 with a brand new website and we’re looking forward to lighting the way for more successful digital transformations and robotics implementations across the globe. When developing our new site that our old website never answered one important question. Have you […]

LawNet 2018 – Congratulations!

November 29, 2018 | Robotics

Last week, T-Impact had the pleasure of presenting the LawNet Enterprise Award to Gardner Leader LLP at the 2018 Lawnet Annual Conference and Awards Evening Gala at the Hinckley Island hotel in Leicester.