3

I'm going crazy with this, so any help provided is apprecaited.

I'm trying to use Selenium 2.44 (.NET 3.5) within Powershell v4. I'm using the ChromeWebDriver, and am using chromedriver.exe v2.13 (found here).

# Import the Selenium DLLs
Add-Type -Path "C:\mydir\Selenium.WebDriverBackedSelenium.dll"
Add-Type -Path "C:\mydir\ThoughtWorks.Selenium.Core.dll"
Add-Type -Path "C:\mydir\WebDriver.dll"
Add-Type -Path "C:\mydir\WebDriver.Support.dll"

$driver = New-Object OpenQA.Selenium.Chrome.ChromeDriver
$driver.Navigate().GoToUrl("https://my.login.site.com/")

$inputField = $driver.FindElementsById("input_1")
$inputField.SendKeys("My_Search_Query")

Everything I have listed there works great. However, as soon as I try to clear the input from my $inputField variable, I get this error:

$inputField.Clear()
##############
Cannot find an overload for "Clear" and the argument count: "0".
At line:1 char:1
+ $inputField.Clear()
+ ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest

However, if I pipe $inputField into the Get-Member cmdlet, I can clearly see there is a Clear() method:

> $inputField | Get-Member

   TypeName: OpenQA.Selenium.Remote.RemoteWebElement

Name                                 MemberType Definition
----                                 ---------- ----------
Clear                                Method     void Clear(), void IWebElement.Clear()
Click                                Method     void Click(), void IWebElement.Click()
Equals                               Method     bool Equals(System.Object obj)
. . .

I've tried looking in the OpenQA.Selenium.Interactions.Actions namespace along with [System.Windows.Forms.SendKeys] namespace, and that doesn't work.

$actions = New-Object OpenQA.Selenium.Interactions.Actions($driver)
Add-Type -AssemblyName System.Windows.Forms
$actions.SendKeys($inputField,[System.Windows.Forms.SendKeys]::SendWait("{BACKSPACE}"))

Cannot find an overload for "SendKeys" and the argument count: "2".
At line:1 char:1
+ $actions.SendKeys($inputField,[System.Windows.Forms.SendKeys]::SendWait("{BACKSP ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest

Any suggestions or help would be greatly appreciated. Thank you.

2
  • 4
    Very quick look...FindElementsById...."elements"? Are you sure it's not returning an array? Should it not be FindElementById?
    – Arran
    Jan 13, 2015 at 22:39
  • 2
    !%@#$^ Genius! FindElementById returns what I want, an OpenQA.Selenium.IWebElement, and FindElementsById returns a System.Collections.ObjectModel.ReadOnlyCollection. Somehow everything was still working, but yes, as soon as I changed it, Clear() worked immediately. I've been working on this all damn day! Thank you so much!
    – romellem
    Jan 13, 2015 at 22:48

2 Answers 2

3

I tried this and I think Clear is doing what you expect.

$driver.FindElementsById("input_1").Clear

Will give you

OverloadDefinitions                                        
-------------------                                        
void ICollection[IWebElement].Clear()                      
void IList.Clear() 

I think clear will be called on the collection and not the element because FindElementsById returns a list.

If you try

$driver.FindElementsById("input_1") | % { $_.Clear }

Then you will get the Clear method you want.

OverloadDefinitions                     
-------------------                     
void Clear()                            
void IWebElement.Clear() 

I hope it helps.

0

Might have something to do with the way that you are navigating to your link. This works for me, give it a go.

$driver = Start-SeChrome -StartURL "https://my.login.site.com/"
$driver.FindElementById('input_1').SendKeys("My_Search_Query");
$driver.FindElementById('input_1').clear()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.