Selenium IDE: How to continue script on element not found or on error

偶尔善良 提交于 2019-12-19 17:46:31

问题


I need your help. I just want to continue my Selenium IDE script on Firefox even there's an error or element not found. I'm using script with HTML format.


回答1:


You have to make an explicit check if an element is present before using this element in the next command (which might cause an error and break an execution of a script). User extension "sideflow" enables conditional jumps inside your code.

Here is home page of sideflow extension: https://github.com/darrenderidder/sideflow

With sideflow extension you may use code like this:

storeElementPresent id=btnRecSearch isPresent
gotoIf  ${isPresent} == false   End
click   id=btnRecSearch 
....
label   End



回答2:


This example is for the new Selenium IDE version 3 that works on Firefox and Chrome. The basic premise:

1) store xpath count -- this saves the count of elements that match to a variable

store xpath count | xpath=//body[@id='error-page'] | error

2) if -- in this case test the count is more than 0

if | ${error} > 0

3) do some magic stuff

4) end

end

Here is an article I posted using the same example: http://lance.bio/2018/12/21/selenium-ide-if-element-exists/




回答3:


If you use all verify related command then your script do not stop if element is not found by seleniun IDE but if you use assert related command then your script is stop if that element is not found by selenium ide

If have to create your script using verifytext command then run that script and see your script will not stop if element is not displayed




回答4:


You can also use plugin Selblocks which implements "catch" method in order to catch error and continue the script anyway. try/catch/finally blocks Try can provide special handling when an error is thrown, and can guarantee execution of, for example, "cleanup" logic.

A catch block specifies what to do if an error is thrown in the try block. That is, if any statement within the try block, or in a function or sub-function called from within the try block, throws an error, then control jumps to the matching catch block, and the error is cleared. If no error is thrown in the try block, then the catch block is skipped. Catch intercepts a given error by matching on its error message, which is specified as a "substring", or as a /regular expression/. Providing no specification will catch all errors.

A finally block executes after the try and catch blocks. It always executes regardless of whether or not an error is thrown or caught. It executes even if the try or catch block concludes via "flow change" commands: continue, break, return, or exitTest commands.

Try blocks can be nested. If an inner try does not catch an error, the enclosing try/catch is entered. The contents of all nested finally block(s) are guaranteed to execute, innermost to outermost - again, even if error(s) occur along the way.

Both catch and finally are optional, but if neither is provided then the try simply has no effect.

An optional identifier may be specified on try and endTry, which will be validated for proper pairing.

Example of try/catch/finally try|outer try|inner throw|"blamo" getEval|alert("this alert will NOT get executed") finally getEval|alert("1st") endTry|inner catch|/blamo/ getEval|alert("2nd") endTry|outer You can read more here http://refactoror.wikia.com/wiki/Selblocks_Reference




回答5:


The Selenium IDE plug-in is intended more for recording (i.e., showing you the code behind the actions); it does allow some playback, but that's pretty limited and I'm not sure it lets you handle errors. Now if you you're willing to use actual programming, then you can easily handle errors. E.g., in Python you can use something like this:

try:
    driver.find_element_by_id("userid").click()
except NoSuchElementException:
    # do something else (close page, give you a warning, etc)



回答6:


Here is how I solved the problem, example with a SELECT command :

I created a trySelect custom command which act like a normal select but don't stop the test if it fails to find the element

1) create file 'user-extensions.js' with the following code

Selenium.prototype.doTrySelect = function(locator,target) {
    try {
        return Selenium.prototype.doSelect.call(this,locator,target);
    } catch(err) { return null; }   
};

2) in Selenium IDE => options => options => general : add your 'user-extensions.js' file in the 3rd input, as below

3) restart Selenium IDE

4) example of use (like a normal SELECT)

trySelect | //select | label=regexpi:.*Hey.*

You can do the same for other commands which might fail, like click for example.




回答7:


  1. Open a page in Mozilla browser and Selenium IDE.
  2. Right click on text and choose waitForElementPresent option.
  3. The command gets added in the recorded script.
  4. Once the element is verified and present the next command could be either click or type as per the users need and target will always be the element id.


来源:https://stackoverflow.com/questions/18349435/selenium-ide-how-to-continue-script-on-element-not-found-or-on-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!