Selenium executes JavaScript statements

Time:2021-10-28

Selenium provides two ways to execute JavaScript scripts:

execute_script: synchronous execution——Commonly used

execute_async_script: asynchronous execution

Application scenario 1: time input box

Some time input box controls have been addedreadonlyProperty to restrict the control from being read-only and not writable, so it cannot be passed directlysend_keys()Enter the content.

Handling methods and steps for this situation:

  1. Remove via JavaScript scriptreadonlyattribute
  2. Clear the contents of the input box
  3. Enter information in time format

Selenium executes JavaScript statements

from selenium import webdriver
from time import sleep

Driver = webdriver. Chrome() # open browser
driver.get(" https://www.12306.cn/index/ ") # jump to test page

sleep(1)
JS = "document. Getelementbyid ('train_date '). Removeattribute ('readonly')" # write a JavaScript statement to remove the readonly attribute
driver.execute_ Script (JS) # synchronous execution of JavaScript statements
element = driver.find_ element_ by_ ID ("train_date") # positioning element
Element. Clear() # clear contents
sleep(1)
element.send_ Keys ("2020-08-10") # input content
sleep(2)

Driver. Quit() # close browser

Application scenario 2: operating the scroll bar

JavaScript statements commonly used to operate scroll bars:

Serial number JavaScript statement describe
1 document.documentElement.scrollTop=1000 Move to 1000 from top
2 window.scrollTo(0, document.body.scrollHeight*0.5) Move to absolute position in proportion to height (x-axis direction, Y-axis direction)
3 window.scrollTo(0, 1000) Move to absolute coordinate position (x-axis direction, Y-axis direction)
4 window.scrollBy(0, -200) Move the corresponding distance relative to the current coordinate (x-axis direction, Y-axis direction)
from selenium import webdriver
from time import sleep

Driver = webdriver. Chrome() # open browser
driver.get(" https://www.douban.com/ ") # jump to test page

sleep(1)
Js1 = "document. Documentelement. Scrolltop = 500" # move the y-axis direction to a position 500 away from the top
JS2 = "window. Scrollto (0, document. Body. Scrollheight * 0.5)" # move the y-axis direction to the position where the coordinate is 50% of the total height
Js3 = "window.scrollto (0, 1000)" # Y-axis direction moves to the position with height of 1000
Js4 = "window.scrollby (0, - 500)" # move 500 upward relative to the current position

driver.execute_ Script (js1) # executes JavaScript statements
sleep(3)
driver.execute_ Script (JS2) # executes JavaScript statements
sleep(3)
driver.execute_ Script (js3) # executes JavaScript statements
sleep(3)
driver.execute_ Script (js4) # executes JavaScript statements

sleep(3)
Driver. Quit() # close browser

summary

Selenium executes JavaScript statements

Recommended Today

Swift advanced (XV) extension

The extension in swift is somewhat similar to the category in OC Extension can beenumeration、structural morphology、class、agreementAdd new features□ you can add methods, calculation attributes, subscripts, (convenient) initializers, nested types, protocols, etc What extensions can’t do:□ original functions cannot be overwritten□ you cannot add storage attributes or add attribute observers to existing attributes□ cannot add parent […]