import datetime
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver =webdriver.Chrome()
driver.maximize_window()
driver.get("http://www.baidu.com")
driver.find_element(By.XPATH,'//input[@id="kw"]').send_keys("python")
driver.find_element(By.XPATH,'//input[@id="su"]').click()
#js函数
#日期框操作
#通过js语句修改属性值,利用document对象
js_date ="""
var a=document.getElementById("train_date")
a.readOnly=false
a.value="2021-08-23"
"""
#由于dom中没有xpath定位,则需要从外部通过传参的方式获取,例如:
date_js ="""
var a =arguments[0];
a.readOnly=false;
a.value=arguments[1];
"""
loc =(By.XPATH,'//input[@id="train_date"]')
ele =driver.find_element(*loc) #通过xpath或其它方式定位元素,相当于document.getElementById("train_date"),然后利用arguments[0]来接收这个参数
t =(datetime.datetime.now()+datetime.timedelta(days=+10)).strftime("%Y-%m-%d")
driver.execute_script(date_js,ele,t) #arguments[0]接收ele参数,arguments[1]接收t参数