这是一个规范的问题,旨在为许多相关问题提供全面的答案。

我了解使用Selenium Webdriver的基础知识;我可以导航到页面,单击按钮,然后在文本框中键入内容。但是现在我想使用下拉框(也称为“选择”框)来做事。

如何执行常见任务,例如遍历下拉菜单中的选项或从下拉菜单中选择选项? br />

#1 楼

使用Select Utility类

使用下拉菜单的最大秘密是,您不想将它们作为WebElement来使用,而是为其创建Select元素。 Select类(java和python文档)包括实用程序方法,可让您执行常见任务。我们将使用以下html:

<select id="mySelectID">
    <option value="Value">Option</option>
    <option value="NotValue">Not Option</option>
</select>


通过选项名称选择

Java:

WebElement mySelectElm = driver.findElement(By.id("mySelectID")); 
Select mySelect= new Select(mySelectElm);
selMySelect.selectByVisibleText("Option");


Python:

mySelect = Select(driver.find_element_by_id("mySelectID"))
mySelect.select_by_visible_text("Option")



var mySelectElm = driver.FindElement(By.Id("mySelectID"));
var mySelect = new SelectElement(mySelectElm);
selectElement.SelectByText("Option");


通过选项值选择

Java:

WebElement mySelectElm = driver.findElement(By.id("mySelectID")); 
Select mySelect= new Select(mySelectElm);
selMySelect.selectByValue("Value");


Python:

mySelect = Select(driver.find_element_by_id("mySelectID"))
mySelect.select_by_value("Value")


C#:

var mySelectElm = driver.FindElement(By.Id("mySelectID"));
var mySelect = new SelectElement(mySelectElm);
selectElement.SelectByValue("Value");


按索引选择

Java:

WebElement mySelectElm = driver.findElement(By.id("mySelectID")); 
Select mySelect= new Select(mySelectElm);
selMySelect.selectByIndex(0);


Python:

mySelect = Select(driver.find_element_by_id("mySelectID"))
mySelect.select_by_index(0)


C#:

var mySelectElm = driver.FindElement(By.Id("mySelectID"));
var mySelect = new SelectElement(mySelectElm);
selectElement.SelectByIndex(0);


获取所选选项

Java:

WebElement mySelectElm = driver.findElement(By.id("mySelectID")); 
Select mySelect= new Select(mySelectElm);
WebElement option = mySelect.getFirstSelectedOption();
System.out.println(option.getText()); //prints "Option"


Python:

mySelect = Select(driver.find_element_by_id("mySelectID"))
option = mySelect.first_selected_option
print option.text  #prints "Option"


C#:

var mySelectElm = driver.FindElement(By.Id("mySelectID"));
var mySelect = new SelectElement(mySelectElm);
var option = mySelect.SelectedOption;
Console.write(option.Text); //prints "Option"


>获取选项列表

Java:

WebElement mySelectElm = driver.findElement(By.id("mySelectID")); 
Select mySelect= new Select(mySelectElm);
List<WebElement> options = mySelect.getOptions();
for (WebElement option : options) {
    System.out.println(option.getText()); //Prints "Option", followed by "Not Option"
}


Python:

mySelect = Select(driver.find_element_by_id("mySelectID"))
print [o.text for o in mySelect.options] #Prints "Option", followed by "Not Option"


C#:

var mySelectElm = driver.FindElement(By.Id("mySelectID"));
var mySelect = new SelectElement(mySelectElm);
var options = mySelect.SelectedOptions;
foreach(var option in options) {
    Console.write(option.Text); //Prints "Option", followed by "Not Option"
}


评论


请注意,在C#中,SelectElement类是Selenium.Support项目的一部分。因此,您必须确保除了Selenium.WebDriver之外,还有其他功能。

–路易斯·佩雷斯(Luis Perez)
2015年10月19日在18:42

我可以对此答案进行多次投票吗?很棒的答案!

– OM Prakash
18年2月8日在9:10

#2 楼

如果没有Select类

我明智地使用选择器就可以完成我在其他答案中列出的所有内容。由于Select类在Ruby中不存在(据我所知),这是在Ruby中使用Select选项的唯一方法。

同样,HTML:

<select id="mySelectID">
    <option value="Value">Option</option>
    <option value="NotValue">Not Option</option>
</select>


通过选项名称选择

Java,方法1:

在本示例中,我们通过复杂的xpath找到了该选项,然后单击它:

WebElement myoption = driver.findElement(By.xpath(
    "//Select[@id='mySelectID']/option[normalize-space(text())='Option']")
);
 myOption.click();


Java,方法2:

在此示例中,我们找到了所有选项,对其进行迭代,然后单击我们想要的一个。如果您有更复杂的标准,这将非常有用。

WebElement mySelectElm = driver.findElement(By.id("mySelectID")); 
Select mySelect= new Select(mySelect);
List<WebElement> options = mySelect.getOptions();
for (WebElement option : options) {
    if (option.getText().equalsIgnoreCase("Option") {
        option.click();
    }
}



mySelect=webdriver.find_element(:id,"mySelectID")
options=mySelect.find_elements(:tag_name=>"option")
options.each do |g|
  if g.text == "Option"
  g.click
  break
  end
end


Ruby,方法3:

在这里,我们很喜欢使用闭包而不是循环来找到正确的选项:

mySelect = webdriver.find_element(:id,"mySelectID")
option = dropdown.find_elements(:tag_name,"option").detect { |option| option.attribute('text').eql? "Option"}
option.click


通过选项值选择

相同的基本思想,但是我们可以使用CSS通过值选择选项,而不必考虑xpath:

Java

WebElement myoption = driver.findElement(By.cssSelector("#mySelectID option[value='Value']"));
myOption.click();



mySelect = webdriver.find_element(:id,"mySelectID")
option = mySelect.find_element(:css, "option[value='Value']")
option.click


通过索引选择

再次使用CSS选择器很容易:

Java

WebElement myoption = driver.findElement(By.cssSelector("#mySelectID option:nth-child(1)"));
myOption.click();


Ruby

mySelect = webdriver.find_element(:id,"mySelectID")
option = mySelect.find_element(:css, "option:nth-child(1)")
option.click


获取选定的选项

CSS选择器万岁!出于遗留原因,“选中”的选择器是“选中的”,如复选框:

Java: >
WebElement myoption = driver.findElement(By.cssSelector("#mySelectID option:checked"));
System.out.println(myoption.getText()); //prints "Option"


获取选项列表

Java:

mySelect = webdriver.find_element(:id,"mySelectID")
option = mySelect.find_element(:css, "option:nth-child(1)")
print option.text


/>
List<WebElement> options = driver.findElements(By.cssSelector("#mySelectID option"));
for (WebElement option : options) {
    System.out.println(option.getText()); //Prints "Option", followed by "Not Option"
}


#3 楼

您可以使用以下方法来处理硒的下降。

 1. driver.selectByVisibleText("Text");
 2. driver.selectByIndex(1);
 3. driver.selectByValue("prog");


有关更多详细信息,请参阅此文章。
它绝对可以帮助您解决查询。

#4 楼

有一个可怕的错误,即在Firefox或phantomjs版本的硒中选择选项未触发。我选择了以下方法,也许会对其他人有所帮助:

from selenium.webdriver.support.ui import Select

def trigger_event(driver, id_, event):
    js = '''
    var event = new Event("%s");
    element = document.getElementById("%s")
    element.dispatchEvent(event);
    return true;
    ''' % (event, id_)

    assert driver.execute_script(js)

select_id = "SELECT_ID"
select = driver.find_element_by_id(select_id)
Select(select).select_by_visible_text("SELECT_TEXT")
trigger_event(driver, select_id, "input")


#5 楼

1)SelectByIndex

Select select = new Select(driver.findElement(By.id("mobile-operator"))); select.selectByValue(index of the record you want to select );

2)SelectByValue

Select select = new Select(driver.findElement(By.id("mobile-operator")));
select.selectByValue(value of the record you want to select );


3)SelectByVisibleText

Select select = new Select(driver.findElement(By.id("mobile-operator"))); /n
select.selectByValue(visible text of the record you want to select );


#6 楼

Ruby-使用capybara gem

http://www.rubydoc.info/github/jnicklas/capybara/Capybara/Node/Actions#select-instance_method

以奇妙而简洁的红宝石方式实现其他实现的优点:

select('Option', from: 'Select Box')
html有<select id='driver-license-month'>...<option value='November'>November</option>...