selenium-JavaScript processing

JavaScript processing

In the automated process, encounters an element js processing, it is necessary to use js language elements operate, for example, slide to the bottom or the top of the browser, time control processing element is visible and invisible handle rich text, would require js language support, in selenium, a call to the js script is execute_script. below with examples to tell the story.

Element invisible operation

Js processing method is displayed to hide, as follows:
js =. "Document.getElementById ( 'Li'). Style.display = 'Block';"
driver.execute_script (js)

Browser sliding operation

In Baidu search as an example, operation of the sliding in the end portions, respectively, to the top of the slide operation, as follows:
from the webdriver Selenium Import
Import Time

= webdriver.Chrome Driver ()
driver.implicitly_wait (30)
driver.maximize_window ()
driver.get ( " http://www.baidu.com ")
driver.find_element_by_id ( "kW"). send_keys ( "Selenium")
Driver .find_element_by_id ( "SU"). the Click ()
the time.sleep (. 4)
'' 'sliding operation, the sliding portion in the end' ''
downjs = "var = document.documentElement.scrollTop Q = 10000"
driver.execute_script (downjs)
'' 'click next goes to the second page' ''
driver.find_element_by_partial_link_text ( "next") .click ()
'' 'performs a slide operation sliding the top' ''
upjs = "var = Q Document = 10000 .documentElement.scrollTop "
'first performs a sliding operation in the end portion, to the top of the slide' '''
driver.execute_script(downjs)
time.sleep(3)
driver.execute_script(upjs)
time.sleep(2)
driver.quit()

Processing time control

时间控件在测试过程中经常遇到,但大部分时间控件都是只读属性,要实现在只读属性的时间控件输入时间,可以先取消时间控件的只读属性,取消只读属性后,在Input输入框输入value即可,操作代码如下:

from selenium import webdriver
import time

= webdriver.Chrome Driver ()
DEF startJs (the startTime):
'' 'input time from the start time defined' ''
JS = "$ (" INPUT [placeholder = 'Start time> current time'] ") removerAttr (. 'readonly');. "" $ ( "input [placeholder = ' start time> current time'].") attr ( 'value', {0}) "the format (the startTime)
driver.execute_script (JS)

DEF endJs (endTime):
'' 'the end time to enter custom time' ''
JS = "$ (" INPUT [placeholder = 'end time> current time'] ") removerAttr ( 'readonly ');." "$ ( "input [placeholder = 'end time> current time']"). attr ( 'value', {0}). "the format (endTime)
driver.execute_script (JS)

startJs("2019-10-10 10:00:00")
time.sleep(3)
endJs("2019-10-10 10:02:00")

Guess you like

Origin www.cnblogs.com/qixc/p/11798369.html