Click() 方法在使用 C# Selenium IDE 时不起作用

本文关键字:Selenium IDE 不起作用 方法 Click | 更新日期: 2023-09-27 17:56:28

我正在尝试从 c# 执行硒测试。我已经在硒 IDE 上记录了测试,并使用 IDE 工具验证了它是否有效。该测试是一个简单的概念证明。浏览到美国联合航空公司的网站,在其搜索小部件上输入一些航班信息并调用搜索。当我运行项目时,除了单击搜索按钮不会像使用 Selenium IDE 时那样调用单击之外,一切正常。

非工作线,未抛出错误

 driver.FindElement(By.Id("ctl00_ContentInfo_Booking1_btnSearchFlight")).Click();

完整代码

driver = new FirefoxDriver();
baseURL = "http://www.united.com/";
verificationErrors = new StringBuilder();
enter code here
driver.Navigate().GoToUrl(baseURL + "/web/en-US/default.aspx?root=1");
driver.FindElement(By.Id("ctl00_ContentInfo_Booking1_Origin_txtOrigin")).Clear();
driver.FindElement(By.Id("ctl00_ContentInfo_Booking1_Origin_txtOrigin")).SendKeys("fort lauderdale, fl");
driver.FindElement(By.Id("ctl00_ContentInfo_Booking1_Destination_txtDestination")).Clear();
driver.FindElement(By.Id("ctl00_ContentInfo_Booking1_Destination_txtDestination")).SendKeys("New York/Newark, NJ (EWR - Liberty)");
driver.FindElement(By.Id("ctl00_ContentInfo_Booking1_DepDateTime_Depdate_txtDptDate")).Click();
driver.FindElement(By.Id("ctl00_ContentInfo_Booking1_DepDateTime_Depdate_txtDptDate")).Clear();
driver.FindElement(By.Id("ctl00_ContentInfo_Booking1_DepDateTime_Depdate_txtDptDate")).SendKeys("4/7/2014");
driver.FindElement(By.Id("ctl00_ContentInfo_Booking1_RetDateTime_Retdate_txtRetDate")).Clear();
driver.FindElement(By.Id("ctl00_ContentInfo_Booking1_RetDateTime_Retdate_txtRetDate")).SendKeys("4/14/2014");
driver.FindElement(By.Id("ctl00_ContentInfo_Booking1_btnSearchFlight")).Click();

String test = driver.PageSource;

Click() 方法在使用 C# Selenium IDE 时不起作用

问题是页面上有两个ctl00_ContentInfo_Booking1_btnSearchFlight。 第一个是div,第二个是您要单击的按钮。

试试这个:

FindElement(By.CssSelector("input#ctl00_ContentInfo_Booking1_btnSearchFlight")).Click();