用户界面中具有以下字段,其中已实现了onblur事件。当我们手动执行步骤时,它可以工作,但是当我们使用Webdriver 2.45版本执行相同的步骤时,它无法在total字段中获取更新的值。
HTML代码:
<input name="percent_list" id="percent_list" type="text" value="1" align="right" class="formInput" onblur="calculate();">
<input name="percent_sell" id="percent_sell" type="text" value="1" align="right" class="formInput" onblur="calculate();">
<input name="total_commision" id="total_commision" type="text" value="2.00" readonly="yes" class="formInput">
测试数据:
-场1:1.00
-场2:1.00
-验证字段(总计):2.00
脚步:
-输入字段1和2的值。
-验证“总计”字段是否具有字段1和2的总和。
观察:我们看到由于事件是onblur,它是手动工作的,但是使用IE浏览器和Selenium时,UI不会自动填充总和值。
尝试过的方法:
-Sendkeys:没有用。尝试点击其他网络元素也没有效果。
-使用过的Java脚本编辑器也不起作用。我们可以更新字段1和2,但字段总和不会自动求和。
-也尝试使用Keys.TAB。它没有用。
码:
driver.findElement(By.id("percent_list")).sendKeys("2.5");
//JavascriptExecutor jse = (JavascriptExecutor) driver;
// jse.executeScript("arguments[0].setAttribute('value', arguments[1])", driver.findElement(By.id("percent_sell")), Keys.TAB);
//jse.executeScript("arguments[0].setAttribute('value', arguments[1])", driver.findElement(By.id("referral_fee_percent")), Keys.TAB);
driver.findElement(By.id("listing_agent_bonus")).click();
有什么建议可以解决这种与onblur相关的行为吗?
最佳答案
分析
模糊方法对不是文档中活动元素的元素无效。
由于InternetExplorerDriver仅限于Windows,因此它尝试使用所谓的“本机”或OS级事件在浏览器中执行鼠标和键盘操作。这与对相同的操作使用模拟的JavaScript事件相反。
我猜想InternetExplorerDriver的click()并按Tab键不会更改活动元素,因此即使单击其他元素也不会触发模糊。
解
我使用selenium-webdriver Nodejs包准备了一个JavaScript代码段,它打开github login page并通过sendKeys()输入用户名和Tab键
我在Chrome / Firefox / IE 11上运行脚本,在调用sendKeys()之后,我注意到密码文本框从页面获取焦点,并且脚本打印出活动元素也是3个浏览器上的密码文本框。
您能否在IE浏览器上运行以下代码,并看着密码文本框获得焦点?打印出来的活动元素也是密码文本框吗?
如果以上两个答案是肯定的,则通过将网址更改为网站并将用户名文本框更改为“字段1”来修改代码以测试您的网页,然后运行以查看会发生什么。
如果两个答案中的任何一个是否定的,请更改您的InternetExplorerDriver版本,然后再次运行
感谢您抽出宝贵的时间来运行我的脚本来证明我的猜测,希望我们可以从此问题中学到更多,不仅使用Google搜索提供的解决方案,还不知道为什么。
var webdriver = require('selenium-webdriver'),
By = webdriver.By,
Key = webdriver.Key,
Capabilities = webdriver.Capabilities;
function getDriver(browserName) {
if (browserName === 'internet explorer') {
let capabilities = Capabilities.ie();
capabilities.set('ignoreProtectedModeSettings', true);
capabilities.set('ignoreZoomSetting', true);
return new webdriver.Builder()
.forBrowser(browserName)
.withCapabilities(capabilities)
.usingServer('http://localhost:4444/wd/hub')
.build();
} else {
return new webdriver.Builder()
.forBrowser(browserName)
.usingServer('http://localhost:4444/wd/hub')
.build();
}
}
function login_github(driver) {
driver.get('https://github.com/login');
driver.sleep(5000);
driver.findElement(By.css('#login_field')).sendKeys('abc@gmail.com', Key.TAB);
driver.sleep(2000);
// expect password text box obtain focus and become active element
// below code should print out the html code of password text box
// <input class="form-control form-control input-block" id="password"
// name="password" tabindex="2" type="password">
driver.executeScript("return new XMLSerializer().serializeToString(document.activeElement);")
.then(function(html) {
console.log('activeElement html: ' + html);
})
driver.sleep(5000);
driver.quit();
}
//run on chrome
// login_github(getDriver('chrome'));
//run on firefox
// login_github(getDriver('firefox'));
//run on ie
login_github(getDriver('internet explorer'));
仅供参考,我在代码中使用了RemoteWebDriver,所以我使用了Nodejs软件包:webdriver-manager在本地安装了硒服务器。我使用以下版本:
standalone-server 3.4.0
InternetExplorerDriver-32bit 3.5.1
chromedriver 2.30
geckodriver v0.19.0
chrome browser 60
firefox browser 55
ie browser 11
下面是我安装webdriver.exe和独立服务器的命令
webdriver-manager update --versions.standalone=3.4.0 --versions.chrome=2.30 --verions.ie=3.5.1 --version.gecko=v0.19.0
下面是我启动硒服务器的命令:
webdriver-manager start --versions.standalone=3.4.0 --versions.chrome=2.30 --verions.ie=3.5.1 --version.gecko=v0.19.0
关于javascript - Selenium 中的Onblur事件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46373745/