from selenium import webdriver
# 显性等待 导入库
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
# 开始会话
driver = webdriver.Chrome()
# 隐性等待 全局有效,从打开到关闭
# driver.implicitly_wait(30)
# 访问一个地址
driver.get("http://www.baidu.com")
# 全屏
driver.maximize_window() # 全屏
#driver.set_window_size(1000,800) # 键入宽高
# 后退
driver.back()
# 前进
driver.forward()
# 刷新
driver.refresh()
# 获取窗口标题
print(driver.title)
# 获取当前窗口的URL
print(driver.current_url)
# 获取当前窗口的句柄
print(driver.current_window_handle)
driver.find_element_by_id("kw").send_keys("618活动")
# driver.find_element_by_id("su").click()
driver.find_element_by_xpath('//img[@class="index-logo-srcnew" and @title="到百度首页"]').click()
# 获取属性值
value = driver.find_element_by_id("su").get_attribute("value")
print(value)
# 获取文本内容
text = driver.find_element_by_xpath('//a[@name="tj_trnews"]').text
print(text)
# time.sleep(2)
# 点击登录的链接
driver.find_element_by_xpath('//div[@id="u1"]/a[@name="tj_login"]').click()
# time.sleep(2)
# 点击用户名密码登录的方式
WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.ID,'TANGRAM__PSP_10__footerULoginBtn'))) # 显性等待
driver.find_element_by_id("TANGRAM__PSP_10__footerULoginBtn").click()
# time.sleep(1)
WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.ID,'TANGRAM__PSP_10__userName'))) # 显性等待
driver.find_element_by_id('TANGRAM__PSP_10__userName').send_keys('1893285458')
# 关闭浏览器当前正在使用的窗口
driver.close()
# 关闭整个浏览器会话
driver.quit()
元素四大操作:click(),send_keys(),get_attribute(),text