2017-10-28 70 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() 
相关问题