lazyindfw
lazyindfw

Reputation: 13

Powershell Selenium select a dropdown menu with FindElementByCssSelector

I'm using Selenium to attempt to open a webpage, enter login credentials, click logon button, click new change control option, then enter my name and select it from a dropdown. I have it all working until the dropdown portion. Here's a pic of my recording:

Pic

So here's the code I'm using (I'm using the Selenium module because I thought it would make life easier) I have my username and password obfuscated just so it doesn't show up in cleartext

$LandeskUrl = "The site I'm going to is here"

$Driver = Start-SeChrome
Enter-SeUrl $LandeskUrl -Driver $Driver

###Logon screen

#user name
$UserName = Find-SeElement -Driver $Driver -Id "Ecom_User_ID"
Send-SeKeys -Element $UserName -Keys $passCreds.UserName

#password field
$Password = Find-SeElement -Driver $Driver -Id "Ecom_User_Password"
Send-SeKeys -Element $Password -Keys $thePass

#click logon button
$LogonButton = Find-SeElement -Drive $Driver -Id "logonButton"
Invoke-SeClick -Element $LogonButton

##Landesk screen

#select "Create New Change Control"
$NewCCR = Find-SeElement -Drive $Driver -Id "shortcutItem_4765085e-1b8b-44a1-a896-883efb86f151"
Invoke-SeClick -Element $NewCCR

#select user field and type name
$User = Find-SeElement -Drive $Driver -Id "mainForm-RaiseUser2Display"
Send-SeKeys -Element $User -Keys "My user name here"

#THIS IS WHERE I'M HAVING TROUBLE
$dropDown = $Driver.FindElementByCssSelector(".dropdownItem:nth-child(2)");
Invoke-SeClick -Element $dropDown

Here's the HTML for the page I am testing, plus link https://drive.google.com/file/d/1mYHqApsGue1IE-Zm9Eqwz2aiXKk7jIO2/view:

<div class="dropdown" id="mainForm-RaiseUser2-Dropdown" style="height: 503px; left: 299px; top: 171px; width: 973px; display: none;">
    <div class="dropdownContent" style="height: 502.5px;"></div>
    <div class="dropdownFooter"></div>
</div>

And here's the output I get:

Exception calling "FindElementByCssSelector" with "1" argument(s): "no such 
element: Unable to locate element: {"method":"css 
selector","selector":".dropdownItem:nth-child(2)"}
  (Session info: chrome=77.0.3865.90)"
At C:\Scripts\SeleniumWebAutomation.ps1:36 char:1
+ $dropDown = $Driver.FindElementByCssSelector(".dropdownItem:nth-child ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : NoSuchElementException

Invoke-SeClick : Cannot bind argument to parameter 'Element' because it is null.
At C:\Scripts\SeleniumWebAutomation.ps1:37 char:25
+ Invoke-SeClick -Element $dropDown
+                         ~~~~~~~~~
+ CategoryInfo          : InvalidData: (:) [Invoke-SeClick], ParameterBindingV 
   alidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Invok 
   e-SeClick

Upvotes: 0

Views: 4301

Answers (3)

DagicCross
DagicCross

Reputation: 401

Late to the party but I found out this is working for me, probably works for you too.

$Value = '2'
$Option = Find-SeElement -Driver $Driver -XPath "/html/body/div/select"

$SelectElement = [OpenQA.Selenium.Support.UI.SelectElement]::new($Option)
$SelectElement.SelectByValue($Value)

My HTML element looks like this and will select Watermelon with the script above...

<select class="custom-select">
      <option selected="">Open this select menu</option>
      <option value="1">Banana</option>
      <option value="2">Watermelon</option>
      <option value="3">Mango</option>
</select>

Upvotes: 1

lazyindfw
lazyindfw

Reputation: 13

Thanks for your help, Christine! I dug into xpath usage a lot after this. After pulling my hair out and trying different things (including ditching the selenium module i found, incorporating Firebug, etc) I was able to get it to work by adding a Start-Sleep (wait) statement. It just wasn't finding the drop down because I wasn't waiting for it to pop up I guess.

Start-sleep -s 2
$ChromeDriver.FindElementByClassName("dropdownSelectedItem").click()

Now it works! Joy!

Upvotes: 0

CEH
CEH

Reputation: 5909

Edited to reflect comments you added to the original post relating to dropdown & dropdown options selectors.

Given the following HTML for a dropdown menu:

<div class="dropdownContent" style="height: 502.5px; overflow-y: auto;"><div class="dropdownItem" value="00000000-0000-0000-0000-000000000000" params="{}">

And the following HTML for a dropdown menu item:

<div class="dropdownItem dropdownSelectedItem" value="fcbe6ca8-1ab7-4083-ae17-075139afa876" params="{}">mylastname, myfirstname</div>

It looks like you want to expand a dropdown by clicking it, then click a dropdown option containing text lastname, firstname. Replacing your last two lines of code, here's how I would do that:

# First expand the dropdown
$dropDown = $Driver.FindElementByXPath("//div[@class='dropdownContent']");
Invoke-SeClick -Element $dropDown

# Not sure on the syntax but you may need to Invoke WebDriverWait here, before you can click element.

# Click the desired option
$dropdownOption = $Driver.FindElementByXPath("//div[text()='mylastname, myfirstname']");
Invoke-SeClick -Element $dropdownOption

Upvotes: 2

Related Questions