[英]Python selenium drop down menu click
我想從下拉菜單中選擇選項,為此我使用:
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()
要選擇第 4 個月的選項,但是當我執行該 pyhton 返回錯誤消息時:
selenium.common.exceptions.ElementNotVisibleException:消息:元素不可見(會話信息:chrome=51.0.2704.103)(驅動程序信息:chromedriver=2.22.397929(fb72fb249a903a0b1041ea71eb4c8b3fa).61b4c8b3fa0d9.610d9x1000
那是 HTML 代碼:
</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>
怎么了 ? 我知道硒找不到元素,但我不知道為什么,xpath 錯誤? 我需要使用其他方法來查找元素嗎? 感謝您的回答
您應該使用Select()
從下拉列表中選擇一個選項,如下所示:-
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")
編輯:- 如果不幸以上不起作用,您也可以嘗試使用.execute_script()
如下:-
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")
希望它會起作用...:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.