sai
sai

Reputation: 11

Selecting value from dropdown using Python + Selenium

Please help me with selecting a value from the dropdown. I was able to login to the website but unable to select a value from the dropdown list after logging in. I had tried few solutions/suggestions found online, but couldn't get that to work. Please see below for the code and error with HTML.

HTML:

<select id="highlightunits" name="highlightunits">
<option selected="" value="px">Pixels</option>
<option value="in">Inches</option>
<option value="mm">Millimeters </option>
<option value="cm">Centimeters</option>
</select> </td>

Code:

 from selenium.webdriver.chrome.webdriver import WebDriver
 from selenium.webdriver.common.by import By
 from selenium.webdriver.support.ui import WebDriverWait
 from selenium.webdriver.support import expected_conditions as EC
 from selenium.webdriver.support.ui import Select
 from selenium.webdriver.common.keys import Keys


 usernameStr = 'username'
 passwordStr = 'password'
 browser = webdriver.Chrome()
 browser.maximize_window()
 browser.get('mywebsite/')
 username = browser.find_element_by_id('username')
 username.send_keys(usernameStr)
 password = WebDriverWait(browser, 
 5).until(EC.presence_of_element_located((By.ID,'password')))
 password.send_keys(passwordStr)
 logInButton = browser.find_element_by_id('login_button')
 logInButton.click()
 file_input1 = browser.find_element_by_id('reference')
 file_input2 = browser.find_element_by_id('candidate')
 file_input1.send_keys("filelocation on my drive")
 file_input2.send_keys("filelocaton on my drive")
 s1= Select(browser.find_element_by_id('highlightunits'))
 print(s1.options)
 for option in s1.options:
   s1.select_by_visible_text('Inches')

Error:

[<selenium.webdriver.remote.webelement.WebElement (session="3903b52d4ab75592fbd965a21424f192", element="0.5058773595053347-4")>, <selenium.webdriver.remote.webelement.WebElement (session="3903b52d4ab75592fbd965a21424f192", element="0.5058773595053347-5")>, <selenium.webdriver.remote.webelement.WebElement (session="3903b52d4ab75592fbd965a21424f192", element="0.5058773595053347-6")>, <selenium.webdriver.remote.webelement.WebElement (session="3903b52d4ab75592fbd965a21424f192", element="0.5058773595053347-7")>]

Traceback (most recent call last):
  File "C:\Users\Desktop\MyProjects\Applications\dropdown.py", line 26, in <module>
    s1.select_by_visible_text('Inches')
  File "C:\Python27\lib\site-packages\selenium\webdriver\support\select.py", line 120, in select_by_visible_text
    self._setSelected(opt)
  File "C:\Python27\lib\site-packages\selenium\webdriver\support\select.py", line 212, in _setSelected
    option.click()
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
    self._execute(Command.CLICK_ELEMENT)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 628, in _execute
    return self._parent.execute(command, params)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 312, in execute
    self.error_handler.check_response(response)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
ElementNotVisibleException: Message: element not visible: Element is not currently visible and may not be manipulated
  (Session info: chrome=63.0.3239.132)
  (Driver info: chromedriver=2.35.528161 (5b82f2d2aae0ca24b877009200ced9065a772e73),platform=Windows NT 6.1.7601 SP1 x86_64)

Upvotes: 0

Views: 15778

Answers (3)

Arham Sanghvi
Arham Sanghvi

Reputation: 11

Try this code

s1 = Select(browser.find_element_by_id('highlightunits'))
s1.select_by_index(1)

This will select your id

Upvotes: 0

kevin
kevin

Reputation: 2014

You can use the following code:

s1 = browser.find_element_by_xpath("//input[@id='highlightunits']") 
s1.send_keys('Inches')

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

Upvotes: 1

Ratmir Asanov
Ratmir Asanov

Reputation: 6459

You need to use the following code:

s1 = Select(browser.find_element_by_id('highlightunits'))
s1.select_by_visible_text('Inches')

PS: Without using a loop.

Upvotes: 3

Related Questions