I would like for Selenium to navigate a menu via arrow keys--Starting with clicking the top menu item then pressing "DOWN", "DOWN", ...
我希望Selenium通过箭头键导航菜单 - 从单击顶部菜单项开始,然后按“DOWN”,“DOWN”,...
The problem is that you have to always supply a specific element to send the "DOWN" to.
问题是你必须提供一个特定的元素来发送“DOWN”。
Is there any way to get the current element?
有没有办法获得当前元素?
I tried:
我试过了:
by.xpath(".")
but it said that the expression was unrecognized or didn't return a proper object.
但它说表达式未被识别或未返回正确的对象。
I expect I'm jsut missing some stupid trick.
我希望我能错过一些愚蠢的伎俩。
3 个解决方案
#1
5
Don't know of a more straightforward way than accessing document.activeElement
不知道比访问document.activeElement更简单的方法
How do I test which element has the focus in Selenium RC?
如何在Selenium RC中测试哪个元素具有焦点?
#2
28
In Selenium 2.0, if you are using WebDriver
to drive the tests in the browser, you can use the WebDriver.TargetLocator
class to get the element in focus, in a window/frame:
在Selenium 2.0中,如果您使用WebDriver在浏览器中驱动测试,则可以使用WebDriver.TargetLocator类在窗口/框架中获取焦点元素:
WebDriver driver = ... // initialize the driver
WebElement currentElement = driver.switchTo().activeElement();
If no element is in focus, the active element would turn out to be the body
of the document being displayed, which might be the case when you launch a new page, for instance. When you invoke methods like click
, sendKeys
etc. you'll find the WebElement
returned by the above invocation will always represent the element in focus.
如果没有元素处于焦点,则活动元素将变成正在显示的文档的主体,例如,当您启动新页面时可能就是这种情况。当你调用click,sendKeys等方法时,你会发现上面调用返回的WebElement总是代表焦点元素。
This was tested using FirefoxDriver
, and I would suspect that the same would be true of other drivers, except for the HtmlUnitDriver
and similar drivers that do not use a full-fledged browser under the hood.
这是使用FirefoxDriver测试的,我怀疑其他驱动程序也是如此,除了HtmlUnitDriver以及不使用成熟浏览器的类似驱动程序。
#3
8
in python:
在python中:
element = driver.switch_to.active_element
#1
5
Don't know of a more straightforward way than accessing document.activeElement
不知道比访问document.activeElement更简单的方法
How do I test which element has the focus in Selenium RC?
如何在Selenium RC中测试哪个元素具有焦点?
#2
28
In Selenium 2.0, if you are using WebDriver
to drive the tests in the browser, you can use the WebDriver.TargetLocator
class to get the element in focus, in a window/frame:
在Selenium 2.0中,如果您使用WebDriver在浏览器中驱动测试,则可以使用WebDriver.TargetLocator类在窗口/框架中获取焦点元素:
WebDriver driver = ... // initialize the driver
WebElement currentElement = driver.switchTo().activeElement();
If no element is in focus, the active element would turn out to be the body
of the document being displayed, which might be the case when you launch a new page, for instance. When you invoke methods like click
, sendKeys
etc. you'll find the WebElement
returned by the above invocation will always represent the element in focus.
如果没有元素处于焦点,则活动元素将变成正在显示的文档的主体,例如,当您启动新页面时可能就是这种情况。当你调用click,sendKeys等方法时,你会发现上面调用返回的WebElement总是代表焦点元素。
This was tested using FirefoxDriver
, and I would suspect that the same would be true of other drivers, except for the HtmlUnitDriver
and similar drivers that do not use a full-fledged browser under the hood.
这是使用FirefoxDriver测试的,我怀疑其他驱动程序也是如此,除了HtmlUnitDriver以及不使用成熟浏览器的类似驱动程序。
#3
8
in python:
在python中:
element = driver.switch_to.active_element