Collections:
execute_script() to Run JavaScript in Java
How to run JavaScript code on WebDriver with execute_script() method in Python?
✍: FYIcenter.com
If you want run some client side JavaScript code in the same way
as JavaScript code embedded in the Web page source code,
you can use the execute_script() method as describe below:
1. Load the Web page to a WebDriver object like (driver.get(url);).
3. Execute any JavaScript code string by calling the execute_script() or execute_async_script() method on the WebDriver object.
Here is an example program that uses execute_script() to change the background color of the loaded Web page.
# JavaScriptTest.py # Copyright (c) FYIcenter.com from selenium.webdriver import Chrome import time driver = Chrome() driver.get("http://sqa.fyicenter.com") time.sleep(3) script = "document.body.style.backgroundColor=\"#ffeeee\";" driver.execute_script(script) time.sleep(60) driver.quit()
Run the program, you will see that the background color of the Web page changes to pink in 3 seconds.
C:\fyicenter> python JavaScriptTest.py
You can also try some other JavaScript code examples list below:
String script = "alert(\"Hello world!\")"; String script = "confirm(\"Hello world!\")";
Â
⇒ Dump Elements with find_elements_by_xpath() in Python
⇠Submit Web Form with WebDriver in Python
⇑ Using Selenium WebDriver Client Python API
⇑⇑ Selenium Tutorials
2022-09-30, 1827👍, 1💬
Popular Posts:
Where to find tutorials on UFT (Unified Functional Testing) tool? I want to know how to use UFT. Her...
How to generate IP Addresses? To help you to obtain some IP addresses for testing purpose, FYIcenter...
How to see my IP address Host Name? To help you to see your IP Address Host Name, FYIcenter.com has ...
How to set Content-Type to application/json? The server requires the Content-Type to be application/...
How to valid IPv6 addresses? In order to help your programming or testing tasks, FYIcenter.com has d...