所以我试图想办法自动点击选择元素选项,但是当我使用selenium网站提供的代码导航选项时,我得到陈旧元素异常错误。我已经尝试使用等待时间等待元素加载,但无论我在哪里等待它都会给我一个错误。它确实通过了第一个选择并选择了一个选项,但是对于第二个选项,它要么经过每个选项,然后单击然后给我一个错误,而不是在屏幕上更新它,或者它通过一半并给出陈旧元素错误。
这是我的代码的一部分:
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()
这是我试图导航的HTML:
'''
<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>
'''
答案 0 :(得分:1)
当在dom上更改了有问题的元素并且驱动程序丢失了对该元素的初始引用时,就会发生StaleElementException。
您可以再次搜索该元素。下面的代码远没有使用,如果您决定在陈旧元素错误后搜索元素,则可能需要对其进行处理
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()
答案 1 :(得分:0)
for(int i =0; i<2;i++) {
try {
WebElement se = driver.findElement(By.xpath("desired xpath"));
js.executeScript("arguments[0].click()", se);
break;
}
catch(StaleElementReferenceException e) {
}
}
这可以肯定并且很简单。