1.设置元素等待
1.1显示等待
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get("http://www.baidu.com")
element = WebDriverWait(driver,5,0.5).until(
EC.visibility_of_element_located((By.ID,"kw"))
)
element.send_keys('selenium')
driver.quit()
from time import sleep,ctime
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("http://www.baidu.com")
print(ctime())
for i in range(10):
try:
el=driver.find_element_by_id("kw22")
if el.is_displayed():
break
except:
pass
sleep(1)
else:
print("time out")
print(ctime())
driver.quit()
1.2隐式等待
from time import ctime
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.get("http://www.baidu.com")
try:
print(ctime())
driver.find_element_by_id("kw22").send_keys('selenium')
except NoSuchElementException as e:
print(e)
finally:
print(ctime())
driver.quit()
2.定位一组元素
from time import sleep
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.baidu.cn")
driver.find_element_by_id("kw").send_keys("selenium")
driver.find_element_by_id("su").click()
sleep(2)
texts = driver.find_elements_by_xpath("//div[@tpl='se_com_default']/h3/a")
print(len(texts))
for t in texts:
print(t.text)
driver.quit()
3.多窗口切换
import time
from selenium import webdriver
driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.get("http://www.baidu.com")
search_windows=driver.current_window_handle
driver.find_element_by_link_text('登录').click()
driver.find_element_by_link_text("立即注册").click()
all_handles=driver.window_handles
for handle in all_handles:
if __name__ == '__main__':
if handle != search_windows:
driver.switch_to_window(handle)
print(driver.title)
driver.find_element_by_name("username").send_keys('username')
driver.find_element_by_name('phone').send_keys('138xxxxxxxx')
time.sleep(2)
driver.close()
driver.switch_to.window(search_windows)
print(driver.title)
driver.quit()
4.警告框处理
from time import sleep
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://www.baidu.com')
link=driver.find_element_by_link_text("设置").click()
driver.find_element_by_link_text("搜索设置").click()
sleep(2)
driver.find_element_by_class_name("prefpanelgo").click()
alert = driver.switch_to.alert
alert_text=alert.text
print(alert_text)
alert.accept()
driver.quit()
5.下拉框处理
from time import sleep
from selenium import webdriver
from selenium.webdriver.support.select import Select
driver = webdriver.Chrome()
driver.get('https://www.baidu.com')
link = driver.find_element_by_link_text('设置').click()
driver.find_element_by_link_text("搜索设置").click()
sel=driver.find_element_by_xpath("//select[@id='nr']")
Select(sel).select_by_value('20')
sleep(2)
Select(sel).select_by_visible_text("每页显示50条")
sleep(2)
Select(sel).select_by_index(0)
sleep(2)
driver.quit()
6.上传文件
import os
from selenium import webdriver
file_path = os.path.abspath('./files/')
driver = webdriver.Chrome()
upload_page = 'file:///'+file_path+'upfile.html'
driver.get(upload_page)
driver.find_element_by_id("file").send_keys(file_path + 'test.txt')
7.下载文件
import os
from selenium import webdriver
options = webdriver.ChromeOptions()
prefs = {'profile.default_content_settings.popups':0,
'download.default_directory':os.getcwd()}
options.add_experimental_option('prefs',prefs)
driver = webdriver.Chrome(chrome_options=options)
driver.get("https://pypi.org/project/selenium/#files")
driver.find_element_by_partial_link_text("selenium-3.141.0.tar.gz").click()
8.操作Cookie
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("http://www.baidu.com")
cookie = driver.get_cookies()
print(cookie)
driver.add_cookie({'name': 'key-aaaaaaaa','value': 'value-bbbbbbb'})
for cookie in driver.get_cookies():
print("%s -> %s" %(cookie['name'],cookie['value']))
|