In Selenium how do I find the “Current” object

前端 未结 4 999
余生分开走
余生分开走 2020-12-18 19:15

I would like for Selenium to navigate a menu via arrow keys--Starting with clicking the top menu item then pressing \"DOWN\", \"DOWN\", ...

The problem is that you h

相关标签:
4条回答
  • 2020-12-18 19:41

    in python:

    element = driver.switch_to.active_element
    
    0 讨论(0)
  • 2020-12-18 19:43

    In Ruby/Capybara:

    page.driver.browser.switch_to.active_element
    

    Note that this returns a Selenium::WebDriver::Element not a Capybara::Node::Element.

    0 讨论(0)
  • 2020-12-18 19:46

    Don't know of a more straightforward way than accessing document.activeElement

    How do I test which element has the focus in Selenium RC?

    0 讨论(0)
  • 2020-12-18 19:47

    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:

    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.

    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.

    0 讨论(0)
提交回复
热议问题