我是这个的新手,请耐心等待。 我试图执行连续的按键循环,并一直在可见的特定元素上释放它。这就是我所拥有的:
def do something(self):
element = driver.find_element_by_xpath(variable)
while element.is_displayed():
ActionChains(driver).click_and_hold(element).perform()
time.sleep(2)
ActionChains(driver).release(element).perform()
time.sleep(2)
driver.quit()
问题在于,即使该元素不再可见,它似乎仍会尝试执行该操作。预计该元素将在随机的时间后消失,因此循环应结束,但我总是会收到错误:
StaleElementReferenceException: Message: The element reference of <div id="hearing-test-button" class="btn-mega null"> is stale; either the element is no longer attached to the DOM, it is not in the current frame context, or the document has been refreshed
答案 0 :(得分:0)
try:
driver.find_element_by_xpath(variable)
while True:
element = driver.find_element_by_xpath(variable)
ActionChains(driver).click_and_hold(element).perform()
time.sleep(quality)
try:
driver.find_element_by_xpath(variable)
if True:
element = driver.find_element_by_xpath(variable)
ActionChains(driver).release(element).perform()
time.sleep(quality)
except NoSuchElementException:
return False
except NoSuchElementException:
return False