In this blog article, you will learn how to execute JavaScript in Selenium using Python. As a software testing company, writing technical articles which are helpful for newbie automation testers gives us immense happiness.
Before using the snippets, please note that your scripts may fail unexpectedly due to cross-domain policies. Executing JavaScript in Selenium using Python should be your last resort since it does not mimic user actions.
Entering Value in Textbox
txtBox = driver.find_element_by_id("txt1") driver.execute_script("arguments[0].value=arguments[1]",txtBox,"codoid")
Get Value
txtBox = driver.find_element_by_id("txt1") print driver.execute_script("return arguments[0].value",txtBox)
Click Event
link = driver.find_element_by_link_text("Window1") driver.execute_script("arguments[0].click()",link)
Storing and Retrieving Global Variables
driver.execute_script("window.variable1=arguments[0]","codoid") print driver.execute_script("return window.variable1")
Scroll Page
driver.execute_script("window.scrollBy(0,150)")
Page Status
print driver.execute_script("document.readyState")
If the readystate returns ‘complete’, it confirms that the page and its resources have been loaded and parsed.
Comments(0)