Background:

JavaScript is a scripting language that runs on both the client (browser) and server side, allowing static web pages to be transformed into interactive web pages. You can use Python Selenium WebDriver to execute JavaScript statements and interact with JS in Web pages. Selenium should be able to do most of what JS can do. WebDriver simulates end-user interactions, so invisible elements cannot be clicked, and sometimes visible elements cannot be clicked either. In these cases, we can click or execute page elements by executing JavaScript via WebDriver.

1. Execute execute_script() simultaneously

It is a synchronous method, using it to execute JS code will block the main thread until the js code is finished executing. Using execute_script() to execute JavaScript code, there are two ways to implement element manipulation:

Method 1: Document-level operations
  • Direct use of JavaScript to achieve element positioning and action execution, the main methods are:
document.getElementById
document.getElementsByClassName
document.getElementsByName
document.getElementsByTagName
document.getElementsByTagNameNS
Copy the code
Method 2: Element-level manipulation
  • Get the return value
Self.driver.execute_script ("return document.getelementByid ('kw').value") js = 'return ' Document.title 'self.driver.execute_script(js) # print(title)Copy the code
  • Sliding operation
# slide to the bottom of the browser document. DocumentElement. ScrollTop = 10000 window. ScrollTo (0, Document. Body. ScrollHeight) # slide to the top document browser. The documentElement. ScrollTop = 0 window.scrollTo(document.body.scrollHeight,0)Copy the code
  • Changing element attributes

Most of the time controls are readonly properties and need to manually select the corresponding time. In automated tests, you can disable the readonly attribute using JavaScript code

Def test_datetTime (self) def test_datetTime (self): The self. The driver. The get cancel the readonly attribute # (" https://www.12306.cn/index/ ") self.driver.execute_script("dat=document.getElementById('train_date'); dat.removeAttribute('readonly')") self.driver.execute_script("document.getElementById('train_date').value='2020-10-01'")  time.sleep(3) now_time = self.driver.execute_script("return document.getElementById('train_date').value") assert '2020-10-01' == now_time sleep(2) self.driver.quit()Copy the code

2. Execute execute_async_script() asynchronously

An asynchronous method that does not block main thread execution

Example Code:

from selenium import webdriver from time import sleep class TestCase(object): def __init__(self): self.driver = webdriver.Chrome() self.driver.maximize_window() self.driver.get('http://www.baidu.com') def test1(self): Self.driver.execute_script ("alert('test')") sleep(2) self.driver.switch_to.alert.accept() def test2(self): # return TAB title js = 'return document.title' title = self.driver.execute_script(js) print(title) def test3(self): Js = 'var q = document.getelementbyId ("kw"); q.style.border=2px solid' self.driver.execute_script(js) sleep(2) def test4(self): Self.driver.find_element_by_id ('kw').send_keys('Github') self.driver.find_element_by_id('su').click() sleep(4) # scroll to the bottom Js2 = 'window.scrollTo(0, 0) ' document.body.scrollHeight)' self.driver.execute_script(js1) sleep(2) self.driver.execute_script(js2) sleep(2) self.driver.quit() def test_datettime(self): The self. The driver. The get cancel the readonly attribute # (" https://www.12306.cn/index/ ") self.driver.execute_script("dat=document.getElementById('train_date'); dat.removeAttribute('readonly')") self.driver.execute_script("document.getElementById('train_date').value='2020-10-01'")  time.sleep(3) now_time = self.driver.execute_script("return document.getElementById('train_date').value") assert '2020-10-01' == now_time sleep(2) self.driver.quit() if __name__ == '__main__': case = TestCase() # case.test1() # case.test2() # case.test3() # case.test4() case.test_datettime()Copy the code