selenium3 + python - js & jquery operation process

# Study recommended: HTTPS: //www.w3school.com.cn/js/index.asp 
#
# Jane book below to login & registration positioned elements, for example
"" "
JS positioning id name class_name xpath css element is

in addition to id is positioning the element is a single element of the object, the other elements are the object list is returned
1. acquired by the ID
js_id = 'document.getElementById ( "ID value");'
2 obtained by the CLASS
js_class = 'document.getElementsByClassName ( "class value") [0]; '
3 obtained by the name
js_name =' document.getElementsByName ( "name value") [0]; '
4. select element by the tag name
js_tag =' document.getElementsByTagName ( " tag value ") [0]; '
5. select element by CSS selector
js_css =' document.querySelectorAll (" CSS selector syntax ") [0];"
"" "

from selenium import webdriver
import time as t

driver = webdriver.Chrome()
driver.get('https://www.jianshu.com/sign_in')

#js id 定位注册
js_id = 'document.getElementById("js-sign-up-btn").click();'
driver.execute_script(js_id)
t.sleep(2)

#返回首页
driver.get('https://www.jianshu.com/sign_in')

#js CLASS 定位登录
js_class = 'document.getElementsByClassName("active")[0].click();'
driver.execute_script(js_class)
t.sleep(2)


#js Name 输入用户名
js_name = 'document.getElementsByName("session[email_or_mobile_number]")[0].value="username";'
driver.execute_script(js_name)
t.sleep(2)

#js tag 输入密码
js_tag = 'document.getElementsByTagName("input")[3].value="123456";'
driver.execute_script(js_tag)
t.sleep(2)




#js Css click Sign
js_css = 'document.querySelectorAll ( "Sign-in-Button.") [0] .click ();'
driver.execute_script (js_css)
t.sleep (2)



driver.quit()

"" " 
Jquery operation processing
jquery syntax
jq_id = '" $ ( "id ) vla ( value).' - enter the text
jq_id = '$ (" id " ) click ().' - click
jq process id, type , tag-level positioning
"" "

from the Selenium webdriver Import
Import AS Time t

Driver = webdriver.Chrome ()
# simple book site
driver.get ( 'https://www.jianshu.com/sign_in')

# # represents positioned according to id ID
jq_id = '$ ( "# session_email_or_mobile_number") Val ( "username").'
driver.execute_script (jq_id)
t.sleep (2)

# The positioning type attribute type targeting: property value password
jq_type = '$ ( ": password "). Val (" 123 ") '
driver.execute_script (jq_type)
t.sleep (. 1)


# Hierarchical positioning tick Remember me
# 1 with the label type positioning.
Jq_remember = '$ ( "Remember-btn> the INPUT:. The CheckBox") Val ( "123456").'
Driver.execute_script (jq_remember)
t.sleep (2)
. untagged # 2
# jq_remember = '$ ( "BTN-Remember>:. CheckBox") the Click ()).'
# driver.execute_script (jq_remember)
# t.sleep (2)
#. 3 without. level symbol
# jq_remember = '$. ( "BTN-Remember:. CheckBox") the Click ())'
# driver.execute_script (jq_remember)
# t.sleep (2)
. #. 4 to select the last tag (input) element
# = jq_remember '$ ( "BTN-Remember> INPUT:. Last") the Click ()).'
# driver.execute_script (jq_remember)
# t.sleep (2)


Positioning Positioning #class login button
jq_remember = '$ (. "Sign-in-Button"). the Click ()'
driver.execute_script (jq_remember)
t.sleep (2)

driver.quit ()

Guess you like

Origin www.cnblogs.com/Teachertao/p/11707227.html