-4

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

br.find_element_by_xpath("//*[@id='adyen-encrypted-form']/fieldset/div[3]/div[2]/div/div/div/div/div[2]/div/ul/li[5]/span").click()

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 x86_64)

That is the HTML code:

</div>
   <div class="form-row exp-date clearfix fancyform">
   <div class="formfield expired-label monthcaption">
     <label>Date d'expiration <span>*</span></label>
</div>
<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>
</select>

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

benten
  • 1,995
  • 2
  • 23
  • 38
robin Mistr
  • 45
  • 1
  • 2
  • 5
  • 1
    Welcome to Stack Overflow! What have you tried and what was the result? Please read the help topics on how to ask a good question. You need to research your own issue, find code samples, etc. and write your own code to solve the issue. If you do all that and still can't figure it out, then come back and edit your question and add notes from the research you did, the code you have tried reduced to a [mcve], and what the result was... any error messages, etc. It's also very important to include any relevant HTML and properly format the HTML and code. – JeffC Jul 10 '16 at 17:18

1 Answers1

0

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

from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

 wait = WebDriverWait(driver, 10)


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

 select = Select(element)
 select.select_by_value("04")

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...:)

Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
  • I try it but python return this error message : selenium.common.exceptions.ElementNotVisibleException: Message: element not visible: Element is not currently visible and may not be manipulated (Session info: chrome=51.0.2704.103) (Driver info: chromedriver=2.22.397929 (fb72fb249a903a0b1041ea71eb4c8b3fa0d9be5a),platform=Mac OS X 10.11.5 x86_64) – robin Mistr Jul 10 '16 at 13:02
  • @robin Mistr try using `.execute_script()` and let me know...see edited answer – Saurabh Gaur Jul 10 '16 at 13:45
  • What is BY ? element = wait.until(EC.visibility_of_element_located((By.ID, "dwfrm_adyenencrypted_expiryMonth"))) NameError: name 'By' is not defined – robin Mistr Jul 10 '16 at 14:42
  • Have a look to know about `BY` class...http://selenium-python.readthedocs.io/locating-elements.html – Saurabh Gaur Jul 10 '16 at 15:36
  • With the 2 method its say that : raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message: – robin Mistr Jul 10 '16 at 15:54
  • @robin Mistr then try using `.executeScript()` as second option provided with `EC.presence_of_element_located` insteadof `EC.visibility_of_element_located`...see updated answer – Saurabh Gaur Jul 10 '16 at 16:09
  • @robinMistr could you tell me its actually visible manually on the page or not??? – Saurabh Gaur Jul 10 '16 at 16:11
  • that is the page source : view-source:https://www.adidas.fr/on/demandware.store/Sites-adidas-FR-Site/fr_FR/COSummary-Start – robin Mistr Jul 10 '16 at 16:42
  • Anny idee ? please – robin Mistr Jul 12 '16 at 20:03
  • I beat my head against the wall trying to get this answer to work and finally found it.@SaurabhGaur should add a ```select.onchange();``` after the selection is made if your select includes a javascript onchange method (which mine does). – Chuck Claunch Mar 11 '17 at 01:36