2016-04-08 38 views
0

我在演示網站上創建練習測試,但是,我從下拉列表中選擇一個值時出現問題,但無法找到元素,但它是正確的ID,我已經通過ID和CSS選擇器,沒有運氣嘗試以及:(我將發佈HTML和下面硒代碼:Select Drop Down - Selenium Webdriver

HTML

<select id="dropdown_7" name="dropdown_7" class=" piereg_validate[required]"><option value="Afghanistan">Afghanistan</option> 

的Ruby代碼:

drop_list = @@wait.until { 
      drop = @@driver.find_element :id => '#dropdown_7' 
      drop if drop.displayed? 
      drop.click 
     } 


     options=drop_list.find_element :id => '#dropdown_7' 

     options.each do |i| 
      if i.text == 'American Samoa' 
      i.click 
      break 
      end 

回答

1

的問題是,您指定的ID爲 「#dropdown_7」。雖然這是一個與ID爲「dropdown_7」的元素相匹配的CSS選擇器,但它不會與id屬性匹配。

應該僅僅是:

drop = @@driver.find_element :id => 'dropdown_7' 
0

在下面的java是方法來選擇下拉菜單

Select dropdown = new Select(driver.findElement(By.id("your id"))); 

    dropdown.selectByVisibleText("option text here"); 

dropdown.selectByIndex(1); 

dropdown.selectByValue("value attribute of option"); 

所以沒必要在使用的webdriver選擇點擊選項。

謝謝你, 穆拉利

0

如何我Ruby實現是這樣的:

Selenium::WebDriver::Support::Select.new(
    @driver.find_element(:how, :what) 
).select_by(:how, :what) 
0

使用JavaScript執行人單擊列表中的選項。

public void javascriptclick(String element) 
{ 
    WebElement webElement=driver.findElement(By.xpath(element)); 
    JavascriptExecutor js = (JavascriptExecutor) driver; 

    js.executeScript("arguments[0].click();",webElement); 
    System.out.println("javascriptclick"+" "+ element); 
}