12

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

'Keys' is an ambiguous reference between 'OpenQA.Selenium.Keys' and '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);
Liam
30k28 gold badges142 silver badges204 bronze badges
asked Jun 3, 2012 at 3:35

6 Answers 6

25

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

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

answered Jun 3, 2012 at 3:37

Comments

7

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

answered Sep 3, 2013 at 8:14

Comments

2

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.Keys and '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 :

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:

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

answered Nov 22, 2017 at 0:13

Comments

1

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

answered Aug 20, 2019 at 10:23

Comments

0

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();
answered Sep 3, 2018 at 14:49

Comments

-1

Actions class is an ability provided by Selenium for handling keyboard and mouse events. In Selenium WebDriver, handling these events includes operations such as drag and drop, clicking on multiple elements with the control key, among others.

 IWebDriver driver = new ChromeDriver();
 Actions action = new Actions(driver);
 action.SendKeys(Keys.UpArrow);
 action.Build().Perform(); // build and perform is used to complete that particular action. 
 action = new Actions(driver); // reinitialize 
 action.SendKeys(Keys.DownArrow);
 action.Build().Perform(); 
answered Apr 21, 2021 at 17:54

3 Comments

While this code block may answer the question, it would be best if you could provide a little explanation for why it does so.
Actions class is an ability provided by Selenium for handling keyboard and mouse events. In Selenium WebDriver, handling these events includes operations such as drag and drop, clicking on multiple elements with the control key, among others.
build and perform is used to complete that particular action.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.