将Selenium与Powershell结合使用-清除输入字段 [英] Using Selenium with Powershell - Clearing an input field
问题描述
对此我感到非常疯狂,因此请提供所提供的任何帮助.
I'm going crazy with this, so any help provided is apprecaited.
我正在尝试在Powershell v4中使用Selenium 2.44(.NET 3.5).我正在使用ChromeWebDriver,并正在使用chromedriver.exe v2.13(在此处).
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")
我在那里列出的所有内容都很有效.但是,一旦我尝试清除来自我的 $ inputField
变量的输入,就会收到此错误:
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
但是,如果我将 $ inputField
用管道传输到 Get-Member
cmdlet中,则可以清楚地看到有一个Clear()方法:
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)
. . .
我已经尝试在 OpenQA.Selenium.Interactions.Actions
命名空间以及 [System.Windows.Forms.SendKeys]
命名空间中进行查找,但这并不是工作.
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.
推荐答案
我尝试了此操作,我认为Clear可以满足您的期望.
I tried this and I think Clear is doing what you expect.
$driver.FindElementsById("input_1").Clear
会给你
OverloadDefinitions
-------------------
void ICollection[IWebElement].Clear()
void IList.Clear()
我认为clear将在集合上而不是在元素上调用,因为FindElementsById返回一个列表.
I think clear will be called on the collection and not the element because FindElementsById returns a list.
如果您尝试
$driver.FindElementsById("input_1") | % { $_.Clear }
然后,您将获得所需的Clear方法.
Then you will get the Clear method you want.
OverloadDefinitions
-------------------
void Clear()
void IWebElement.Clear()
我希望它会有所帮助.
这篇关于将Selenium与Powershell结合使用-清除输入字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!