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.
Introduction Almost every leadership book ever written talks about the importance of delegation and surrounding yourself with brilliant people who are each experts in their own discipline. For a long time, this was the prevailing attitude from Managing Partners towards technology. But when does technology cease to be peripheral and become a core competence? When […]
The best law firms understand that they have to treat their employees as customers, and that means really understanding what matters to them. This article will explore some of those key drivers and how the best firms are turning them to their advantage. 1 – Smart lawyers know that the future rests with the firms […]
Many Local Governments stand on the brink of bankruptcy after years of underfunding and budget cuts. Employees are overworked and underpaid, whilst Chief Executives are forced to make difficult decisions which affect the lives of their constituents. T-Impact have identified six key factors that are changing Local Government and senior staff need to adapt to […]