Is there a way to perform a mouseover (hover over an element) using Selenium and Python bindings?

前端 未结 2 2071
误落风尘
误落风尘 2020-12-02 09:22

Reading here, there apparently used to be a RenderedWebElement class with a hover method. It, however, was exclusively made for Java (I have search

相关标签:
2条回答
  • 2020-12-02 09:45

    @AutomatedTester have given the community a great solution!

    Below is how I used it.

    I used signal to properly quit PhantomJS since it sometimes hangs in the current process.

    I prefer to use find_element_by_xpath since xpath can be easily found in chrome.

    Here's how:

    Right click -> Inspect -> Right click -> Copy -> CopyXpath

    from selenium import webdriver
    from selenium.webdriver.common.action_chains import ActionChains
    import signal
    
    browser = webdriver.PhantomJS()
    browser.implicitly_wait(3)
    
    def hover(browser, xpath):
        element_to_hover_over = browser.find_element_by_xpath(xpath)
        hover = ActionChains(browser).move_to_element(element_to_hover_over)
        hover.perform()
    
    
    
    browser.service.process.send_signal(signal.SIGTERM)  # kill the specific phantomjs child proc
    browser.quit()
    
    0 讨论(0)
  • 2020-12-02 09:56

    To do a hover you need to use the move_to_element method.

    Here is an example

    from selenium import webdriver
    from selenium.webdriver.common.action_chains import ActionChains
    
    firefox = webdriver.Firefox()
    firefox.get('http://foo.bar')
    element_to_hover_over = firefox.find_element_by_id("baz")
    
    hover = ActionChains(firefox).move_to_element(element_to_hover_over)
    hover.perform()
    
    0 讨论(0)
提交回复
热议问题