robin Mistr robin Mistr - 3 months ago 13
Python Question

Python selenium drop down menu click

i want to select option from a drop down menu, for this i use that :


To select option month 4 but when i do that pyhton return error message :

selenium.common.exceptions.ElementNotVisibleException: Message:
element not visible (Session info: chrome=51.0.2704.103) (Driver
info: chromedriver=2.22.397929
(fb72fb249a903a0b1041ea71eb4c8b3fa0d9be5a),platform=Mac OS X 10.11.5

That is the HTML code:

<div class="form-row exp-date clearfix fancyform">
<div class="formfield expired-label monthcaption">
<label>Date d'expiration <span>*</span></label>
<div class="formfield month">
<div class="value value-select">
<select class="selectbox required" id="dwfrm_adyenencrypted_expiryMonth" data-missing-error="Veuillez sélectionner le mois d'expiration" data-parse-error="Ce contenu est invalide" data-range-error="Ce contenu est trop long ou trop court" data-value-error="Cette date d'expiration est invalide" pattern="^(:?0[1-9]|1[0-2])$" required="required" >
<option class="selectoption" label="Mois" value="">Mois</option>
<option class="selectoption" label="01" value="01">01</option>
<option class="selectoption" label="02" value="02">02</option>
<option class="selectoption" label="03" value="03">03</option>
<option class="selectoption" label="04" value="04">04</option>
<option class="selectoption" label="05" value="05">05</option>
<option class="selectoption" label="06" value="06">06</option>
<option class="selectoption" label="07" value="07">07</option>
<option class="selectoption" label="08" value="08">08</option>
<option class="selectoption" label="09" value="09">09</option>
<option class="selectoption" label="10" value="10">10</option>
<option class="selectoption" label="11" value="11">11</option>
<option class="selectoption" label="12" value="12">12</option>

What is wrong ? I know selenium cant find the element but i dont know why , xpath wrong ? i need to use other method to find element ? thanks for anwsers


You should use Select() to select an option from drop down as below :-

from import Select
from import WebDriverWait
from import expected_conditions as EC
from import By

 wait = WebDriverWait(driver, 10)

 element = wait.until(EC.visibility_of_element_located((By.ID, "dwfrm_adyenencrypted_expiryMonth")))

 select = Select(element)

Edited :- If unfortunately above does not work you can also try using .execute_script() as below :-

wait = WebDriverWait(driver, 10)

element = wait.until(EC.presence_of_element_located((By.ID, "dwfrm_adyenencrypted_expiryMonth")))

 driver.execute_script("var select = arguments[0]; for(var i = 0; i < select.options.length; i++){ if(select.options[i].value == arguments[1]){ select.options[i].selected = true; } }", element, "04")

Hope it will work...:)