簡體   English   中英

硒c#:無法單擊按鈕

[英]Selenium c#: Unable to click button

我在運行以下代碼來單擊按鈕時遇到問題。

當我以調試方式運行並逐步執行時,我可以找到該按鈕並單擊它而沒有任何問題。 但是在實際運行期間,它無法單擊按鈕。

有什么建議嗎?

 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。 請參考以下鏈接以獲取相同信息:-

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

希望它能對您有所幫助:)

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

嘗試使用“顯式等待”。 在此示例中,它將最多等待60秒,然后拋出Timeout Exception 但是,如果在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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM