Selenium Webdriver C#Sendkeys(Keys.Arrowdown)
我正在尝试使用Selenium Webdriver / C#compile做一个箭头,但是当我尝试编译时,我得到了这个错误:
‘Keys’是’OpenQA.Selenium.Keys’和’System.Windows.Forms.Keys’之间的模糊参考(CS0104)
我的代码:
driver.FindElement(By.Id("ctl00_PlaceHolderMain_ctrlChangeBillingAddress_ctrlChangeBillingAddress_txtBillingAddress")).SendKeys(Keys.ArrowDown); driver.FindElement(By.Id("ctl00_PlaceHolderMain_ctrlChangeBillingAddress_ctrlChangeBillingAddress_txtBillingAddress")).SendKeys(Keys.Enter);
正如错误所述,两个不同的命名空间中有两种不同的Keys
类型。
您需要通过编写OpenQA.Selenium.Keys
来明确限定类型。
我可以为您提供两种实现,但第一种只能在本地使用:
-
Element.SendKeys(OpenQA.Selenium.Keys.ArrowUp);
-
char c = '\uE013'; // ASCII code ArrowUp
Element.SendKeys(Convert.ToString(c));
我的代码也发生了同样的事情。 就像在我的注册中一样,1。我有一个地址字段,从谷歌搜索中获取输入的地址,然后填写相应的字段,如:子urb,城市,邮政编码等.2。有一个按钮附加一个文件(比如从桌面浏览并选择要附加的任何图像或文档)我收到错误“’Keys’是OpenQA.Selenium.Keys
和'System.Windows.Forms.Keys' (CS0104)
之间的模糊参考'System.Windows.Forms.Keys' (CS0104)
然后我意识到它意味着在两个不同的命名空间中有两种不同的Keys类型.Coz用于地址选择,我的代码是:
driver.FindElement(By.XPath("//*[@id='PostalAddress_Address']")).SendKeys(Address); //Address to select from autofill and fill textboxes accordingly Thread.Sleep(500); driver.FindElement(By.XPath("//*[@id='PostalAddress_Address']")).SendKeys(Keys.ArrowDown); driver.FindElement(By.XPath("//*[@id='PostalAddress_Address']")).SendKeys(Keys.Enter);
对于附加文件,代码是:
//Select and attach file from the computer driver.FindElement(By.XPath("//*[@id='graduate-education']/div[4]/label")).Click(); //Click Attach file button Thread.Sleep(500); //driver.FindElement(By.XPath("//*[@id='graduate-education']/div[4]/label")).SendKeys(AttachFile); SendKeys.SendWait(@"Complete File Path"); //Select the file from the location Thread.Sleep(500); SendKeys.SendWait(@"{Enter}");
添加的命名空间是:
using OpenQA.Selenium; using System; using System.Threading; using System.Windows.Forms;
因为 – Keys类型无法识别它实际所属的位置,所以我不得不更改地址字段的代码并使用OpenQA.Selenium.keys.ArrowDown如下:
driver.FindElement(By.XPath("//*[@id='PostalAddress_Address']")).SendKeys(Address); //Address to select from autofill and fill textboxes accordingly Thread.Sleep(500); driver.FindElement(By.XPath("//*[@id='PostalAddress_Address']")).SendKeys(OpenQA.Selenium.Keys.ArrowDown); driver.FindElement(By.XPath("//*[@id='PostalAddress_Address']")).SendKeys(OpenQA.Selenium.Keys.Enter);
这对我有用,希望对你也一样
我建议下一步做:
IWebElement element = driver.FindElement(By.Id("ctl00_PlaceHolderMain_ctrlChangeBillingAddress_ctrlChangeBillingAddress_txtBillingAddress")); OpenQA.Selenium.Interactions.Actions action = new OpenQA.Selenium.Interactions.Actions(driver); action.SendKeys(element, Keys.Down).SendKeys(element, Keys.Enter).Build().Perform();