{"flag":true,"single":true,"pageTitle":"ActionChains in python , Click,  Click and hold, Right Click, Double Click, Drag and drop, Key Down & hold","post":{"id":95,"user_id":"1","slug":"actionchains-in-python-click-click-and-hold-right-click-double-click-drag-and-drop-key-down-hold-0ofa","title":"ActionChains in python , Click,  Click and hold, Right Click, Double Click, Drag and drop, Key Down & hold","body":"<p>Its do complex actions like<\/p>\r\n<ul>\r\n<li>hover over<\/li>\r\n<li>drag and drop<\/li>\r\n<li>File Manager control<\/li>\r\n<li>Control + f to open search bar etc<\/li>\r\n<\/ul>\r\n<p><strong>Syntax:<\/strong><\/p>\r\n<p>Create obj, use actions, call perform method<\/p>\r\n<pre class=\"language-markup\"><code>from selenium import webdriver # import webdriver\r\nfrom selenium.webdriver.common.action_chains import ActionChains # import Action chains \r\ndriver = webdriver.Chrome() # create webdriver object\r\n\r\naction = ActionChains(driver) #1. create action chain object \r\nactions.key_down(Keys.TAB).key_up(Keys.TAB).perform() #SEND KEYS AND CALL PERFORM method<\/code><\/pre>\r\n<p><strong>EXAMPLES:<\/strong><\/p>\r\n<p><strong>1. click on hidden menu<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>menu = driver.find_element_by_css_selector(\".nav\")\r\nhidden_submenu = driver.find_element_by_css_selector(\".nav # submenu1\")\r\nActionChains(driver).move_to_element(menu).click(hidden_submenu).perform()<\/code><\/pre>\r\n<p><strong>2.&nbsp; Click<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>element = driver.find_element_by_link_text(\"PPSC MCQS\")\r\naction = ActionChains(driver)\r\naction.click(on_element = element)\r\naction.perform()<\/code><\/pre>\r\n<p>3. <strong>click_and_hold<\/strong> method is used to hold down the left mouse button on an element.<\/p>\r\n<pre class=\"language-markup\"><code>ActionChains(driver).click_and_hold(on_element = element).perform()<\/code><\/pre>\r\n<p><strong>4. Right click<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>ActionChains(driver).context_click(on_element=None).perform()<\/code><\/pre>\r\n<p><strong>5. Double Click<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>ActionChains(driver).double_click(on_element = element).perform()<\/code><\/pre>\r\n<p><strong>6.Drag and Drop<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>source_element = driver.find_element_by_link_text(\"Courses\")\r\ntarget_element = driver.find_element_by_link_text(\"Hard\")\r\n\r\nActionChains(driver).drag_and_drop(source_element, target_element).perform()<\/code><\/pre>\r\n<p><strong>7. Kye Down and not release it.&nbsp; key_up for <\/strong><strong>Releases a modifier key.<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>ActionChains(driver).key_down(Keys.CONTROL).send_keys('F').key_up(Keys.CONTROL).perform()<\/code><\/pre>\r\n<p><strong>8. Move Mouse current position<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>ActionChains(driver).move_by_offset(200, 200).perform()<\/code><\/pre>\r\n<p><strong>9. move the mouse to the middle of an element<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>ActionChains(driver).move_to_element(element).perform()<\/code><\/pre>\r\n<p><strong>10.&nbsp; Pause all inputs for the specified duration in seconds<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>ActionChains(driver).pause(60).perform() #pause all inputs for 1 minute<\/code><\/pre>\r\n<p><strong>11. Releasing a held mouse button on an element.<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>ActionChains(driver).release(on_element = element).perform()<\/code><\/pre>\r\n<p><strong>12. Clears actions that are already stored locally and on the remote end<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>ActionChains(driver).reset_actions().perform()<\/code><\/pre>\r\n<p><strong>13. <\/strong><strong>send keys to current focused element.<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>element = driver.find_element_by_class_name(\"gsc-input\") # get element   \r\naction = ActionChains(driver) # create action chain object  \r\naction.click(on_element = element) # click the item \r\naction.send_keys(\"Hello\") # send keys  \r\naction.perform() # perform the operation\r\n\r\nOR\r\nactions = ActionChains(bot)\r\nactions.key_down(Keys.TAB).key_up(Keys.TAB).perform()<\/code><\/pre>\r\n<p><strong>14. Scroll to element&nbsp;<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>from selenium.webdriver.common.action_chains import ActionChains\r\nactions = ActionChains(driver)\r\nactions.move_to_element(element).perform()<\/code><\/pre>\r\n<p>&nbsp;<\/p>\r\n<p><strong>perform method is used to perform all stored operations in action instance of ActionChains class.<\/strong><\/p>\r\n<p>&nbsp;<\/p>\r\n<p>&nbsp;<\/p>","category_id":"19","is_private":"0","created_at":"2023-04-02T11:19:00.000000Z","updated_at":"2023-12-30T09:17:54.000000Z","category":{"id":19,"user_id":"1","name":"Python Selenium","slug":"python-selenium-vbxw","parent_id":"5","created_at":"2023-04-02T11:11:46.000000Z","updated_at":"2023-04-02T11:11:46.000000Z"},"user":{"id":1,"name":"R GONDAL","email":"rizikmw@gmail.com","email_verified_at":null,"two_factor_confirmed_at":null,"current_team_id":"1","profile_photo_path":null,"created_at":"2023-03-12T10:49:33.000000Z","updated_at":"2025-01-10T12:59:00.000000Z","profile_photo_url":"https:\/\/ui-avatars.com\/api\/?name=R+G&color=7F9CF5&background=EBF4FF"}},"pageDesc":"Its do complex actions like  hover over drag and drop File Manager control Control + f to open search bar etc  Syntax: Create obj, use actio - ActionChains in python , Click,  Click and hold, Right Click, Double Click, Drag and drop, Key Down & hold (Updated: December 30, 2023) - Read more about ActionChains in python , Click,  Click and hold, Right Click, Double Click, Drag and drop, Key Down & hold at my programming site [SITE]","categories":[]}