Check for a stale element using selenium 2?

前端 未结 2 461
温柔的废话
温柔的废话 2021-01-13 09:03

Using selenium 2, is there a way to test if an element is stale?

Suppose I initiate a transition from one page to another (A -> B). I then select element X and test

2条回答
  •  不知归路
    2021-01-13 09:11

    I don't know what language you are using there but the basic idea you need in order to solve this is:

    boolean found = false
    set implicit wait to 5 seconds
    loop while not found 
    try
      element.click()
      found = true
    catch StaleElementReferenceException
      print message
      found = false
      wait a few seconds
    end loop
    set implicit wait back to default
    

    NOTE: Of course, most people don't do it this way. Most of the time people use the ExpectedConditions class but, in cases where the exceptions need to be handled better this method ( I state above) might work better.

提交回复
热议问题