2016-11-09 27 views
1

私はPythonには比較的新しい(よく、相対的に)。ドロップダウンメニューからオプションを選択する必要があります。私はほとんどすべてのソリューションを試しました。しかし、何も動作していないようです。 これは私が対話していますページです:http://www.europarl.europa.eu/plenary/en/debates-video.html?action=1&tabActif=tabResult#sidesForm そして、これは私に問題を与えているページのソースの一部です:ドロップダウンメニューからオプションを選択できませんPython Selenium

<select id="criteriaSidesLeg" name="leg" style="display:none;" aria-disabled="false"> 

        <option title="2014 - 2019" value="8">2014 - 2019</option> 

        <option title="2009 - 2014" value="7" selected="selected">2009 - 2014</option> 

        <option title="2004 - 2009" value="6">2004 - 2009</option> 

        <option title="1999 - 2004" value="5">1999 - 2004</option> 

</select> 

私が今までにしようとしたことは次のとおりです。

import time 
from selenium import webdriver 
from selenium.webdriver.common.keys import Keys 
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 
# pressing the botton year menu making the element visible 
elem_year_arrow=driver.find_element_by_id("criteriaSidesLeg-button") 
elem_year_arrow.click() 
year= driver.find_element_by_id('criteriaSidesLeg') 
for option in year.find_elements_by_tag_name('option'): 
    if option.text=='2009 - 2014': 
     option.click() 
     break 

私はこの他のソリューション

も試してみました

selenium.common.exceptions.ElementNotVisibleException: Message: element not visible: Element is not currently visible and may not be manipulated 
    (Session info: chrome=54.0.2840.87) 
    (Driver info: chromedriver=2.25.426935 (820a95b0b81d33e42712f9198c215f703412e1a1),platform=Mac OS X 10.10.5 x86_64) 

:これは私に、このエラーが発生します0

wait = WebDriverWait(driver, 10) 
element = wait.until(EC.visibility_of_element_located((By.ID,"criteriaSidesLeg"))) 
select = Select(element) 
select.select_by_value('7') 

これは私にエラーを与えていないが、このTimeOutexception

raise TimeoutException(message, screen, stacktrace) 
selenium.common.exceptions.TimeoutException: Message: 

は、だから私はまた、このコマンドラインを実行しようとした:

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, "01") 

しかし、再び、続行するか、前述のタイムアウト例外 を取得します? 質問が十分に明確であり、事前に感謝したいと思います!

答えて

1

誤った要素を処理しようとしています。このコードを試してみて、私はすべての問題の場合に知らせ:XPathを使用せずに

driver.find_element_by_xpath('//a[@id="criteriaSidesLeg-button"]').click() 
driver.find_element_by_xpath('//a[text()="2009 - 2014"]').click() 

同じ:

driver.find_element_by_id('criteriaSidesLeg-button').click() 
driver.find_element_by_link_text('2009 - 2014').click() 
+0

これは完全に機能し、私の問題を解決しました!あなたの一種! – Bene

0

あなたが選択ボックスがから管理されていることにリンクしたサイトの問題JSのみで、非選択のドロップダウンに置き換えられました。これは、OS定義の選択動作では常に利用可能ではないカスタムUXを適用する場合によく使用されます。

Seleniumに選択肢からdisplay:noneスタイルを取り除くJavascriptを実行させてから、オプションを選択することができます。

driver.execute_script("document.getElementById('criteriaSidesLeg').setAttribute('style','')"); 

または、擬似選択要素の必要なクリックをシミュレートして、サイトJSに選択肢を更新させます。

関連する問題