2012-08-01 99 views
2

我想從下拉菜單中使用C#中的Selenium webdriver選擇一個文本# 它與Chrome瀏覽器完美配合,但與Firefox無法正常工作。任何人都可以幫我解決這個問題。Selenium Webdriver C# - 未選擇Firefox下拉菜單文本

我使用的代碼如下。

public void SelectCountry1(string country) 
{ 
var countryDropDown = Driver.FindElement(By.XPath(xpathidofthecountrydropdown)); 
countryDropDown .Click(); 
//Driver.FindElement(By.XPath(xpathidofthecountrydropdown)).Click; 
var selectElement = new SelectElement(countryDropDown); 
selectElement.SelectByText(country); 
} 

我可以調用這個函數,並且這個函數在沒有任何錯誤消息的情況下成功執行。儘管存在,但我無法選擇預期的關鍵字事件。

目前我有一個解決方法點擊兩次相同的ID,並使代碼工作。評論部分未註釋但我不認爲這是正確的解決方法。讓我知道你的想法。

感謝

+0

嘗試更新硒和Firefox的最新。有時,不匹配會導致這些類型的問題。 – iMatoria 2012-08-01 18:36:30

+0

您正在執行的.Click()可能會干擾選擇。試着評論一下,看看是否有用。 – 2012-08-01 22:23:37

+0

您是否嘗試過使用硒動作來執行點擊操作。 – 2012-08-02 06:14:58

回答

0

是啊,它不適用於Firefox。我不得不使用這個作爲使用jQuery的解決方法。如果您在頁面上沒有jQuery,請隨意使用常規JavaScript修改此代碼。

public static void SetDropdownSelectedOptionByText(IWebDriver driver, string tagId, string newText, int sleepTime) 
{ 
    // not working when selecting client id of certain types of ASP.NET user controls 
    //new SelectElement(driver.FindElement(By.Id(tagId))).SelectByText(newText); 

    IJavaScriptExecutor js = driver as IJavaScriptExecutor; 
    js.ExecuteScript("$('#" + tagId + " option:contains(" + Element.NonNullValue(newText) + ")').attr('selected', 'selected')"); 
    js.ExecuteScript("$('#" + tagId + "').change()"); 
    System.Threading.Thread.Sleep(sleepTime); // wait for dependent dropdown to load its values 
} 
+0

謝謝MacGyver。會嘗試讓你知道。 – Vinee 2012-08-07 11:59:17

0

通常情況下,選擇類處理的選擇沒有你甚至不點擊下拉。它應該可以在FF和Chrome中使用,即使在Select中也有其他問題。儘量不要點擊下拉按鈕。如果Select類沒有按照假想的方式運行,那麼嘗試點擊並導航選項,然後按下回車鍵。

相關問題