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="{"user_is_listing_owner":false,"order_already_started":false,"inline_variation_labels":false,"listing_mode":"listing_mode_default","show_preview_warning":false,"quantity_behavior":"quantity_enabled","quantity_related_nudge_is_on":true,"additional_button_class":"","is_mobile":false,"channel":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&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>
'''
我來自硒的webdriver切換到WebdriverIO並沒有回頭就可以工作。 wdio代碼總是阻塞,所以你不必擔心「等待」和競爭條件。唯一需要「等待」的時間是在頁面加載之後加載的元素,而不是頁面加載期間加載的元素。 –
查看使用Select類選擇下拉菜單 - https://selenium-python.readthedocs.io/navigating.html#filling-in-forms – Amit