python Automated Test selenium Executes js Script Implementation Example
- 2021-12-13 08:20:55
- OfStack
WebDriver has two methods to execute the Java Script script.
(1) Synchronous execution: execute_script
(2) Asynchronous execution: execute_async_script
from selenium import webdriver
from time import sleep
class TestScriptss(object):
def setup(self):
self.driver = webdriver.Chrome()
self.driver.get(http://www.baidu.com)
# Execute js Prompt box, and click OK
def test_scripts(self):
self.driver.execute_script("alert('test')")
sleep(2)
self.driver.switch_to.alert.accept()
self.driver.quit()
# Execute the script to get the current page title
def test_scripts(self):
js = "return document.title"
title = self.driver.execute_script(js)
print(title)
sleep(2)
self.driver.quit()
# Formatting a text box
def test_format(self):
js = 'var q = document.getElementById("kw"); q.style.border="10px solid red"'
self.driver.execute_script(js)
sleep(5)
self.driver.quit()
# Realize page scrolling through scripts
def test_scroll(self):
self.driver.find_element_by_id('kw').send_keys('selenium test')
self.driver.find_element_by_id('su').click()
sleep(2)
# Search results page scrolling
js = 'window.scrollTo(0, document.body.scrollHeight)'
# Synchronous execution
self.driver.execute_script(js)
# Asynchronous execution
# self.driver.execute_async_script(js)
sleep(2)
self.driver.quit()
The above comes from: Geek Time Course: selenium Automated Test Learning Summary.
The above is the python automated test selenium execution js script implementation example details, more information about the selenium test framework please pay attention to other related articles on this site!