-1

Here's my code:

import threading
import Queue
import socket
import pickle
import base64
import time

def enter_mashov():
    from selenium import webdriver
    from selenium.common.exceptions import TimeoutException
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC 

    # Create a new instance of the Firefox driver
    start = time.time()
    driver = webdriver.Firefox()

    driver.get('https://safe.mashov.info/students/login.aspx')

    # find the elements
    IDChanger = driver.find_element_by_id('TextBoxId')
    PassChanger = driver.find_element_by_id('TextBoxPass')

    IDChanger.send_keys('someid')
    PassChanger.send_keys('somepass')

enter_mashov()

I'd like to do the same as I did with the ID Changer and the Password Changer, but I the problem is, it's a drop-down list, and it's options doesn't own IDs or names, but a value. How to change an object's value, then?
Let's say, change it's value and by that, PICKING an option from the options of the drop-down list?

3 Answers3

0

If you want to interact with a <select> element use the Select() class.

select = Select(driver.find_element_by_id("select_id"))
select.select_by_visible_text("The thing you want to select")
Mark Rowlands
  • 5,357
  • 2
  • 29
  • 41
0

You can iterate through the elements by tag name and select an option that way, or you could use their xpath, which does not require the elements to have an id:

select = driver.find_element_by_tag_name("select")
allOptions = select.find_elements_by_tag_name("option")
for option in allOptions:
    print "Value is: " + option.get_attribute("value")
    option.click()

There's a very similar question about how to do it in Java.

xpath method in Python:

from selenium.webdriver.common.by import By
inputs = driver.find_elements(By.XPATH, "//input")
Community
  • 1
  • 1
Alex W
  • 37,233
  • 13
  • 109
  • 109
0

The dropdown list is most likely a select element.

Inside the select element will be a group of option elements.

<select...>
    <option value="valueForFirstOption"...>Visible text for first option</option>
    <option value="valueForSecondOption"...>Visible text for second option</option>
</select>

Use the web developer tools in your browser to see the html code for your dropdown list and check this is the case.

To set its value, just do what a user would do:

  1. Click on the select element
  2. Click on the option element you want to pick.

There are multiple ways to locate the option element. If you want to identify it by the visible text, use @MarkRowlands answer. If you want to locate it by its value, you can use a css selector such as option[value='valueToPick'].

John O.
  • 708
  • 5
  • 9