2016-04-08 39 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); 
}