0

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)
Ratmir Asanov
  • 6,237
  • 5
  • 26
  • 40
sai
  • 11
  • 1
  • 1
  • 3
  • 1
    As error states, the element is not visible, and HTML you posted confirmed that `...style="display: none;"` You need to make element visible first. How to do it depends on page you are testing. Basically you need to do what user needs to do to get that drop-down displayed. Or maybe you are not waiting long enough for it to get displayed. – timbre timbre Feb 26 '18 at 17:35
  • As per the [Revision 2](https://stackoverflow.com/revisions/48993743/2) of this question if you are trying to hide the attributes **style="display: none;"** from the ` – undetected Selenium Feb 26 '18 at 18:54
  • I did edit the post, because when I looked at the HTML in "view page source" - it doesn't have style ="display: none;", however when I "inspect" by clicking the drop-down option, it's showing up the style="display: none;". – sai Feb 26 '18 at 19:26
  • @sai, how is going? – Ratmir Asanov Mar 05 '18 at 16:03

3 Answers3

3

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.

Ratmir Asanov
  • 6,237
  • 5
  • 26
  • 40
  • Tried that as well, didn't work and getting the same error – sai Feb 26 '18 at 18:20
  • @sai, share your URL. – Ratmir Asanov Feb 26 '18 at 19:26
  • 1
    Hi Ratmir, I apologize for not getting back to you on time and unfortunately sharing URL won't help, as you will be needing credentials to login into the page and I'm still facing the same issue. However, I tried your solution on the different webpage and was working. – sai Mar 15 '18 at 20:09
1

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?

kevin
  • 1,914
  • 4
  • 25
  • 30
0

Try this code

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

This will select your id