一致地给StaleElementReferenceExcepti

一致地给StaleElementReferenceExcepti

本文介绍了Python Selenium WebDriverWait和单击不一致地给StaleElementReferenceException()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我在这里尝试解释这个问题,我什至还没有为自己解决过.我正在将Selenium与python绑定一起使用,并且在使用WebDriverWait后跟click()事件时,似乎没有随机找到页面元素的问题.这是我的代码:

Ok, here goes my attempt to explain this problem that I haven't even figured out for myself yet. I'm using Selenium with the python-bindings and seem to have an issue with a page element randomly not being found when when using WebDriverWait followed by a click() event. Here is my code:

yearOption = WebDriverWait(self.br, 40).until(lambda d: d.find_element_by_xpath("//select[@name='ctl00$holdSection$rptCommissionYears']/option[@value='%s']" % year), self.br)
print yearOption.text
yearOption.click()

此命令集处于for循环中,并且会在.click()事件上随机失败,并产生错误:打印yearOption.text后出现StaleElementReferenceException()错误.这对我来说是完全奇怪的,因为WebDriverWait行显然找到了该元素,并且在单击该元素之前我没有重新加载或更改浏览器状态...

This command set is in a for loop and will randomly fail on the .click() event producing the error: StaleElementReferenceException() after the yearOption.text is printed. This is completely odd to me since the WebDriverWait line obviously found the element, and I haven't reloaded or changed the browser state before clicking the element...

有什么主意为什么会出现此错误?请记住,实际上并不能始终如一地发生-有时,我的整个脚本会成功执行而不会出现错误.

Any ideas why I would be getting this error? Remember, it doesn't happen consistently, infact--sometimes, my entire script will execute successfully with no errors.

推荐答案

我之前也遇到过类似的问题,并且我99%确信您的问题是相同的.

I have run into a similar issue before, and I am 99% sure that your problem is the same.

如果您检查循环为:

  1. 寻找这个元素
  2. 点击
  3. 寻找相同的元素
  4. 点击

通常,单击后会重新加载某些页面或进行更改.这可能会影响您要搜索的元素.而且,如果您不小心,最终可能会在重新加载元素之前先查找它,而当您单击该元素时,该元素ID已经更改,因此会给您一个过时的异常.

Normaly after a Click, some page reload or changes occur. This may affect to the element that you are searching for. And if you don't take care, you may end up looking for the element before is reloaded, and when you click it, the element ID already changed, therefore giving you a Stale exception.

让我们一个一个地走

  1. 页面加载,并且您的元素具有ID = 1
  2. 找到了.
  3. 单击它,然后重新加载/更改就开始发生
  4. 您输入另一个循环并找到该元素.请注意,这甚至可以很快发生,因为单击后无需等待,因此find可以退出并再次给您ID = 1元素.您尝试单击元素ID = 1,但由于已重新加载,因此不再存在.
  1. Page loads and you element has ID=1
  2. You find it.
  3. You click it and reload/changes start to happen
  4. You enter another loop and find the element. Notice that this evencan happen really fast as there is no wait after click, and thusfind can exit giving you ID=1 element again. You try to click theelement ID=1, but since it was reloaded, it does not exist anymore.

您可以通过不同的方式解决此问题:

You can fix this in different ways:

  1. 如果速度不是问题,则可以在单击后添加明确的等待几秒钟,就给了足够的时间来完成javascript.
  2. 您可以保存元素的ID,每次查找时,您检查是否不同,否则,请等待并重试.
  1. If speed is not an issue, you can add an explicit wait after clickof a few seconds, giving enough time to javascript to finish.
  2. You can save the ID of the element, and every time you look for it,you check that is different, and if it is not, you wait and retry.

鉴于此,如果这不是您的问题,您可以随时共享更多代码和测试目标,我们将很乐意为您提供帮助.

Giving this, if that is not your problem, you can always share more of your code and your testing target and I would be happy to help.

这篇关于Python Selenium WebDriverWait和单击不一致地给StaleElementReferenceException()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 19:04