2017-09-27 18 views
0

私は.click()のWebページのポップアップリストにいくつかの要素を追加しようとしていますが、move_to_elementsを試してもStaleElementReferenceExceptionを取得し続けます。move_to_element(Python)を実行しようとしているときにStaleElementReferenceExceptionが発生しました

コードは、フィード内のクリック可能な要素の数に基づいています。これらの要素をクリックすると、アクセス可能なクリック可能な要素が多いポップアップボックスが表示されます。

私はアップボックスpopupbox_linksはポップアップボックスの座標とのリンクがリストされている次のコード、とポップにアクセス:

for coordinate in popupbox_links: 
    actions = ActionChains(driver) 
    actions.move_to_element(coordinate["Popupbox location"]).perform() 
    time.sleep(3) 
    popupboxpath = coordinate["Popupbox link"] 
    popupboxpath.click() 
    time.sleep(3) 

これが正常に動作します。ポップアップボックスが開いているときしかし、私は次のことを実行する:

seemore = driver.find_element_by_link_text("See More") 
time.sleep(2) 
actions.move_to_element(seemore).perform() 
time.sleep(2) 
seemore.click() 
time.sleep(3) 
findbuttons = driver.find_elements_by_link_text("Button") 
time.sleep(2) 
print(findbutton) 
for button in findbuttons: 
    time.sleep(2) 
    actions.move_to_element(button).perform() 
    time.sleep(2) 
    button.click() 
    time.sleep(randint(1, 5)) 

トラブルが両方と「ボタン」、「詳細を参照してください」にactions.move_to_elementから始まります。 print(findbutton)は、実際には、クリックしたい要素を含む内容のリストを返しますが、Seleniumはこれらに対してmove_to_elementを実行できません。代わりに、StaleElementReferenceExceptionがスローされます。

もっと混乱させるために、スクリプトは時々動作するようです。通常はクラッシュしますが。

これを解決する手掛かりはありますか?事前に大きな感謝。

私はPython 3.6上で最新のSeleniumをChrome WebDriverで実行しています。

答えて

0

StaleElementReferenceException webElementオブジェクトの作成後に何かがページ内で変更されたため、その要素はステージです。あなたの場合は、button.click()のために起こっている可能性があります。

最も単純な解決策は、ループから要素を反復するのではなく、常に新しい要素を作成することです。

次の変更が行われる場合があります。

findbuttons = driver.find_elements_by_link_text("Button") 
time.sleep(2) 
print(findbuttons) 
for i in range(len(findbuttons)): 
    time.sleep(2) 
    elem = driver.find_elements_by_link_text("Button")[i] 
    actions.move_to_element(elem).perform() 
    time.sleep(2) 
    elem.click() 
    time.sleep(randint(1, 5)) 
+0

アドバイスをいただき、ありがとうございます。 seemore = driver.find_element_by_link_text( "もっと見る") ここでは反復する要素のリストではなく、ただ1つの特定の要素です。それに移動しようとするとStaleElementを取得します。 –

+0

あなたは根本的な原因を知る必要があります。あなたがそれを手に入れた理由です。この場合は何でもかまいません。この状態を処理するために「Explicit Wait」もあります。 –