2016-12-06 71 views
0

選択フィールドからSelenium & Pythonを使用してオプションを選択したいとします。SeleniumとPythonを使用してドロップダウンから値を選択する方法

次のようにHTMLは次のとおりです。

<select autocomplete="off" class="style_input_item" name="AccountEnable" id="Enable" value="0" onchange="onPageDataChange()"> 
    <option value="0" selected="selected"><script>T("Disabled")</script>Disabled</option> 
    <option value="1"><script>T("Enabled")</script>Enabled</option> 
</select> 

を、次のように私が試した:

driver.find_element_by_xpath('//*[@id="Enable"]/option[value="1"]').click() 

私はそのエラーを受け取りました:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="Enable"]/option[value="0"]"}

+0

[セレン - パイソン - ドロップダウンメニューのオプション値]の可能性のある重複します(http:// selectoverflow.com/questions/7867537/selenium-python-drop-down-menu-option-value)。受け入れられた答えは最良の方法ではありません。最良の方法は、アレクセの答えです。 http://sqa.stackexchange.com/questions/1355/what-is-the-correct-way-to-select-an-option-using-seleniums-python-webdriverも参照してください。 – JeffC

+0

受け入れられた答えは最善の方法ではありません。最良の方法は、アレクセの答えです。 http://sqa.stackexchange.com/questions/1355/what-is-the-correct-way-to-select-an-option-using-seleniums-python-webdriverも参照してください。 – JeffC

答えて

0

ただ、試してみてください。

mydriver.find_element_by_xpath('//*[@id="Enable"]/option[@value="1"]').click() 

または

mydriver.find_element_by_xpath('//*[@id="Enable"]/option[2]').click() 
0

あなたが選択含まれていることを確認してください:

from selenium.webdriver.support.select import Select 

その後、

select = Select(driver.find_element_by_id('Enable')) 
select.select_by_index(0) 
関連する問題