try:
submit = driver.find_element(By.XPATH,submit_buttonId)
submit.click()
except:
submit = driver.find_element(By.XPATH,submit_buttonId)
submit.send_keys(Keys.ENTER)
当我尝试让它单击提交按钮时,这是唯一有效的代码块
任何其他方法,例如
submit = driver.find_element(By.XPATH,submit_buttonId)
submit = driver.find_element(By.XPATH,submit_buttonId)
submit.send_keys(Keys.ENTER)
尝试预测错误..或
try:
submit = driver.find_element(By.XPATH,submit_buttonId)
submit.click()
except:
submit = driver.find_element(By.XPATH,submit_buttonId)
submit.click()
实际上是相同的事情,但是点击
不起作用,并且总是给出“selenium.common.exceptions.StaleElementReferenceException:消息:陈旧元素引用:未找到陈旧元素”
我不确定它如何以及为什么只能以这样一个特定的顺序工作,提交按钮也始终可见,并且如果手动则可单击
在没有看到页面的情况下很难确定,但听起来页面正在加载,然后页面的至少一部分被重新加载。这会创建 StaleElementException。
避免这种情况的方法是等待该元素,然后等待它变得陈旧,然后等待它可单击,然后单击它。听起来工作量很大,但此过程应该消除这些步骤中的许多间歇性(和确定性)故障。
代码看起来像这样
wait = WebDriverWait(driver, 10)
# define the locator since we're going to use it more than once
submit_locator = (By.XPATH, submit_buttonId)
# wait for the page to load enough that the element exists
submit = wait.until(EC.presence_of_element_located(submit_locator))
# wait for the element to go stale
wait.until(EC.staleness_of(submit))
# now that it's gone stale, now we wait for it to be clickable and click it
wait.until(EC.element_to_be_clickable(submit_locator)).click()