Reputation: 21
I want to verify if my required text is appearing on page. i cant use selenium webdriver's gettext()
method as its throwing permission exception. so i am using javascript to compare the 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 am getting this exception: org.openqa.selenium.remote.
ErrorHandler
$UnknownServerException: element.text is undefined
Upvotes: 0
Views: 528
Reputation: 25056
Many JavaScript issues here as well as a severe lack of understanding about selectors. contains
isn't part of the standard and won't work with older browsers:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/contains
If there is element.text
is undefined for whatever reason, you'll need to cope with that:
if (element && element.text && element.text.contains('mytext'))
(meaning, if "element" is something and element.text is something and if element.text contains 'mytext')
Also, I can see you are basically trying to do a contains search on some text - you absolutely do not need JavaScript for this. It can be done using an XPath selector!
Upvotes: 1