How to select a drop-down menu value with Selenium using Python?

前端 未结 13 1847
南方客
南方客 2020-11-22 06:33

I need to select an element from a drop-down menu.

For example:


                        
    
提交评论

  • 2020-11-22 07:10

    Selenium provides a convenient Select class to work with select -> option constructs:

    from selenium import webdriver
    from selenium.webdriver.support.ui import Select
    
    driver = webdriver.Firefox()
    driver.get('url')
    
    select = Select(driver.find_element_by_id('fruits01'))
    
    # select by visible text
    select.select_by_visible_text('Banana')
    
    # select by value 
    select.select_by_value('1')
    

    See also:

    • What is the correct way to select an using Selenium's Python WebDriver?
    0 讨论(0)
  • 2020-11-22 07:10

    firstly you need to import the Select class and then you need to create the instance of Select class. After creating the instance of Select class, you can perform select methods on that instance to select the options from dropdown list. Here is the code

    from selenium.webdriver.support.select import Select
    
    select_fr = Select(driver.find_element_by_id("fruits01"))
    select_fr.select_by_index(0)
    
    0 讨论(0)
  • 2020-11-22 07:18

    In this way you can select all the options in any dropdowns.

    driver.get("https://www.spectrapremium.com/en/aftermarket/north-america")
    
    print( "The title is  : " + driver.title)
    
    inputs = Select(driver.find_element_by_css_selector('#year'))
    
    input1 = len(inputs.options)
    
    for items in range(input1):
    
        inputs.select_by_index(items)
        time.sleep(1)
    
    0 讨论(0)
  • 2020-11-22 07:18
    1. List item

    public class 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();
    
    }
    

    }

    0 讨论(0)
  • 2020-11-22 07:20

    You don't have to click anything. Use find by xpath or whatever you choose and then use send keys

    For your example: HTML:

    <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>
    

    Python:

    fruit_field = browser.find_element_by_xpath("//input[@name='fruits']")
    fruit_field.send_keys("Mango")
    

    That's it.

    0 讨论(0)
  • 提交回复
    热议问题