因此,我试图找出一种方法来自动单击所选的选择元素选项,但是当我使用selenium站点提供的代码来导航选项时,我得到了stale element exception错误。我尝试使用等待时间来等待元素加载,但是无论我把等待时间放在哪里,它都会给我一个错误。它确实通过第一个选择并选择了一个选项,但对于第二个选项,它要么遍历每个选项并单击,然后给我一个错误,而不在屏幕上更新它,要么遍历一半并给出陈旧的元素错误。
这是我下面代码的一部分:
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>
'''发布于 2017-10-29 08:16:32
当所讨论的元素在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()发布于 2018-09-22 22:32:08
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) {
}
}这肯定会起作用,而且很简单。
https://stackoverflow.com/questions/46992406
复制相似问题