PowerShell Selenium WebDriver and ChromeDriver - FindElementBy() syntax differences

Viewed 395

I've followed the steps in this article https://adamtheautomator.com/selenium-powershell/ and a couple of others, to install Selenium WebDriver and ChromeDriver for use with PowerShell. Things are fine, however, I did struggle to implement the use of FindElementByXPath() or FindElementByCssSelector(). It didn't find a master documentation for using Selenium WebDriver and ChromeDriver form PowerShell. It seems there is some issue with the methods FindElementXXXXX() as they are not working in my case, although they are mentioned in the above-mentioned article.

After a lot of trial and error, I was able to implement FindElementBy() as follows:

$scriptPath = "C:\Projects\Selenium\Setup"
Add-Type -path "$scriptPath\webdriver.dll"

$ChromeOptions = $null
#$ChromeOptions = New-Object OpenQA.Selenium.Chrome.ChromeOptions
#$ChromeOptions.AddArgument("--user-data-dir=C:\Users\user_dir\AppData\Local\Google\Chrome\User Data\")

$chrome = New-Object OpenQA.Selenium.Chrome.ChromeDriver($ChromeOptions)
#$chrome = New-Object OpenQA.Selenium.Chrome.ChromeDriver
$usernameVal = "user-name"
$pwordVal = "password432"
$chrome.Navigate().GoToUrl("http://your-website-with-username-pwd.com")

Start-Sleep -s 6

$loginName = [OpenQA.Selenium.By]::CssSelector("#loginName-id")
$loginNameElm = $chrome.FindElement($loginName)
$loginNameElm.SendKeys($usernameVal)

$pword = [OpenQA.Selenium.By]::CssSelector("[a-id=password-input-xyz]")
$pwordElm = $chrome.FindElement($pword)
$pwordElm.SendKeys($pwordVal)

$btnLogin = [OpenQA.Selenium.By]::CssSelector("#ng-app > div.app.ng-scope > div > div:nth-child(2) > div > form > div > div.ng-isolate-scope > div > div > div > div > div.fis-action-bar.clearfix.ng-scope > div.fis-block > div.fis-primary-block > button")
#The CSS Selector below didn't work
#$btnLogin = [OpenQA.Selenium.By]::CssSelector("button:contains('Sign in')")
$btnLoginElm = $chrome.FindElement($btnLogin)
$btnLoginElm.click()

So as you can see the trick is to use the syntax [OpenQA.Selenium.By] namespace or class.

What really puzzled me is in all references to this form are using the syntax By.CssSelector() as in this page https://www.toolsqa.com/selenium-webdriver/find-element-selenium/.

Also, I tried to use CSS Selector format button:contains('Sign in') but it did throw an error although it is working using $() notation. If I used the other normal selector (by copying it from Chrome DevTools) it works fine. I was trying to avoid having a supper long css selector that looks funny.

So the question is:

  • Where is the main documentation for using Selenium from PowerShell?

  • Why there are different variations if the use of a method like FindElementBy and in other instance we see FindElementByXPath()?

  • How we can import the namespace[OpenQA.Selenium.By] to avoid typing the whole name? So that we use the syntax By.CssSelector() directly in PowerShell.

  • How can we locate a button with the value used for the caption? That is to avoid using a very long CSS Selector.

  • I don't like the option to use sleep in PowerShell to wait until Chrome is ready. Is there another better way?

0 Answers
Related