Selenium c#:无法点击按钮

时间:2016-03-10 08:25:12

标签: c# selenium selenium-webdriver

我在运行以下代码时遇到问题,无法点击按钮。

当我作为调试运行并逐步执行时,我可以找到按钮并单击它而没有任何问题。但在实际运行期间,无法单击按钮。

有什么建议吗?

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

3 个答案:

答案 0 :(得分:0)

在以下代码中将时间从5增加到约40: -

Driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(40));

您也可以将WebDriverWait用于该元素。请参阅以下链接: -

http://watirmelon.com/2014/01/29/waiting-in-c-webdriver/

希望它会对你有所帮助:)。

答案 1 :(得分:0)

private WebDriverWait wait;
wait = new WebDriverWait(driver, TimeSpan.FromSeconds(15));

new SelectElement(Driver.FindElement(By.Name("searchType"))).SelectByText("Location");
new SelectElement(Driver.FindElement(By.Id("Location"))).SelectByText("Brentwood");


wait.Until(driver1 => (driver.FindElement(By.ClassName("btn-go"))));
Driver.FindElement(By.ClassName("btn-go")).Click();

答案 2 :(得分:0)

尝试使用显式等待。在此示例中,它将在抛出Timeout Exception之前等待最多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();