[英]using javascript in selenium javascript executor
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. 我不能使用硒webdriver的gettext()
方法作为其抛出权限异常。 so i am using javascript to compare the text. 所以我正在使用JavaScript来比较文本。
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. 我收到此异常:org.openqa.selenium.remote。
ErrorHandler
$UnknownServerException: element.text is undefined ErrorHandler
$ UnknownServerException:element.text未定义
Many JavaScript issues here as well as a severe lack of understanding about selectors. 这里有许多JavaScript问题,以及对选择器的严重缺乏了解。 contains
isn't part of the standard and won't work with older browsers: contains
不是标准的一部分,并且不适用于较旧的浏览器:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/contains 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: 如果由于任何原因未定义element.text
则需要解决:
if (element && element.text && element.text.contains('mytext'))
(meaning, if "element" is something and element.text is something and if element.text contains 'mytext') (这意味着,如果“ element”是某物,而element.text是某物 ,并且element.text包含“ 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. 另外,我可以看到您基本上是在尝试对某些文本进行包含搜索-您绝对不需要JavaScript 。 It can be done using an XPath selector! 可以使用XPath选择器来完成!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.