js operation of the python + selenium

  1. Window scroll
= webdriver.Chrome Browser () 
URL = 'HTTPS: //www.baidu.com/'

browser.get (URL)

the try:
the wait = WebDriverWait (Browser, 10)
input_element = wait.until (EC.presence_of_element_located ((
By. CSS_SELECTOR, "# kw"
)))
# enter keyword
input_element.send_keys ( "lemon class")
# click on Baidu
wait.until (EC.presence_of_element_located ((
By.CSS_SELECTOR, "# su"
))). the click ()
# method a: first positioning element and then to specify the scroll bar to scroll

lemfix_ele = wait.until (EC.presence_of_element_located ((
By.XPATH, "a // [the contains (text (), 'www.lemfix.com/') ] "
)))
browser.execute_script ( 'arguments [0] .scrollIntoView ()',lemfix_ele) # The scroll bar to scroll to the element at the specified

the time.sleep (10)
# Method two:
  js1 = "window.scrollTo(0, document.body.scrollHeight)"#滑动滚动条到底部
  js2 = "window.scrollTo(0,0)"#滑动到顶部
  js3 = "window.scrollTo(0,200)"#向下移动200像素 js4 = "arguments[0].scrollIntoView();"#滑动滚动条到某个指定的元素
 browser.execute_script(js1) #执行js1,将滚动条滑到最下方

"""
#横向滚动条
browser.set_window_size(500,500) #缩小浏览器窗口,使之出现横向滚动条
js5 = "window.scrollTo(document.body.scrollWidth,0)"
js6 = "window.scrollTo(0,0)"
js7 = "window.scrollTo(200,0)"
browser.execute_script(js5)  #移动到最右边 time.sleep(2) browser.execute_script(js6) #移动到最左边 time.sleep(2) browser.execute_script(js7) #向右移动200像素

"""

 




except (TimeoutException,NoSuchElementException) as e:
print("出现异常:{}".format(e))

finally:
browser.quit()

And modify the attribute values ​​2.js

from selenium import webdriver
from time import sleep
from datetime import datetime, date, timedelta
driver=webdriver.Chrome()
driver.maximize_window()
driver.get('https://www.12306.cn/index/')
sleep(2)
#js去掉属性
js='document.getElementById("train_date").removeAttribute("readonly")'
driver.execute_script(js)
tommorow date.today = () + delta h (days = 1)

# Method one: send_keys () input value
# driver.find_element_by_id ( "train_date") Clear ().
. Send_keys (TOMMOROW) # driver.find_element_by_id ( "train_date")

# Second way: attribute modification value JS




= JS 'document.getElementById ( "train_date"). value = "{}"'. the format (TOMMOROW)
driver.execute_script (JS)
SLEEP (2)
driver.quit ()
increases modify the element properties

# Id increased title to property is nice and the elements of the value "test title"
JS = 'document.getElementById ( "nice"). SetAttribute ( "title", "Test title")'
# id is nice to delete the title element properties
JS = 'document.getElementById ( "nice"). removeAttribute ( "title")'
# id to obtain nice elements title attribute value
js = 'document.getElementById ( "nice" ). getAttribute ( "title")'
# id is modified elements nice title attribute value
js = 'document.getELementById ( "nice" ). title = " test"'

Import the webdriver Selenium from 
from SLEEP Time Import 
Driver = webdriver.Chrome () 
driver.maximize_window () 
driver.get ( 'https://www.baidu.com/') 
driver.find_element_by_link_text ( "Login") .click () 
SLEEP (1) 
# positioning "Register now" position, modify the target property is empty, so that the newly opened window displayed in the same link 
js = 'document.getElementsByClassName ( "pass- reglink pass-link") [0] .target = "" ' 
driver.execute_script (js) # execute js statement 
driver.find_element_by_link_text ( "Register Now") .click () 
SLEEP (3) 
driver.quit ()
 

Guess you like

Origin www.cnblogs.com/fyangq/p/12559119.html