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