2016-03-04 29 views
0

我有一個網頁下拉選擇一個國家,已使用jQuery選擇插件呈現。下面的HTML的提取物,CssSelector Webdriver FindElement點擊不工作

<div> 
<label for="phMainContent_EmployeeAdd1_ddlCountry" id="phMainContent_EmployeeAdd1_lblCountry" class="short required">Country*</label>: 
    <div id="phMainContent_EmployeeAdd1_ddlCountry_chzn" class="chzn-container undefined chzn-container-single" style="width: 199.44444px;"> 
     <a href="#x" class="chzn-single"><span>Please select ...</span><div><b></b></div></a> 
     <div class="chzn-drop" style="left: -9000px; width: 197.222px; top: 28px;"> 
      <div class="chzn-search"><input type="text" style="width: 162px;"></div> 
      <ul class="chzn-results"> 
      <li id="phMainContent_EmployeeAdd1_ddlCountry_chzn_o_0" class="active-result result-selected">Please select ...</li> 
      <li id="phMainContent_EmployeeAdd1_ddlCountry_chzn_o_1" class="active-result">United Kingdom</li> 
      <li id="phMainContent_EmployeeAdd1_ddlCountry_chzn_o_2" class="active-result">Afghanistan</li> 
....... 

如果我使用硒IDE錄製的動作,從下面的腳本被記錄在列表中選擇「聯合王國」。運行代碼片段以查看其中的命令。

<table border="1"> 
 
    <tr> 
 
    <td>Command</td> 
 
    <td>Target</td> 
 
    </tr> 
 
    <tr> 
 
    <td>click</td> 
 
    <td>css=a.chzn-single > span</td> 
 
    </tr> 
 
    <tr> 
 
    <td>click</td> 
 
    <td>id=phMainContent_EmployeeAdd1_ddlCountry_chzn_o_1</td> 
 
    </tr> 
 
</table>

我可以在IDE中重複運行此腳本和英國從下拉每次選擇。但是,如果我出口的C#/ NUnit的/ webdriver的代碼下面

driver.FindElement(By.CssSelector("a.chzn-single > span")).Click(); 
driver.FindElement(By.Id("phMainContent_EmployeeAdd1_ddlCountry_chzn_o_1")).Click(); 

並執行它,它失敗與硒元素不可見例外一號聲明。

有關如何解決此問題的任何建議?

+0

這可能是一個時間問題之前,爲了確保下拉是可見的。嘗試插入一個Thread.sleep(2000);聲明之間。請注意,該聲明採用Java語言。 – rs79

回答

0

你可以嘗試XPath和選擇像//span[contains(.,'Please Select')]

0

使用明確的等待點擊

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10)); 
IWebElement dropdown = wait.Until(ExpectedConditions.ElementIsVisible(By.CssSelector("a.chzn-single > span"))); 
dropdown.Click(); 
driver.FindElement(By.Id("phMainContent_EmployeeAdd1_ddlCountry_chzn_o_1")).Click(); 
+0

不幸的是仍然沒有工作,我不認爲這是一個時間/可見問題。如果我查看** dropdown **變量的屬性,我會看到以下內容。它被識別爲((OpenQA.Selenium.Remote.RemoteWebElement)(下拉))。標記名:span,文本:「請選擇...」 - 這表明找到了正確的元素。但dropdown.Click()仍然失敗。就它的工作原理而言,使用jQuery Chosen插件會成爲一個問題嗎? – Cooldudescrib

+0

我剛剛重新檢查了您提供的代碼,並意識到我犯了一個錯誤。現在糾正了所有作品。非常感謝。 – Cooldudescrib