如何解决在硒webdriver,ruby中处理jquery定位器
|| 我正在尝试使用ruby在Selenium Webdriver中使用jquery定位器。这是我的代码:require \"selenium-webdriver\"
driver = Selenium::WebDriver.for(:remote,:desired_capabilities => :firefox)
driver.navigate.to(\"http://google.com\")
# input.lst is the search input text in google.com
selector = \"input.lst\"
# get element from the locator
element = driver.execute_script(\"return $(#{selector}).get(0);\")
# type \"google\" into the input text
element.send_keys \"google\"
我试过element = driver.find_element(),它工作正常,所以我认为也许有一种更通用的方法来完成所有的jQuery定位器。这就是为什么我尝试execute_script的原因。但是,似乎execute_script函数已损坏。我从sun.reflect,java.lang或org.openqa中收到很多错误...
P / S:如果有人知道如何在webdriver中添加add_location_strategy,请告诉我有关:D。 Selenium rc如此出色,我找不到在webdriver中实现它的方法。
解决方法
导航到所需页面后,但使用jQuery之前:
# load the JavaScript file into memory as a string
jQuerify = ...
# add jQuery to the current page
driver.execute_async_script(jQuerify)
这是提到的JavaScript文件:
/*** dynamically load jQuery ***/
(function(callback) {
var JQUERY_URL = \'http://code.jquery.com/jquery-latest.min.js\';
if (typeof jQuery == \'undefined\') {
var script=document.createElement(\'script\');
script.src = JQUERY_URL;
var head = document.getElementsByTagName(\'head\')[0];
var done = false;
script.onload = script.onreadystatechange = (function() {
if (!done && (!this.readyState
|| this.readyState == \'loaded\'
|| this.readyState == \'complete\')) {
done = true;
script.onload = script.onreadystatechange = null;
callback(); // tell WebDriver we are done
head.removeChild(script);
}
});
head.appendChild(script);
}
else {
callback();
}
})(arguments[arguments.length - 1]);
该片段将被WebDriver粘贴到匿名函数的主体中,从而将jQuery置于全局范围内。文件内容应被缓存以便重复调用。
(从jQuerify大量借用)
哦,通常来说,选择元素时应该坚持使用Ruby API。直接使用JavaScript被认为是一种解决方法,并且与WebDriver的精神背道而驰。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。