{"action":"created","issue":{"url":"https://api.github.com/repos/gempesaw/Selenium-Remote-Driver/issues/96","labels_url":"https://api.github.com/repos/gempesaw/Selenium-Remote-Driver/issues/96/labels{/name}","comments_url":"https://api.github.com/repos/gempesaw/Selenium-Remote-Driver/issues/96/comments","events_url":"https://api.github.com/repos/gempesaw/Selenium-Remote-Driver/issues/96/events","html_url":"https://github.com/gempesaw/Selenium-Remote-Driver/issues/96","id":24972069,"number":96,"title":"ask if an element exist.","user":{"login":"sergiod1981","id":5153817,"avatar_url":"https://avatars.githubusercontent.com/u/5153817?v=3","gravatar_id":"","url":"https://api.github.com/users/sergiod1981","html_url":"https://github.com/sergiod1981","followers_url":"https://api.github.com/users/sergiod1981/followers","following_url":"https://api.github.com/users/sergiod1981/following{/other_user}","gists_url":"https://api.github.com/users/sergiod1981/gists{/gist_id}","starred_url":"https://api.github.com/users/sergiod1981/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sergiod1981/subscriptions","organizations_url":"https://api.github.com/users/sergiod1981/orgs","repos_url":"https://api.github.com/users/sergiod1981/repos","events_url":"https://api.github.com/users/sergiod1981/events{/privacy}","received_events_url":"https://api.github.com/users/sergiod1981/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/gempesaw/Selenium-Remote-Driver/labels/FAQ","name":"FAQ","color":"009800"},{"url":"https://api.github.com/repos/gempesaw/Selenium-Remote-Driver/labels/usage","name":"usage","color":"e11d21"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":15,"created_at":"2014-01-02T16:09:35Z","updated_at":"2015-01-19T20:08:43Z","closed_at":null,"body":"I need to ask in my script if an element exist or not. and create steps for yes or no.\r\n\r\nHow I can evaluate if an element exist or not (also I need to skip the step if the element doesn't exist)\r\n\r\n\r\nthanks a lot."},"comment":{"url":"https://api.github.com/repos/gempesaw/Selenium-Remote-Driver/issues/comments/70552791","html_url":"https://github.com/gempesaw/Selenium-Remote-Driver/issues/96#issuecomment-70552791","issue_url":"https://api.github.com/repos/gempesaw/Selenium-Remote-Driver/issues/96","id":70552791,"user":{"login":"gempesaw","id":1437272,"avatar_url":"https://avatars.githubusercontent.com/u/1437272?v=3","gravatar_id":"","url":"https://api.github.com/users/gempesaw","html_url":"https://github.com/gempesaw","followers_url":"https://api.github.com/users/gempesaw/followers","following_url":"https://api.github.com/users/gempesaw/following{/other_user}","gists_url":"https://api.github.com/users/gempesaw/gists{/gist_id}","starred_url":"https://api.github.com/users/gempesaw/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gempesaw/subscriptions","organizations_url":"https://api.github.com/users/gempesaw/orgs","repos_url":"https://api.github.com/users/gempesaw/repos","events_url":"https://api.github.com/users/gempesaw/events{/privacy}","received_events_url":"https://api.github.com/users/gempesaw/received_events","type":"User","site_admin":false},"created_at":"2015-01-19T20:08:43Z","updated_at":"2015-01-19T20:08:43Z","body":"> Hmm, just did some poking around in the source. Looks like the `find_elements` method is literally built to croak whenever it gets an error message. \r\n\r\nYeah, this is true - it does `eval` and then checks `$@`. But, for `find_elements` (plural), finding 0 elements doesn't throw an exception. find_elements can throw an exception on the server for different reasons, like an invalid xpath expression for example: https://gist.github.com/gempesaw/0473b78f43c1a7654d7c\r\n\r\n> But that means that there is basically no way to prevent the program crashing whenever it hits an error from failing to find an element. \r\n\r\nsingular `find_element`: try/catch, or `find_elements` first and check size\r\nplural `find_elements`: check size of array after it returns\r\n\r\nNeither of those are clean, but I think it's pretty standard practice amongst all the webdriver bindings to treat those java Exceptions as something to die on and make the user handle try/catching them, or doing a `find_elements` before a potentially missing `find_element`. For example, the [C# bindings](http://iainrose.tumblr.com/post/28060274976/using-by-objects-to-verify-if-webelement-present) and the ruby watir bindings kill the script when attempting to find a missing element, and require the user to circumvent the issue manually. With watir, we see the same exact issue: we must begin/rescue (aka try/catch) or else find_element kills us.\r\n\r\n```\r\nrequire 'watir-webdriver'\r\n\r\nbrowser = Watir::Browser.new :firefox\r\nbrowser.goto \"http://google.com\"\r\nbegin\r\n  browser.text_field(:name => 'missing').set(\"WebDriver rocks!\")\r\nrescue\r\n  puts 'We must explicitly catch this failed find_element'\r\nend\r\n\r\nputs 'Or else it will kill the script'\r\nbrowser.text_field(:name => 'missing').set(\"WebDriver rocks!\")\r\n```\r\n\r\nI agree that it's a big PITA that the test dies on an unexpectedly un-found element, but I can also see that from the test's point of view, not being able to find an element is a big deal - it does seem like a death-worthy event, unless the programmer has explicitly prepared for it. For all we know, the entire script depends on that element being around and able to do things; better to die sooner close to the issue than later down the road.\r\n\r\nI also take responsibility for the fact that this isn't clear. I need to make this information/workarounds publicly available in the readme/pod/wiki instead of forcing everyone to dig into the guts of `find_elements?` with me, no matter how much fun it is :P"}}