Python 陈旧元素引用异常
因此,我试图找到一种方法来自动单击所选的selection elements选项,但当我使用selenium站点提供的代码来导航选项时,会出现stale element异常错误。我尝试使用等待时间来等待元素加载,但无论我将等待时间放在哪里,它都会给我一个错误。它确实通过了第一个选择并选择了一个选项,但对于第二个选择,它要么通过每个选项并单击,然后在屏幕上给我一个错误而不更新它,要么通过了一半并给出了陈旧元素错误 这是我下面代码的一部分:Python 陈旧元素引用异常,python,python-3.x,selenium,selenium-webdriver,Python,Python 3.x,Selenium,Selenium Webdriver,因此,我试图找到一种方法来自动单击所选的selection elements选项,但当我使用selenium站点提供的代码来导航选项时,会出现stale element异常错误。我尝试使用等待时间来等待元素加载,但无论我将等待时间放在哪里,它都会给我一个错误。它确实通过了第一个选择并选择了一个选项,但对于第二个选择,它要么通过每个选项并单击,然后在屏幕上给我一个错误而不更新它,要么通过了一半并给出了陈旧元素错误 这是我下面代码的一部分: displayed = browser.find_elem
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>
'''
当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()
这肯定会奏效,而且很简单。我从Selenium WebDriver切换到WebDrivero,没有回头看。wdio代码总是阻塞的,所以您不必担心等待和竞争条件。您唯一需要等待的时间是在页面加载之后而不是在页面加载期间加载元素-
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) {
}
}