Selenium Webdriver C# Sendkeys (Keys.Arrowdown)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10867695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 15:35:54  来源:igfitidea点击:

Selenium Webdriver C# Sendkeys (Keys.Arrowdown)

c#seleniumwebdriver

提问by automationguy

I'm trying to do do an arrow using Selenium Webdriver/C# compile but when I try to compile I get this error:

我正在尝试使用 Selenium Webdriver/C# compile 做一个箭头,但是当我尝试编译时出现此错误:

'Keys' is an ambiguous reference between 'OpenQA.Selenium.Keys' and 'System.Windows.Forms.Keys' (CS0104)

'Keys' 是 'OpenQA.Selenium.Keys' 和 'System.Windows.Forms.Keys' (CS0104) 之间的模糊引用

My code:

我的代码:

driver.FindElement(By.Id("ctl00_PlaceHolderMain_ctrlChangeBillingAddress_ctrlChangeBillingAddress_txtBillingAddress")).SendKeys(Keys.ArrowDown);
driver.FindElement(By.Id("ctl00_PlaceHolderMain_ctrlChangeBillingAddress_ctrlChangeBillingAddress_txtBillingAddress")).SendKeys(Keys.Enter);

回答by SLaks

As the error states, there are two different Keystypes in two different namespaces.

正如错误所述,Keys两个不同的命名空间中有两种不同的类型。

You need to unambiguously qualify the type by writing OpenQA.Selenium.Keys.

您需要通过编写OpenQA.Selenium.Keys.

回答by user2742238

I can provide you two realizations, but the first one works only locally:

我可以为您提供两个实现,但第一个只能在本地工作:

  1. Element.SendKeys(OpenQA.Selenium.Keys.ArrowUp);

  2. char c = '\uE013'; // ASCII code ArrowUp

    Element.SendKeys(Convert.ToString(c));

  1. Element.SendKeys(OpenQA.Selenium.Keys.ArrowUp);

  2. char c = '\uE013'; // ASCII code ArrowUp

    Element.SendKeys(Convert.ToString(c));

回答by Tester

Same was happening to my code too. Like in my registration from, 1. I had an Address fields that picks up the entered address from google search and then fills the fields accordingly such as: Sub-urb, city , post code etc. 2. There was a button to attach a file (like browse from desktop and select any image or document to attach) I got error "'Keys' is an ambiguous reference between OpenQA.Selenium.Keysand 'System.Windows.Forms.Keys' (CS0104)Then I realized that it means there are two different Keys types in two different namespaces. Coz for address selection, my code was :

我的代码也发生了同样的事情。就像在我的注册中一样,1. 我有一个地址字段,可以从谷歌搜索中获取输入的地址,然后相应地填写字段,例如:子城市、城市、邮政编码等。 2. 有一个按钮可以附加文件(例如从桌面浏览并选择要附加的任何图像或文档)我收到错误“'Keys'是OpenQA.Selenium.Keys和之间的不明确引用'System.Windows.Forms.Keys' (CS0104)然后我意识到这意味着在两个不同的命名空间中有两种不同的Keys类型。因为地址选择,我的代码是:

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);

and for Attach file the code was:

对于附加文件,代码是:

//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}"); 

Namespaces added were:

添加的命名空间是:

    using OpenQA.Selenium; using System; using System.Threading; using System.Windows.Forms;

Because of - Keys type was not recognising from where it actually belong, so I had to change the code of address fields and use OpenQA.Selenium.keys.ArrowDown as below:

由于 - 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);

This worked for me, hope the same for you too

这对我有用,希望你也一样

回答by angrybambr

I would suggest to do next:

我建议下一步:

    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();

回答by Hemlata Gehlot

Try this

尝试这个

IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("http:www.google.com");
IWebElement MyElement = driver.FindElement(By.Name("q"));
MyElement.SendKeys(Keys.ArrowUp); MyElement.SendKeys(Keys.ArrowDown);

IWebDriver 驱动程序 = new ChromeDriver();
driver.Navigate().GoToUrl("http:www.google.com");
IWebElement MyElement = driver.FindElement(By.Name("q"));
MyElement.SendKeys(Keys.ArrowUp); MyElement.SendKeys(Keys.ArrowDown);