1

セレンを使用して<select>からオプションを選択する運がない。私はセレニウムのPython:オプションの選択

requests = driver.find_element_by_id("Dropdownlistrequests") 
requests.click() 
for option in requests.find_elements_by_tag_name('option'): 
    if option.text == "Custom": 
     option.click() 
     break 

そして

requests = Select(driver.find_element_by_id("Dropdownlistrequests")) 
requests.select_by_value("6") 

そして

b.find_element_by_xpath("//select[@id='Dropdownlistrequests']/option[text()='Custom']").click() 
を試してみました

<select name="Dropdownlistrequests" onchange="javascript:setTimeout(&#39;__doPostBack(\&#39;Dropdownlistrequests\&#39;,\&#39;\&#39;)&#39;, 0)" id="Dropdownlistrequests" style="height:51px;width:174px;Z-INDEX: 104; LEFT: 280px; POSITION: absolute; TOP: 72px"> 
    <option selected="selected" value="1">Previous Days</option> 
    <option value="2">Previous Month</option> 
    <option value="3">Last 12 Hours</option> 
    <option value="4">Demand Poll</option> 
    <option value="6">Custom</option> 
</select> 

:私は次のようにHTMLコードがあるhttps://sqa.stackexchange.com/questions/1355/what-is-the-correct-way-to-select-an-option-using-seleniums-python-webdriver

を参照しています

適切なオプションを選択する代わりに、ブラウザは何もせずにコードの次のビットに移動します。それはonchangeによって引き起こされるjavascriptを行う何かを持っていますか?私は、Windows 7のエンタープライズを実行しているし、マリオネットとFirefoxの開発者版49.0a2

アップデートでセレンを使用しています:pythonでマリオネットをusoing場合に発生するこれだけセムスいくつかのより多くのコンテキストを提供するために、

。私はマリオネットとないJavaでこれと同じコードを試してみましたが、あなたは以下のように.execute_script()を試してみてくださいあなたのケースでは、作業の非場合は、

+1

あなたがoption.get_attributeを使用してみてくださいすることができます(「のinnerText」 ) "カスタム"を取得する代わりに?あなたが "要求"をクリックした後に、ちょっと待ってから、クリックを見逃すことがないようにしてください。 –

+0

@YuZhang条件を 'if option.get_attribute(" innerText ")==" Custom "に変更すると、現在のループは' option.click() 'ステートメントに到達します:' 'click()'ステートメントは決して決してありません到達した。クリック後に待つことも何もしません。私はうんざりしている。 – pmaurais

+1

試してみてください: 'requests = Select(driver.find_element_by_id(" Dropdownlistrequests ")) requests.select_by_visible_text(" Custom ")' – ishikun

答えて

0

を働いた: -

select = driver.find_element_by_id("Dropdownlistrequests") 

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

編集: - コードの上にのみ選択します選択ボックスから提供されたオプション。あなたにもオプションが選択されたときonchangeイベントをトリガしたい場合は、以下のようにしてみてください: -

select = driver.find_element_by_id("Dropdownlistrequests") 

driver.execute_script("showDropdown = function (element) {var event; event = document.createEvent('MouseEvents'); event.initMouseEvent('mousedown', true, true, window); element.dispatchEvent(event); }; showDropdown(arguments[0]);",select) 
# now you dropdown will be open 


driver.find_element_by_xpath("//select[@id='Dropdownlistrequests']/option[text()='Custom']").click() 
#this will click the option which text is custom and onchange event will be triggered. 

が、それはあなたのために働くことを願っています。.. :)

+0

私は上記のコードを疲れさせ、 '