using javascript in selenium javascript executor -


i want verify if required text appearing on page. cant use selenium webdriver's gettext() method throwing permission exception. using javascript compare text.

 string scripttoexec = "var result = document.getelementsbyclassname('sender');     for(var element in result){     if (element.text.contains('mytext'))     {return true;}     else     {return false;}}";                javascriptexecutor js = (javascriptexecutor) driver;     boolean result = (boolean) (js.executescript(scripttoexec)); 

i getting exception: org.openqa.selenium.remote.

errorhandler $unknownserverexception: element.text undefined

many javascript issues here severe lack of understanding selectors. contains isn't part of standard , won't work older browsers:

https://developer.mozilla.org/en-us/docs/web/javascript/reference/global_objects/string/contains

if there element.text undefined whatever reason, you'll need cope that:

if (element && element.text && element.text.contains('mytext')) 

(meaning, if "element" something , element.text something , if element.text contains 'mytext')

also, can see trying contains search on text - absolutely do not need javascript this. can done using xpath selector!


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -