Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Selenium how to move mouse on element. which shows drop-down menu

link http://www.babylegs.com

My code:

class TestClassMy(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()


    def test1(self):
        driver = self.driver
        driver.get('http://www.babylegs.com')
        driver.maximize_window()
        element_to_select = driver.find_element_by_xpath(".//*[@id='nav']/ol/li[5]/a") #d.send_keys(Keys.NULL)

        actions = ActionChains(driver)
        element_to_select.click_and_hold(element_to_select).perform()

    def tearDown(self):
        self.driver.close()

if __name__ == '__main__':
    unittest.main()
like image 821
ChantOfSpirit Avatar asked Sep 19 '25 03:09

ChantOfSpirit


1 Answers

Assuming your browser is Firefox, the Python code would look like this:

driver = webdriver.Firefox(executable_path=driver_path)
action = webdriver.ActionChains(driver)
element = driver.find_element_by_id('your-id') # or your another selector here
action.move_to_element(element)
action.perform()

If you have already moved your cursor to an element and want to re-position it relatively, you can use:

action.move_by_offset(10, 20)    # 10px to the right, 20px to bottom
action.perform()

or even shorter:

action.move_by_offset(10, 20).perform()

More documentation is here: https://selenium-python.readthedocs.io/api.html

like image 187
Eugene Chabanov Avatar answered Sep 20 '25 18:09

Eugene Chabanov