0

I'm trying to change the value of a dropdown and facing problem while selecting the value from <select> tag and <option> tag.

This is HTML that i want to change.

<form name="frmSearch" action="" onsubmit="return false;">
  <span class="seljs_title                            ">
    <input id="searchByInput91" name="searchBy91" type="text" readonly="readOnly" class="m-tcol-c" style="width: 110px;">
    <input type="hidden" name="searchBy" value="0" style="display: none;">
  </span>
  <select id="searchBy" name="" class="m-tcol-c" onchange="$('query').focus();" style="width:110px;display:none;">
    <option value="0">one</option>
    <option value="1">two</option>
    <option value="3">three</option>
    <option value="4">four</option>
    <option value="5">five</option>
  </select>
  <input type="text" id="query" name="query" style="ime-mode:active" value="" onkeydown="if (event.keyCode == 13) {nhn.search.goSearch(event);clickcr(this, 'sch.bsearch','','',event)}"
    class="m-tcol-c border-sub text">
  <a href="#" onclick="nhn.search.goSearch(event);clickcr(this, 'sch.bsearch','','',event); return false;">
    <img src="https://cafe.pstatic.net/cafe4/hidden.gif" width="42" height="21" alt="검색" class="btn-search-green">
  </a>
</form> 

I'm using Webdriver(Chrome).This is my code.

driver.find_element_by_name('frmSearch').find_element_by_id('searchByInput91').click()

This code makes me to click the dropdown and open the options. After that, when I use this code :

from selenium.webdriver.support.ui import Select
select = Select(driver.find_element_by_id('searchBy'))
select.select_by_value('1').click()

OR

driver.find_element_by_id("searchBy").send_keys("two")

Error messages always come out.

ElementNotVisibleException: Message: element not visible: Element is not currently visible and may not be manipulated   
  (Session info: chrome=69.0.3497.81)   
  (Driver info: chromedriver=2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e),platform=Windows NT 10.0.17134 x86_64)

what should I do?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
justin_sakong
  • 249
  • 1
  • 6
  • 12
  • Short answer: You cannot. That's the whole point of Selenium. If an user cannot do it (like when element is hidden), Selenium (by design) does not allows you to do that. – Hackerman Sep 10 '18 at 17:08
  • Does form become visible after you click on text input field? – Andersson Sep 10 '18 at 17:08
  • Its look like once you start typing some value in textbox then it start showing the dropdown values as suggestion. Correct me if i'm wrong ? – NarendraR Sep 11 '18 at 05:06
  • I don't know how to reply each comments one by one, sorry. what you mean that click on text input field? In my case, when i click arrow icon beside dropdown, i can see the values that i can chooose. – justin_sakong Sep 11 '18 at 06:08
  • Additionally, when i click dropdown with mouse, the 'class' in the HTML changes to . Does this induces problem? – justin_sakong Sep 11 '18 at 06:17
  • The solution is in here. https://stackoverflow.com/questions/52156388/how-do-i-select-items-from-a-drop-down-box-in-selenium-using-python/52157900#52157900 – justin_sakong Sep 12 '18 at 05:28

1 Answers1

1

As per the HTML you have provided it seems that the <select> tag contains style attribute as display: none;. So you can use the following solution to select an option:

from selenium.webdriver.support.ui import Select
# other lines of code
driver.find_element_by_name('frmSearch').find_element_by_id('searchByInput91').click()
element = driver.find_element_by_xpath("//select[@class='m-tcol-c' and @id='searchBy']")
driver.execute_script("arguments[0].removeAttribute('style')", element)
select = Select(driver.find_element_by_xpath("//select[@class='m-tcol-c' and @id='searchBy']"))
select.select_by_value('1')
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • It's improper way to handle hidden elements. User won't modify HTML DOM manually via dev-tools to make element visible. – Andersson Sep 11 '18 at 06:18
  • Sorry, but can you change [find_element_by_id('searchByInput91')] to [find_element_by_class_name('seljs_title')] for me? I don't know how to code 'xpath'. Thank you very much for help. – justin_sakong Sep 11 '18 at 06:26
  • 1
    Oh! It works!!!!!!! i just change 'searchByInput91' to 'searchByInput952' that changes all the time when i enter. Thanks a lot. – justin_sakong Sep 11 '18 at 06:28
  • 1
    @Andersson Can you tell me why this method is improper way? What you mean "User won't modify HTML DOM manually via dev-tools to make element visible."? – justin_sakong Sep 11 '18 at 06:34
  • @justin_sakong , it means that this doesn't simulate user-like behavior. If your script made for test, with above approach you might miss a bug – Andersson Sep 11 '18 at 06:37
  • 1
    @Andersson, Now I get it. Thank for your support. But how can I solve this problem without remove 'Display:None'?. Your code which you answered only works when 'Display;None' is removed. Could you tell me how to do if you have much times? I'm learned a lot from you today, Thanks. – justin_sakong Sep 11 '18 at 06:53