11

我正在尝试使用 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);
4

6 回答 6

24

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

您需要通过编写明确地限定类型OpenQA.Selenium.Keys

于 2012-06-03T03:37:33.753 回答
5

我可以为您提供两种实现方式,但第一种仅在本地有效:

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

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

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

于 2013-09-03T08:14:57.670 回答
2

我的代码也发生了同样的事情。就像在我的注册中一样,1. 我有一个地址字段,它从谷歌搜索中获取输入的地址,然后相应地填写字段,例如:Sub-urb、city、邮政编码等。2.有一个按钮可以附加一个文件(例如从桌面浏览并选择要附加的任何图像或文档)我收到错误“'Keys' is an ambiguous reference between OpenQA.Selenium.Keysand'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);

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

于 2017-11-22T00:13:54.807 回答
1

尝试这个

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

于 2019-08-20T10:23:40.893 回答
0

我建议下一步做:

    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();
于 2018-09-03T14:49:51.190 回答
-1

Actions 类是 Selenium 提供的用于处理键盘和鼠标事件的能力。在 Selenium WebDriver 中,处理这些事件包括诸如拖放、使用控制键单击多个元素等操作。

    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(); 
于 2021-04-21T17:54:19.493 回答