Selenium c#:无法单击按钮
我在运行以下代码以单击按钮时遇到问题 当我以调试和单步运行时,我可以找到按钮并单击它,而不会出现任何问题。但在实际运行期间,它无法单击按钮 有什么建议吗Selenium c#:无法单击按钮,c#,selenium,selenium-webdriver,C#,Selenium,Selenium Webdriver,我在运行以下代码以单击按钮时遇到问题 当我以调试和单步运行时,我可以找到按钮并单击它,而不会出现任何问题。但在实际运行期间,它无法单击按钮 有什么建议吗 new SelectElement(Driver.FindElement(By.Name("searchType"))).SelectByText("Location"); new SelectElement(Driver.FindElement(By.Id("Location"))).SelectByText("Brentwood");
new SelectElement(Driver.FindElement(By.Name("searchType"))).SelectByText("Location");
new SelectElement(Driver.FindElement(By.Id("Location"))).SelectByText("Brentwood");
Driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5));
var button = Driver.FindElement(By.ClassName("btn-go"));
button.Click();
在以下代码中,将您的时间从5分钟增加到40分钟左右:-
Driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(40));
您也可以对该元素使用WebDriverWait。请参阅以下链接以了解相同的信息:-
希望它能帮助您:)尝试使用显式等待。在本例中,它将等待最多60秒,然后抛出
超时异常。但如果在60秒之前找到元素,它将返回相同的值
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(60));
new SelectElement(driver.FindElement(By.Name("searchType"))).SelectByText("Location");
new SelectElement(driver.FindElement(By.Id("Location"))).SelectByText("Brentwood");
IWebElement button = wait.Until<IWebElement>((d) => { return driver.FindElement(By.ClassName("btn-go")); });
button.Click();
WebDriverWait wait=new-WebDriverWait(驱动程序,TimeSpan.FromSeconds(60));
新的SelectElement(driver.FindElement(By.Name(“searchType”)).SelectByText(“Location”);
新的SelectElement(driver.FindElement(By.Id(“Location”)).SelectByText(“Brentwood”);
IWebElement button=wait.Until((d)=>{return driver.FindElement(By.ClassName(“btn go”);});
按钮。单击();
为什么要以编程方式单击按钮?难道你不能将代码从按钮事件处理程序移到一个方法中,然后调用它吗?@Alex问题是关于使用selenium实现自动化的。@Alex。我试着把按钮放到一个单独的方法中,但它仍然不起作用。
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(60));
new SelectElement(driver.FindElement(By.Name("searchType"))).SelectByText("Location");
new SelectElement(driver.FindElement(By.Id("Location"))).SelectByText("Brentwood");
IWebElement button = wait.Until<IWebElement>((d) => { return driver.FindElement(By.ClassName("btn-go")); });
button.Click();