如何正确地关注某个元素?
我在 C# 中使用 selenium Webdriver 进行 webtest。但我遇到一个问题,当浏览器窗口不是全尺寸时,弹出窗口将在可见区域之外打开一半。
问题是当我触发 .Click(); 时它不会执行任何操作,因为我尝试单击的链接位于查看区域之外。
那么我该如何关注链接才能点击工作呢?我目前正在使用以下解决方法,但我认为这不是一个好方法。
_blogPostPage.FindElement(By.XPath(_popupLogin)).SendKeys("");
_blogPostPage.FindElement(By.XPath(_popupLogin)).Click();
带空格的发送键专注于链接并使 Click 每次都起作用,但是没有正确的方法吗?
Im doing webtest using selenium Webdriver in C#. But I'm having a problem where when the browser window isn't in full size a popup will open half way outside the visible area.
The problem is that when i fire a .Click(); it doesn't do anything because the link i attempt to click is outside of the viewed area.
So how do i focus on the link to get click to work? Im currently using the following workaround but i don't think that's a nice way.
_blogPostPage.FindElement(By.XPath(_popupLogin)).SendKeys("");
_blogPostPage.FindElement(By.XPath(_popupLogin)).Click();
The sendkeys with space focuses on the link and makes Click work everytime, but isn't there a right way to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我们一直在使用 Selenium,也遇到了这个问题。我不知道是否是 WebDriver 整体、C# 实现、Firefox 版本等问题,但我们找到了一个不错的解决方法:
诀窍是强制 Selenium 评估
LocationOnScreenOnceScrolledIntoView
属性RemoteWebElement
类(由FirefoxWebElement
继承并实现IWebElement
)。这会强制浏览器滚动以便该元素位于视图中。我们做到这一点的方法是使用扩展方法:
这样我们所要做的就是将生成的代码从: 更改
为:
希望它适合您!?
We've been playing with Selenium and have run into this problem as well. I don't know if it's the WebDriver as a whole, the C# implementation, the version of Firefox etc, but we have found an ok workaround:
The trick is to force Selenium to evaluate the
LocationOnScreenOnceScrolledIntoView
property on theRemoteWebElement
class (which is inherited byFirefoxWebElement
and implementsIWebElement
). This forces the browser to scroll so that the element is in view.The way we've done it is to use an extension method:
this way all we have to do is change the generated code from:
to:
Hope it works for you!?
不要发送键来获取空白值,而是发送它来获取空间。这是选择复选框的键盘快捷键。
只需将代码替换
为:
Instead of doing send key for blank value, send it for space. Thats the keyboard shortcut to select a checkbox.
Just replace the code :
by
driver.find_element(:id, "edit-section").send_keys " "
与对我有用的空间。我将 webdriver rspec 与 selenium-server-2.24.1 一起使用,并且在使用 IE8 时遇到了问题 - 我不断收到
Selenium::WebDriver::Error::ElementNotVisibleError
。它可以在 IE9 和 FF 中运行,但不能在 IE8 中运行,直到我添加了 send_keys " "。driver.find_element(:id, "edit-section").send_keys " "
with the space worked for me.I am using webdriver rspec with selenium-server-2.24.1 and I was having trouble with IE8 - I kept getting
Selenium::WebDriver::Error::ElementNotVisibleError
. It was working in IE9 and FF but not IE8 until I added send_keys " ".