Python Selenium显式等待

时间:2017-10-22 12:26:05

标签: python python-2.7 selenium jenkins

在我的Python Selenium测试中找到一种干净可靠的等待方式,我遇到了很多麻烦。我一直在使用Implicit等待很长时间,并且由于某种原因它开始变得不稳定所以我切换到显式等待。出于某种原因,我无法找到一种方法让我的测试在100%的时间内都能正常工作。现在,即使正在测试的代码和测试本身没有改变,问题也会一次又一次地发生。 (我说它在50%的时间都有效)。当问题发生时,它通常是相同的线。以下是其中一些:

for th in td_list:
    if(th.text == "18"):
        th.click()
        break
    time.sleep(3)
    element = WebDriverWait(driver, 20).until(
      EC.element_to_be_clickable((By.ID, "closeModalDisplayBill"))
    )

这里的“closeModalDisplayBill”是一个必须按下的按钮。它引发的问题如下:

Traceback (most recent call last):
  File "./scripts/test-sl/SeleniumTest.py", line 54, in test_selenium_launcher
    frontAdminErr = SlFrontAdminErr(driver, self.add)
  File "/Users/JNEB/WebstormProjects/backend/backend/scripts/test-sl/SlFrontAdminErr.py", line 45, in __init__
    driver.find_element_by_id("closeModalAddComp").click()
  File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webelement.py", line 78, in click
    self._execute(Command.CLICK_ELEMENT)
  File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webelement.py", line 499, in _execute
    return self._parent.execute(command, params)
  File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 297, in execute
    self.error_handler.check_response(response)
  File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
ElementNotInteractableException: Message: 

另一个问题反复发生的地方是Selenium试图打开一个模态来编辑某些东西(点击或清除输入非常不稳定)。这是代码:

time.sleep(5)
driver.implicitly_wait(15)
driver.find_element_by_id("btnLogin").click()
time.sleep(1)
driver.find_element_by_id("login-email").clear()

以下是发生错误时的日志(尝试清除登录电子邮件输入字段):

Traceback (most recent call last):
  File "./scripts/test-sl/SeleniumTest.py", line 39, in test_selenium_launcher
    frontUserTest = SlFrontUser(self, driver, self.add)
  File "/Users/JNEB/WebstormProjects/backend/backend/scripts/test-sl/SlFrontUser.py", line 21, in __init__
    driver.find_element_by_id("login-email").clear()
  File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webelement.py", line 93, in clear
    self._execute(Command.CLEAR_ELEMENT)
  File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webelement.py", line 499, in _execute
    return self._parent.execute(command, params)
  File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 297, in execute
    self.error_handler.check_response(response)
  File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
InvalidElementStateException: Message: Element is not currently interactable and may not be manipulated

相同的测试可以连续四到五次成功,然后错误相同的时间。 (这里的所有内容都是从我自己的计算机上测试的)。当这些测试通过Jenkins完成时会变得更糟。有些时候可以在10或8分钟内完成全套测试,但有些时候测试将在30分钟后完成/失败。我认为Jenkins的缓慢因素可能是我的测试不稳定的原因之一,但这并不能解释为什么这些bug经常出现在我自己的计算机上。

所有这些测试都是从另一个启动firefox驱动程序实例的Python脚本启动的,然后启动所有类似的测试:

class SeleniumTestLauncher(unittest.TestCase):
    environmnent = "envName"
    add = ""
    quickbooks_url = "https://developer.intuit.com/"
    port = ""
    driver = webdriver.Firefox()
    base_url = ""

    def setUpAdd(self):
        self.driver.implicitly_wait(30)
        self.verificationErrors = []
        self.accept_next_alert = True

    def test_selenium_launcher(self):
        driver = self.driver
        ### -- Here just call every selenium test -- ###

        ## -- Test Front User -- ##
        frontUserTest = SlFrontUser(self, driver, self.add)

第二次编辑: 建议我删除explicit_wait的所有implicitly_wait()。似乎更稳定但我仍然遇到错误。例如:

element = WebDriverWait(driver, 20).until(
      EC.element_to_be_clickable((By.ID, "closeModalDisplayBill"))
)
driver.find_element_by_id("closeModalDisplayBill").click()

即使驱动程序应该等待'closeModalDisplayBill'可点击以实际尝试点击,我也会收到此错误:

Traceback (most recent call last):
  File "./scripts/test-sl/SeleniumTest.py", line 53, in test_selenium_launcher
    frontUserTest = SlFrontUser(self, driver, self.add)
  File "/Users/JNEB/WebstormProjects/backend/backend/scripts/test-sl/SlFrontUser.py", line 37, in __init__
    driver.find_element_by_id("closeModalDisplayBill").click()
  File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webelement.py", line 78, in click
    self._execute(Command.CLICK_ELEMENT)
  File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webelement.py", line 499, in _execute
    return self._parent.execute(command, params)
  File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 297, in execute
    self.error_handler.check_response(response)
  File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
ElementNotInteractableException: Message: 

编辑3: 仅使用显式等待与以前一样不稳定。我遇到的大多数错误都是:“InvalidElementStateException:Message:Element当前不可交互,可能无法操作”。我不明白为什么Selenium试图点击“输入字段”,即使我使用explicit_wait。例如:

time.sleep(3)
element = WebDriverWait(driver, 20).until(      
  EC.presence_of_element_located((By.ID, "login-email"))
)
driver.find_element_by_id("login-email").clear()

在这里,我要求selenium等待'login-email'出现,如果是,请点击它。等待在20秒超时之前返回,但.clear()函数抛出“InvalidElementStateException:Message:Element当前不可交互,可能不会被操纵”。

1 个答案:

答案 0 :(得分:1)

closeModalDialog表明您的应用程序基于某些Ajax框架。

使用Selenium自动化Ajax应用程序有时可能是一个很大的挑战。当Webriver尝试定位元素时,元素似乎在时间X可见且可点击,但仍有一些待修改的javascript代码修改DOM,并且当测试尝试点击时将在时间X + 10毫秒更改此元素在元素上,在这种情况下Webdriver通常会引发以下之一:ElementNotVisibleException,InvalidElementStateException,StaleElementReferenceException,ElementNotInteractableException。
该测试在本地(快速)计算机上运行良好,但在速度较慢的虚拟机上运行失败,或者当应用程序负载较重且响应时间增加时失败。

首先与您的开发者讨论此问题。
我们的应用基于JSF + https://github.com/bwilcox-1234/ChatScript/blob/master/SRC/mainSystem.h,我们在尝试自动化产品时遇到了许多问题。我们已经与开发人员讨论了这些问题,他们在每个页面的标题中添加了简单的Primefaces framework,表明是否存在正在进行的(活动的)ajax请求。其次,由于Primefaces正在使用jQuery(许多ajax framoworks正在使用jQuery),我们也在检查jQuery状态是否准备就绪(有关详细信息,请参阅Status component
我们已经创建了一个简单的waitForPrimefacesAndJQuery函数,它等待unitil Primefaces状态和jQuery状态准备就绪,并且在标准WebDriverWait的所有地方都使用了这个函数。
这解决了99.9%的问题。

但仍有一些组件不想合作  对于他们来说,我们通常使用强力方法 - 只需使用异常,然后再尝试几次,例如以这种方式:

for(int i=0; i <= 5; i++ ){
  try{
    findElement( element ).click();
    break;
  } catch( ElementNotVisibleException | InvalidElementStateException 
          | StaleElementReferenceException | ElementNotInteractableException )
  {
    // swallow the exception, wait some time and try again
    sleep( 500 );
  }
}