我需要从下拉菜单中选择一个元素。

例如:

<select id="fruits01" class="select" name="fruits">
  <option value="0">Choose your fruits:</option>
  <option value="1">Banana</option>
  <option value="2">Mango</option>
</select>

1)首先我得点击它。我是这样做的:

inputElementFruits = driver.find_element_by_xpath("//select[id='fruits']").click()

2)之后,我必须选择好的元素,让我们说芒果。

我尝试用inputElementFruits.send_keys(…)来做,但它不起作用。


当前回答

下拉菜单WITHOUT <select>

这适用于我每次面对没有<select>标签的下拉菜单

# Finds the dropdown option by its text
driver.find_element_by_xpath("//*[text()='text of the option']")

导入ActionChains模块

from selenium.webdriver.common.action_chains import ActionChains

使用ActionChains点击元素

drp_element = driver.find_element_by_xpath("//*[text()='text of the option']")
action = ActionChains(driver)
action.click(on_element=drp_element).perform()

其他回答

下拉菜单WITHOUT <select>

这适用于我每次面对没有<select>标签的下拉菜单

# Finds the dropdown option by its text
driver.find_element_by_xpath("//*[text()='text of the option']")

导入ActionChains模块

from selenium.webdriver.common.action_chains import ActionChains

使用ActionChains点击元素

drp_element = driver.find_element_by_xpath("//*[text()='text of the option']")
action = ActionChains(driver)
action.click(on_element=drp_element).perform()

我希望这段代码对您有所帮助。

from selenium.webdriver.support.ui import Select

带有id的下拉元素

ddelement= Select(driver.find_element_by_id('id_of_element'))

使用xpath的下拉元素

ddelement= Select(driver.find_element_by_xpath('xpath_of_element'))

下拉元素与CSS选择器

ddelement= Select(driver.find_element_by_css_selector('css_selector_of_element'))

从下拉菜单中选择“Banana”

使用下拉索引

ddelement.select_by_index (1)

使用下拉列表的值

ddelement.select_by_value (' 1 ')

您可以使用匹配下拉菜单中显示的文本。

ddelement.select_by_visible_text(香蕉)

以下方式您可以在下拉框中选择。

select=browser.find_element(by=By.XPATH,value='path to the dropdown')
 select.send_keys("Put value here to select it")
dropdown1 = Select(driver.find_element_by_name("fruits"))
dropdown1.select_by_visible_text('banana')

列表项

公共类ListBoxMultiple {

public static void main(String[] args) throws InterruptedException {
    // TODO Auto-generated method stub
    System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
    WebDriver driver=new ChromeDriver();
    driver.get("file:///C:/Users/Amitabh/Desktop/hotel2.html");//open the website
    driver.manage().window().maximize();


    WebElement hotel = driver.findElement(By.id("maarya"));//get the element

    Select sel=new Select(hotel);//for handling list box
    //isMultiple
    if(sel.isMultiple()){
        System.out.println("it is multi select list");
    }
    else{
        System.out.println("it is single select list");
    }
    //select option
    sel.selectByIndex(1);// you can select by index values
    sel.selectByValue("p");//you can select by value
    sel.selectByVisibleText("Fish");// you can also select by visible text of the options
    //deselect option but this is possible only in case of multiple lists
    Thread.sleep(1000);
    sel.deselectByIndex(1);
    sel.deselectAll();

    //getOptions
    List<WebElement> options = sel.getOptions();

    int count=options.size();
    System.out.println("Total options: "+count);

    for(WebElement opt:options){ // getting text of every elements
        String text=opt.getText();
        System.out.println(text);
        }

    //select all options
    for(int i=0;i<count;i++){
        sel.selectByIndex(i);
        Thread.sleep(1000);
    }

    driver.quit();

}

}