2017-03-15 124 views
0

我想在下拉字段中選擇項目。該頁面使用Materialise CSS,我使用Selenium webdriver C#。使用Selenium選擇實現CSS的下拉項目C#

我最後的嘗試是:

IWebElement tipoLoja = driver.FindElement(By.XPath("//select[@id='tipo_empresa']")); 
SelectElement selectTipoLoja = new SelectElement(tipoLoja); 
selectTipoLoja.SelectByValue("0"); 

但結果是:

OpenQA.Selenium.NoSuchElementException:找不到元素://選擇[@ ID = 'tipo_empresa']

HTML代碼:

<br> 
<div class="input-field col s12 m4 l4 "> 
<div class="select-wrapper">  
    <span class="caret">▼</span> 
    <input class="select-dropdown" readonly="true" data-activates="select-options-fe6e0db4-d829-b47a-86e7-0a3d5b55e74e" value="Selecione" type="text"> 
    <ul id="select-options-fe6e0db4-d829-b47a-86e7-0a3d5b55e74e" class="dropdown-content select-dropdown "> 
     <li class="disabled "></li> 
     <li class=""></li> 
     <li class=""></li> 
     <li class=""></li> 
    </ul> 
    <select data-val="true" data-val-required="Informe o Tipo da Loja." id="tipo_empresa" name="TipoLoja"> 
     <option value="" disabled="disabled" selected="selected">Selecione</option> 
     <option value="0">Matriz</option> 
     <option value="1">Filial</option> 
     <option value="2">Centro de Distribuição</option> 
    </select> 
    </div> 
    <label for="tipo_empresa">Tipo da Loja*</label> 
</div> 
+1

歡迎使用stackoverflow!由於鏈接過時,請將'html'放在問題本身中,像我這樣的人不能訪問外部網站。否則,如果在執行該行之前等待幾秒鐘,調試時是否工作? 'driver.FindElement(By.XPath( 「//選擇[@ ID = 'tipo_empresa']」));'? – mrfreester

+2

從HTML中,它看起來像'SELECT'可能是隱藏的,而它上面的'UL'就是下拉菜單。嘗試單擊「UL」,然後用所需的文本單擊「LI」。另外,當你所做的只是與一個ID進行交互時,你爲什麼要使用XPath?只需使用By.Id()'。 – JeffC

+0

對不起@mrfreester我已經把問題的代碼。我還沒有嘗試過。我會嘗試。 – dsimoes

回答

0

我有相同的代碼,我試圖找到由xpath與firefug Firefox中的元素 ,它的工作。 帕例如:

WebElement inputDrop = driver.findElement(By.xpath("/html/body/main/div[2]/div[1]/form/div/div[2]/div[3]/div/div[2]/div/input")); 
inputDrop.click(); 

的Thread.sleep(10000);

  WebElement type = driver.findElement(By.xpath("/html/body/main/div[2]/div[1]/form/div/div[2]/div[3]/div/div[2]/div/ul/li[3]/span")); 
      type.click(); 
相關問題