import time
from selenium import webdriver
from selenium.webdriver import ActionChains, Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
option = webdriver.ChromeOptions()
option.add_experimental_option('detach', True)
driver = webdriver.Chrome(service_log_path="log.txt", executable_path="/Users/zhanghui04/Downloads/chromedriver",
chrome_options=option)
driver.get("http://www.baidu.com")
driver.maximize_window() # 窗口最大化
ele = driver.find_element(By.XPATH, '//*[@id="s-usersetting-top"]') # 查找元素
# ac = ActionChains(driver) # 实例化类
# ac.move_to_element(ele) # 鼠标移动
# ac.perform() # 执行鼠标操作
# ActionChains(driver).move_to_element(ele).perform()
ele.click()
# 获取元素属性
print(ele.get_attribute("name"))
print(ele.text)
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '//span[text()="高级搜索"]')))
driver.find_element(By.XPATH, '//span[text()="高级搜索"]').click()
'''
# Select类只能处理select元素
select = (By.XPATH, '//span[@id="adv-setting-gpc"]//select[@class="c-select-selection"]')
WebDriverWait(driver,10).until(EC.visibility_of_element_located(select))
ele_select = driver.find_element(*select)
s = Select(ele_select)
s.select_by_index(4)
s.select_by_value("最近一年")
s.select_by_visible_text("最近一天")
'''
handles = driver.window_handles
WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.XPATH, '//input[@id="adv_keyword"]')))
ele_input = driver.find_element(By.XPATH, '//input[@id="adv_keyword"]')
ele_input.send_keys("测试")
ele_input.send_keys(Keys.ENTER) # 键盘操作