2017-10-28 98 views
0

所以我想弄清楚一種方法來自動點擊選擇元素選項的選擇,但是當我使用由Selenium網站提供的代碼來瀏覽選項,我得到陳舊的元素異常錯誤。我試着用等待時間來等待元素被加載,但無論我在哪裏等待,都會給我一個錯誤。它確實經歷了第一次選擇並選擇了一個選項,但是第二次它通過每個選項然後單擊,然後給我一個錯誤而不在屏幕上更新它,或者它通過了一半並給出陳舊的元素錯誤。Selenium陳舊的元素引用異常Python

這是我下面的代碼部分:

displayed = browser.find_elements_by_xpath("//select[@name='listing_variation_id']") #check to see if this element is an option 
        if displayed: 
         selections = browser.find_elements_by_xpath("//select[@name='listing_variation_id']") 
         print("\n" + str(len(selections)) + "\n") 

         for options in selections: #select and choose an item choice 
          all_options = options.find_elements_by_tag_name("option") 
          for option in all_options: 
           browser.implicitly_wait(2) 
           print("Value is: %s" % option.get_attribute("innerText")) #debugging 
           option.click() 

這是我試圖導航

''' 
<div id="variations" class="buy-box__variations ui-toolkit " data-buy-box-view-options="{&quot;user_is_listing_owner&quot;:false,&quot;order_already_started&quot;:false,&quot;inline_variation_labels&quot;:false,&quot;listing_mode&quot;:&quot;listing_mode_default&quot;,&quot;show_preview_warning&quot;:false,&quot;quantity_behavior&quot;:&quot;quantity_enabled&quot;,&quot;quantity_related_nudge_is_on&quot;:true,&quot;additional_button_class&quot;:&quot;&quot;,&quot;is_mobile&quot;:false,&quot;channel&quot;:1}"> 
    <div class="buy-box__variation item-variation-option"> 
    <label for="inventory-variation-select-0">How Many?</label> 
    <span> 
     <select id="inventory-variation-select-0" class="variation-select" name="listing_variation_id"> 
      <option value="" selected="">Select an option</option> 
      <option value="44719679623">One Squeaker [$1.75]</option> 
      <option value="44719679633">Two Squeakers [$2.50]</option> 
     </select> 
    </span> 
    <div class="buy-box__variation-error p-xs-1 mt-xs-1 text-smaller bg-red text-white rounded display-none">Please select an option</div> 
</div><div class="buy-box__variation item-variation-option"> 
    <label for="inventory-variation-select-1">Placement</label> 
    <span> 
     <select id="inventory-variation-select-1" class="variation-select" name="listing_variation_id"> 
      <option value="" selected="">Select an option</option> 
      <option value="42697801382">Top of Tail</option> 
      <option value="42697801396">Bottom of Tail</option> 
      <option value="42697801398">For Two- Top&amp;Bottom</option> 
      <option value="42697801406">For Two- Both Bottom</option> 
      <option value="42697801408">For Two- Both Top</option> 
     </select> 
    </span> 
    <div class="buy-box__variation-error p-xs-1 mt-xs-1 text-smaller bg-red text-white rounded display-none">Please select an option</div> 
</div><div class="buy-box__variation item-variation-option"> 
    <label for="inventory-select-quantity">Quantity</label> 
    <span> 
     <select id="inventory-select-quantity" class="small" name=""> 
      <option value="1" selected="">1</option> 
      <option value="2">2</option> 
      <option value="3">3</option> 
      <option value="4">4</option> 
      <option value="5">5</option> 
      <option value="6">6</option> 
     </select> 
    </span> 
    <div class="buy-box__variation-error p-xs-1 mt-xs-1 text-smaller bg-red text-white rounded display-none">Please select a quantity</div> 
</div> 
</div> 
''' 
+0

我來自硒的webdriver切換到WebdriverIO並沒有回頭就可以工作。 wdio代碼總是阻塞,所以你不必擔心「等待」和競爭條件。唯一需要「等待」的時間是在頁面加載之後加載的元素,而不是頁面加載期間加載的元素。 –

+0

查看使用Select類選擇下拉菜單 - https://selenium-python.readthedocs.io/navigating.html#filling-in-forms – Amit

回答

0

的StaleElementException發生時有問題的元素是在DOM改變了HTML並且該元素的初始引用被駕駛員丟失。

您可以再次搜索元素。下面的代碼還遠遠沒有使用,你可能有,如果你決定要搜索陳舊元素錯誤後的元素

from selenium.webdriver.support.select import Select as WebDriverSelect 
options = WebDriverSelect(driver.find_elements_by_xpath("//select[@name='listing_variation_id']")).options 

for i in range(len(options)): 
    try: 
     options[i].click() 
    except StaleElementReferenceException: 
     options = WebDriverSelect(driver.find_elements_by_xpath("//select[@name='listing_variation_id']")).options 
     if not options.is_selected(): 
      options[i].click() 
相關問題