Download presentation
Presentation is loading. Please wait.
1
Selenium HP Web Test Tool Training
Portnov Computer School Selenium HP Web Test Tool Training Test Automation For Web-Based Applications Presenter: Ellie Skobel
2
ActionChains and JavaScript
Day 10 ActionChains and JavaScript
3
Problem WebDriver object contains only basic keyboard and mouse action: click sendKeys clear Modern web UI often requires more advanced methods of interaction: drag and drop mouse over right click click and hold
4
Solution: Actions (Action Chains)
Actions are a way to automate low level interactions such as mouse movements, mouse button actions, key press, and context menu interactions. This is useful for doing more complex actions like hover over and drag and drop.
5
How It Works Import the ActionChains class
from selenium.webdriver import ActionChains Create a new instance of the ActionChains class by passing WebDriver object to the ActionChains() constructor ActionChains(driver)
6
How to Use ActionChains
Add on actions one at a time actions = ActionChains(driver) actions.move_to_element(menu) actions.click(hidden_submenu) actions.perform() Or add all actions at once ActionChains(driver) .move_to_element(menu) .click(hidden_submenu) .perform()
7
Problem WebDriver object does not provide a way to scroll the page
Modern websites often contains UI elements that change style / visibility / size / etc. in response to the currently viewable area of the page: Floating ads Back to the top links …
8
Solution: JavaScript WebDriver provides the execute_script() command
The command provides an ability to inject and execute javascript within the current page of the current window JavaScript can be used to scroll to the top/bottom of the page, or side to side
9
How It Works Declare a variable to hold an instance of the WebDriver
driver = webdriver.Firefox() Create a JavaScript snippet js_snipped = "console.log('Hello');" Execute your snipped at runtime using execute_script() method driver.execute_script(js_snipped)
10
Useful JS Tricks Scroll to the bottom of the page
Scroll by specific number of pixels Change element width/style/position/… Assign temporary ID Enable / disable elements
11
Scroll js_code = "window.scrollTo(0, window.scrollMaxY);” driver.execute_script(js_code) # OR element = driver.find_element_by_xpath( "//tr/td[8]/div[2]/input"); js_code = "arguments[0].scrollIntoView();" driver.execute_script(js_code, element) "window.scrollBy(0, {y});".format(y=50)
12
Assign a temporary ID element = driver.find_element_by_xpath( "//tr/td[8]/div[2]/input"); js_code = "arguments[0].setAttribute('id', 'new_unique_id')" driver.execute(js_code, element);
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.