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
  • 27,717
  • 28
  • 128
  • 190
automationguy
  • 315
  • 1
  • 5
  • 17

6 Answers6

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.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
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));

Community
  • 1
  • 1
user2742238
  • 71
  • 1
  • 2
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

Tester
  • 61
  • 1
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);

Hemlata Gehlot
  • 341
  • 2
  • 12
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();
angrybambr
  • 108
  • 7
-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(); 
AmitKS
  • 132
  • 4
  • 2
    While this code block may answer the question, it would be best if you could provide a little explanation for why it does so. – nik7 Apr 21 '21 at 20:37
  • 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. – AmitKS Apr 22 '21 at 06:34
  • build and perform is used to complete that particular action. – AmitKS Apr 22 '21 at 07:24