2015-09-03 10 views
6

今すぐエラーメッセージが表示される前に、自分のスクリプトがページに移動し、ドロップダウンリスト "Vijesti"から2番目のオブジェクトを開きます。Selenium(Python) - SELECT

StaleElementReferenceException: Message: Element not found in the cache - perhaps the page has changed since it was looked up

セレンサイトから:

は、これは誤りである

Thrown when a reference to an element is now “stale”. Stale means the element no longer appears on the DOM of the page. Possible causes of StaleElementReferenceException include, but not limited to:

  • You are no longer on the same page, or the page may have refreshed since the element was located.
  • The element may have been removed and re-added to the screen, since it was located. Such as an element being relocated. This can happen typically with a javascript framework when values are updated and the node is rebuilt.
  • Element may have been inside an iframe or another context which was refreshed.

私は、各オブジェクトを選択し、それを開くようにしたいどのような。

これはURLからSELECT一部です:

<select id="kategorija" name="kategorija"> 
<option value="0">Kategorija</option> 
<option value="12">Vijesti</option> 
<option value="8">Biznis</option> 
<option value="5">Sport</option> 
<option value="2">Magazin</option> 
<option value="7">Lifestyle</option> 
<option value="3">Scitech</option> 
<option value="6">Auto</option> 
</select> 

コード:

from selenium import webdriver 
from selenium.webdriver.common.keys import Keys 
from selenium.webdriver.support.ui import Select 
import time 

driver = webdriver.Firefox() 

driver.get("http://www.klix.ba/") 

assert "Klix" in driver.title 

elem = driver.find_element_by_name("q") 

elem.send_keys("test") 

elem.send_keys(Keys.RETURN) 


select = Select(driver.find_element_by_name('kategorija')) 

all_options = [o.get_attribute('value') for o in select.options] 

for x in all_options: 
    select.select_by_value(x) 
    time.sleep(3) 

これは私が私のtestingsを行うurlです。

答えて

7

ドロップダウンから項目を選択すると、ページが更新されます。

あなたが選択した各オプションにselect要素を「REFind関数」する必要があります。

select = Select(driver.find_element_by_name('kategorija')) 

for index in range(len(select.options)): 
    select = Select(driver.find_element_by_name('kategorija')) 
    select.select_by_index(index) 

    # grab the results 
+0

私は同じ、感謝 –

+0

おかげで多くのことを思っていた、それは – Cudoviste

+1

@Cudoviste良いを働きました!参照してください:http://stackoverflow.com/help/someone-answers。 – alecxe

関連する問題