How to print list of months present in dropdown list using Selenium Python with Webdriver

前端 未结 4 1762
梦谈多话
梦谈多话 2021-01-22 18:51

dropdown details

 
                        
    
提交评论

  • 2021-01-22 19:12

    This will help:

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    
    driver = webdriver.Chrome()
    
    url='' //of webpage
    
    driver.maximize_window()
    
    driver.get(url)
    
    listofelements=driver.find_elements(By.XPATH,'//*[@name="fromMonth"]/option') //to take all elements matching xpath
    
    for i in range(len(listofelements)):
        print(listofelements[i].text) //print all elements of list
    
    0 讨论(0)
  • 2021-01-22 19:21
    dropdown_data = driver.findElement(By.xpath("Xpath of the dropdown")) 
    

    # which selects the dropdown

    for i in range len(dropdown_data):
        print(dropdown_data[i].text)
    
    0 讨论(0)
  • 2021-01-22 19:25

    I think its "November" instead of "Noenter code herevember"

    In JAVA you can use like this, You can apply the same logic in Python

    //locate select drop down
    WebElement monthsElement = driver.findElement(By.name("fromMonth"));
    
    // use select class
    Select monthsDrop = new Select(monthsElement);
    
    //store the list all months in list using getOptions()
    List<WebElement> allmonths = monthsDrop.getOptions();
    
    //traverse and print all elements
    for (WebElement tempmonth : allmonths) {
        System.out.println(tempmonth.getText());
    }
    
    0 讨论(0)
  • 提交回复
    热议问题