Python selenium drop down menu click

2024/11/8 10:58:19

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) (Driverinfo: chromedriver=2.22.397929(fb72fb249a903a0b1041ea71eb4c8b3fa0d9be5a),platform=Mac OS X 10.11.5x86_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

Answer

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

https://en.xdnf.cn/q/120627.html

Related Q&A

TypeError: Argument must be rect style object - Pygame (Python [duplicate]

This question already has answers here:Closed 11 years ago.Possible Duplicate:Pygame (Python) - TypeError: Argument must be rect style object I am trying to make a brick breaker game in Pygame (with P…

How to compare dates in python and find the greater one

I want to compare 2 date and predict a label true if date 1 greater than date 2 and predict false date 1 less than date 2. I have trained the model but model is predicting wrong for near by dates that …

Python: is isalnum() the same as isalpha with isdigit?

Is there a way to concretely verify this? I tried to solve a coding question but it seems one of the test cases (not revealed to me) takes this as wrong. In what kinds of cases does this fail to be tr…

Python code works fine first time, but fails second time

The first time I run this block of code from Notebook it works fine:#Which letters and how many letters = ["a","b","c"] noOfLetters = len(letters)#Looking for all permutat…

How do I subtract a value in the dictionary? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

CSV IO python: converting a csv file into a list of lists

How to convert a csv file into a list of lists where each line is a list of entries with in a bigger list?Im having trouble with this because some of my entries have a comma in thema file like: 1,2,3…

Sheet of paper in millimeters [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 9 years ago.Improve…

Kivy App build with Buildozer. APK crash

I am using Oracle VirtualBox running Ubuntu 16. I have been able to build apk files for a while until my latest build. My program will run and keep its functionality when run with python 2.7 on the sam…

Python3 - convert csv to json using pandas

Ive got a .csv files with 5 columns but I only need the json file to contain 3 of these how would i go about doing it?csv file:Ncode Ocode name a b c 1 1.1 1x 1a 1b 1…

Python List of Dictionaries by Loops

I have 2 python list of dictionaries:[{index:1,color:red},{index:2,color:blue},{index:3,color:green} ]and[{device:1,name:x},{device:2,name:y},{device:3,name:z} ]How can I append each dictionary from th…