【问题标题】:Selenium Webdriver C# Sendkeys (Keys.Arrowdown)Selenium Webdriver C# Sendkeys (Keys.Arrowdown)
【发布时间】:2023-03-14 01:22:01
【问题描述】:

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

【问题讨论】:

    标签: c# selenium webdriver


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

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

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

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

      【讨论】:

        【解决方案3】:

        我的代码也发生了同样的情况。就像我在注册时一样, 1. 我有一个地址字段,它从谷歌搜索中提取输入的地址,然后相应地填写字段,例如:Sub-urb、city、post code 等。 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);
        

        附加文件的代码是:

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

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

        【讨论】:

          【解决方案4】:

          我建议下一步做:

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

          【讨论】:

            【解决方案5】:

            试试这个

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

            【讨论】:

              【解决方案6】:

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

              【讨论】:

              • 虽然这个代码块可能会回答这个问题,但最好能稍微解释一下为什么会这样。
              • Actions 类是 Selenium 提供的用于处理键盘和鼠标事件的能力。在 Selenium WebDriver 中,处理这些事件包括诸如拖放、使用控制键单击多个元素等操作。
              • build and perform 用于完成该特定操作。
              猜你喜欢
              • 2012-10-15
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2021-11-21
              • 2013-01-11
              • 2016-04-21
              • 2013-10-10
              • 1970-01-01
              相关资源
              最近更新 更多