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.
IWebDriver driver = new ChromeDriver(ExeAppPath);
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);
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);
Press <Login> Button:
IWebElement buttonLogIn = driver.FindElement(By.Id(“page-outer”)).FindElement(By.ClassName(“submit”));
Navigate to <Followers> page:
Please note that the ExeAppPath variable has already been initialized with path where ChromeDriver was extracted.
private void ScrollToBottom(IWebDriver driver, Int32 scrolldelay)
long scrollHeight = 0;
var newScrollHeight = (long)js.ExecuteScript(“window.scrollTo(0, document.body.scrollHeight); return document.body.scrollHeight;”);
if (newScrollHeight == scrollHeight)
scrollHeight = newScrollHeight;
} 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:
IList<IWebElement> profiles = driver.FindElements(By.ClassName(“ProfileCard-screennameLink”));
foreach (IWebElement profile in profiles)
dr = dt.NewRow();
dr[“account”] = profile.Text;
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.
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 […]
NOW EXTENDED FOR CYBER MONDAY! Black Friday is always a popular time to shop for technology, but rather than buying an extra coffee machine, why not future-proof your organisation, greatly enhance your survival prospects and begin to reap the benefits of automation across your business? Take advantage of T-Impact’s unique Black Friday offer and get three […]