有什么办法可以通过 webdriver 在浏览器屏幕中选择一行文本来模拟键盘或鼠标?
我想使用 WebDriver 在浏览器屏幕(实际上是在 CKEditor 编辑区域)中选择一行文本,然后从 CKEditor 工具栏更改其文本样式。有什么方法可以做到这一点吗?
例如,一行 html 代码如下:
this is a sample line.我尝试使用 Actions 构建鼠标动作链,但由于不熟悉而没有成功。感谢您的任何提示或答案。
I want to use WebDriver to select a line of text in browser screen(actually in CKEditor editing area) then change its text style from CKEditor toolbar. Any method can do that?
For example, a line with html code like below:
this is a sample line.
I try to use Actions to build a mouse action chain but no success due to not familiar with that. Thanks for any hints or answer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定这对于 WebDriver 是否真的可行。您想要做的是
clickAndHold(...).moveByOffset(...).release(...)
。不幸的是,WebDriver 只允许将WebElement
作为clickAndHold()
的参数。我给你的最好建议是为此模拟 JavaScript 事件。然后,您可以在测试中执行类似的操作:
我编写了用于模拟鼠标事件的代码,我将其与我的一些 Selenium 测试一起使用。虽然它不能完全满足您的要求,但希望它会很有用(希望您可以设置 x/y 坐标,也许这会使其正常工作):
如果您可以用键盘做您想做的事情,这是迄今为止更好的解决方案。您只需执行
driver.sendKeys(...)
即可。在这种情况下,Keys 枚举对您来说将是无价的:-)I'm not sure this is actually possible with WebDriver. What you would want to do is to
clickAndHold(...).moveByOffset(...).release(...)
. Unforunately, WebDriver only allows aWebElement
as a parameter toclickAndHold()
.My best advice to you is to emulate JavaScript events for this. You can then do something like this in your test:
I wrote code for emulating mouse events which I use with some of my Selenium tests. Although it doesn't do exactly what you want, hopefully it will be useful (hopefully you can just set the x/y coords and perhaps that will get it working):
If it's possible for you to do what you want with the keyboard instead, this is by far the better solution. You can simply do
driver.sendKeys(...)
. The Keys enum will be priceless for you in this case :-)您还可以尝试使用 JavaScript 来完成此操作:
You can also try to do it with JavaScript: