1. RPA Challenge - Dynamic Web Page - Anchor

@badita

Hello Mihai,
excellent :slightly_smiling_face:

Here another solution with Selenium and PowerShell:

#-Begin-----------------------------------------------------------------
#-
#- Checked with Google Chrome 86.0.4240.198 at 18.11.2020
#-
#-----------------------------------------------------------------------

#-Sub LoadSelenium------------------------------------------------------
Function LoadSelenium {
  $Script:Path = "E:\Program Files\Selenium";
  [Void][System.Reflection.Assembly]::LoadFrom("$Script:Path\WebDriver.dll");
  [Void][System.Reflection.Assembly]::LoadFrom("$Script:Path\WebDriver.Support.dll");
}

#-Sub Main--------------------------------------------------------------
Function Main() {

  $Options = New-Object OpenQA.Selenium.Chrome.ChromeOptions;
  $Options.BinaryLocation = "E:/Program Files/Google/Chrome/Application/chrome.exe";
  $Options.AddArguments("--window-size=1536,1024");
  #With the headless argument you can save approximately up to 30%
  #$Options.AddArguments("--window-size=1536,1024", "--headless");

  $Script:Driver = New-Object OpenQA.Selenium.Chrome.ChromeDriver($Script:Path, $Options);

  $Excel = New-Object -ComObject "Excel.Application";
  #$Excel.Visible = $True;
  $WorkBook = $Excel.Workbooks.Open("E:\Projects\Selenium\RPAChallenges.xlsx");
  $WorkSheet = $WorkBook.Sheets.Item("Sheet1");

  $baseURL = "http://rpachallenge.com/";
  $Script:Driver.Navigate().GoToUrl($baseURL); 
  [OpenQA.Selenium.Support.UI.WebDriverWait]$Wait = New-Object OpenQA.Selenium.Support.UI.WebDriverWait($driver, [System.TimeSpan]::FromSeconds(10));
  [Void]$Wait.Until([OpenQA.Selenium.Support.UI.ExpectedConditions]::ElementToBeClickable([OpenQA.Selenium.By]::XPath("//button[contains(text(),'Start')]")));
  $Script:Driver.FindElementByXPath("//button[contains(text(),'Start')]").Click();

  [Void]$Wait.Until([OpenQA.Selenium.Support.UI.ExpectedConditions]::ElementToBeClickable([OpenQA.Selenium.By]::Id("start")));
  $Script:Driver.FindElementById("start").Click();

  For($i = 2; $i -le 11; $i++) { #Row
    [Void]$Wait.Until([OpenQA.Selenium.Support.UI.ExpectedConditions]::ElementIsVisible([OpenQA.Selenium.By]::XPath("//input[@type='submit' and @value='Submit']")));

    $Script:Driver.FindElementByXPath("//label[text()='First Name']/parent::*/input[1]").SendKeys($WorkSheet.Cells.Item($i, 1).Value2);
    $Script:Driver.FindElementByXPath("//label[text()='Last Name']/parent::*/input[1]").SendKeys($WorkSheet.Cells.Item($i, 2).Value2);
    $Script:Driver.FindElementByXPath("//label[text()='Company Name']/parent::*/input[1]").SendKeys($WorkSheet.Cells.Item($i, 3).Value2);
    $Script:Driver.FindElementByXPath("//label[text()='Role in Company']/parent::*/input[1]").SendKeys($WorkSheet.Cells.Item($i, 4).Value2);
    $Script:Driver.FindElementByXPath("//label[text()='Address']/parent::*/input[1]").SendKeys($WorkSheet.Cells.Item($i, 5).Value2);
    $Script:Driver.FindElementByXPath("//label[text()='Email']/parent::*/input[1]").SendKeys($WorkSheet.Cells.Item($i, 6).Value2);
    $Script:Driver.FindElementByXPath("//label[text()='Phone Number']/parent::*/input[1]").SendKeys($WorkSheet.Cells.Item($i, 7).Value2);

    $Script:Driver.FindElementByXPath("//input[@type='submit' and @value='Submit']").Click();
  }

  $WorkBook.Close();
  $Excel.Quit()
  [Void][System.Runtime.Interopservices.Marshal]::ReleaseComObject($Excel);

  Start-Sleep -Seconds 10;

  $Script:driver.Close();
  $Script:driver.Quit();

}

#-Main------------------------------------------------------------------
LoadSelenium;
Main;

#-Error Routine---------------------------------------------------------
Trap {
  Write-Host "Fehler" $_.Exception.GetType().FullName `
    "in Zeile" $_.InvocationInfo.ScriptLineNumber `
    `r`n $_.Exception.Message;
}

#-End-------------------------------------------------------------------

rpachallange

Here a solution for Google Chrome and Microsoft Edge which uses instead Excel a CSV file.

Here the CSV file: RPAChallenges.zip (695 Bytes)

#-Begin-----------------------------------------------------------------
#-
#- Checked with Google Chrome 86.0.4240.198 at 18.11.2020
#- Checked with Microsoft Edge 87.0.664.41 at 18.11.2020
#-
#-----------------------------------------------------------------------

#-Sub LoadSelenium------------------------------------------------------
Function LoadSelenium {
  $Script:Path = "E:\Program Files\Selenium";
  [Void][System.Reflection.Assembly]::LoadFrom("$($Script:Path)\WebDriver.dll");
  [Void][System.Reflection.Assembly]::LoadFrom("$($Script:Path)\WebDriver.Support.dll");
  [Void][System.Reflection.Assembly]::LoadFrom("$($Script:Path)\Microsoft.Edge.SeleniumTools.dll");
}

#-Sub Main--------------------------------------------------------------
Function Main() {

  #-Chrome--------------------------------------------------------------
  $Options = New-Object OpenQA.Selenium.Chrome.ChromeOptions;
  $Options.BinaryLocation = "E:/Program Files/Google/Chrome/Application/chrome.exe";
  $Options.AddArguments("--window-size=1536,1024", "--headless");

  $Script:Driver = New-Object OpenQA.Selenium.Chrome.ChromeDriver($Script:Path, $Options);

  #-Edge----------------------------------------------------------------
  #$Options = New-Object Microsoft.Edge.SeleniumTools.EdgeOptions;
  #$Options.UseChromium = $True;
  #$Options.AddArgument("headless");

  #$Script:Driver = New-Object Microsoft.Edge.SeleniumTools.EdgeDriver($Options);
  #$Script:Driver.Manage().Window.Size = [System.Drawing.Size]::new(1536, 1024);

  $WorkSheet = Import-Csv -Delimiter ";" -Path "E:\Projects\Selenium\RPAChallenges.csv";

  $baseURL = "http://rpachallenge.com/";
  $Script:Driver.Navigate().GoToUrl($baseURL); 
  [OpenQA.Selenium.Support.UI.WebDriverWait]$Wait = New-Object OpenQA.Selenium.Support.UI.WebDriverWait($driver, [System.TimeSpan]::FromSeconds(10));
  [Void]$Wait.Until([OpenQA.Selenium.Support.UI.ExpectedConditions]::ElementToBeClickable([OpenQA.Selenium.By]::XPath("//button[contains(text(),'Start')]")));
  $Script:Driver.FindElementByXPath("//button[contains(text(),'Start')]").Click();

  ForEach($Line in $WorkSheet) { #Row
    [Void]$Wait.Until([OpenQA.Selenium.Support.UI.ExpectedConditions]::ElementIsVisible([OpenQA.Selenium.By]::XPath("//input[@type='submit' and @value='Submit']")));

    $Script:Driver.FindElementByXPath("//label[text()='First Name']/parent::*/input[1]").SendKeys($Line.'First Name');
    $Script:Driver.FindElementByXPath("//label[text()='Last Name']/parent::*/input[1]").SendKeys($Line.'Last Name');
    $Script:Driver.FindElementByXPath("//label[text()='Company Name']/parent::*/input[1]").SendKeys($Line.'Company Name');
    $Script:Driver.FindElementByXPath("//label[text()='Role in Company']/parent::*/input[1]").SendKeys($Line.'Role in Company');
    $Script:Driver.FindElementByXPath("//label[text()='Address']/parent::*/input[1]").SendKeys($Line.'Address');
    $Script:Driver.FindElementByXPath("//label[text()='Email']/parent::*/input[1]").SendKeys($Line.'Email');
    $Script:Driver.FindElementByXPath("//label[text()='Phone Number']/parent::*/input[1]").SendKeys($Line.'Phone Number');

    $Script:Driver.FindElementByXPath("//input[@type='submit' and @value='Submit']").Click();
  }

  [OpenQA.Selenium.Screenshot]$ScreenShot = $Script:Driver.GetScreenshot();
  $ScreenShot.SaveAsFile("E:\Projects\Selenium\RPAChallenges.jpg", [OpenQA.Selenium.ScreenshotImageFormat]::Jpeg);

  Start-Sleep -Seconds 5;

  $Script:Driver.Close();
  $Script:Driver.Quit();

}

#-Main------------------------------------------------------------------
LoadSelenium;
Main;

#-Error Routine---------------------------------------------------------
Trap {
  Write-Host "Fehler" $_.Exception.GetType().FullName `
    "in Zeile" $_.InvocationInfo.ScriptLineNumber `
    `r`n $_.Exception.Message;
}

#-End-------------------------------------------------------------------

The using of the CSV file saves approximately up to 8%.

Best regards
Stefan

5 Likes