元素定位
from selenium import webdriver
import time
browser = webdriver.Chrome()
browser.get('file:///F:/%E4%BA%BA%E5%B7%A5%E6%99%BA%E8%83%BD/p9/pagetest/%E6%B3%A8%E5%86%8CA.html')
browser.find_element_by_css_selector(".zc #userA").send_keys('admin')
time.sleep(2)
browser.quit()
元素操作
from selenium import webdriver
import time
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("file:///F:/%E4%BA%BA%E5%B7%A5%E6%99%BA%E8%83%BD/p9/pagetest/%E6%B3%A8%E5%86%8CA.html")
'''
获取元素信息:size,text,get_attribute
判断元素信息:id_displayed,is_enables,is_selected
'''
print("判断元素是否可见,默认应该是False:",driver.find_element(By.NAME,'sp1').is_displayed())
print("判断取消按钮是否可用,默认应该是False:",driver.find_element(By.ID,"cancelA").is_enabled())
print("判断取消按钮是否可用,默认是True:",driver.find_element(By.TAG_NAME,'button').is_enabled())
print("判断旅游复选框是否选中,默认应该是True:",driver.find_element(By.ID,"lyA").is_selected())
print("判断旅游复选框是否选中,默认应该是False:",driver.find_element(By.ID,'gwA').is_enabled())
driver.quit()
键鼠操作
from selenium import webdriver
import time
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.get("file:///F:/%E4%BA%BA%E5%B7%A5%E6%99%BA%E8%83%BD/p9/pagetest/%E6%B3%A8%E5%86%8CA.html")
'''
鼠标操作的步骤:
1.生成鼠标对象:ActionChains
2.做对应的操作:
3.执行响应的操作:perform
'''
action = ActionChains(driver)
'''
键盘操作:
1.send_keys(Keys.BACK_SPACE) 删除(BackSpace)
2.send_keys(Keys.SPACE) 空格(Space)
3.send_keys(Keys.TAB) 制表(TAB)
4.send_keys(Keys.ESCAPE) 回退(ESCAPE)
5.send_keys(Keys.ENTER) 回车(ENTER)
6.send_keys(Keys.CONTROL,'a') 全选(ctrl+a)
7.send_keys(Keys.CONTROL,'c') 全选(ctrl+c)
8.send_keys(Keys.CONTROL,'v') 全选(ctrl+v)
'''
element = driver.find_element(By.ID,'userA')
element.send_keys("admin1")
time.sleep(2)
element.send_keys(Keys.BACK_SPACE)
element.send_keys(Keys.CONTROL,"a")
time.sleep(2)
b = element.send_keys(Keys.CONTROL,'c')
driver.find_element(By.ID,"passwordA").send_keys(Keys.CONTROL,'v')
time.sleep(2)
driver.quit()
下拉框
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("file:///F:/%E4%BA%BA%E5%B7%A5%E6%99%BA%E8%83%BD/p9/pagetest/%E6%B3%A8%E5%86%8CA.html")
element = driver.find_element(By.CSS_SELECTOR,"#selectA")
select = Select(element)
time.sleep(2)
select.select_by_index(2)
time.sleep(2)
select.select_by_value("sh")
time.sleep(2)
select.select_by_value("深圳")
time.sleep(3)
driver.quit()
弹出框
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.maximize_window()
'''
alert.text 获取弹出框提示信息
alert.accept()确定弹出框
alert.dismiss()取消弹出框
'''
driver.get("file:///F:/%E4%BA%BA%E5%B7%A5%E6%99%BA%E8%83%BD/p9/pagetest/%E6%B3%A8%E5%86%8CA.html")
driver.find_element(By.ID, 'alerta').click()
time.sleep(3)
alert = driver.switch_to.alert
print(alert.text)
alert.dismiss()
driver.find_element(By.ID, 'userA').send_keys('admin')
time.sleep(3)
driver.quit()
弹出框
import time
from selenium import webdriver
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("file:///F:/%E4%BA%BA%E5%B7%A5%E6%99%BA%E8%83%BD/p9/pagetest/%E6%B3%A8%E5%86%8CA.html")
time.sleep(3)
js = "window.scrollTo(0,2000)"
driver.execute_script(js)
time.sleep(3)
driver.quit()
窗口操作
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.maximize_window()
'''
获取当前窗口句柄:driver.current_window_handle
获取所有窗口句柄:driver.window_handles
切换窗口句柄:driver.switch_to.window(window_handle)
窗口句柄:由操作系统组成的一串唯一的识别码,是一串字符
'''
driver.implicitly_wait(10)
driver.get("file:///F:/%E4%BA%BA%E5%B7%A5%E6%99%BA%E8%83%BD/p9/web%E8%87%AA%E5%8A%A8%E5%8C%96/pagetest/%E6%B3%A8%E5%86%8C%E5%AE%9E%E4%BE%8B.html")
time.sleep(1)
driver.find_element(By.ID,"idfr").click()
time.sleep(1)
print(driver.current_window_handle)
windows = driver.window_handles
driver.switch_to.window(windows[-1])
driver.find_element(By.ID,'userA').send_keys('admin')
time.sleep(2)
driver.quit()
'''
目的:当程序运行出错时,通过截图文件能够更直观的知道具体是那个操作步骤出现了找错误
窗口截图就是吧出错的图片保存到指定位置
截图方法:driver.get_screenshot_as_file(filename)
截图的文件名必须是以png结尾
filename中的文件目录必须手动创建
'''
小案例
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.support.wait import WebDriverWait
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://www.szlcsc.com/")
driver.find_element(By.XPATH, "//div[@class='reg']/a[2]").click()
driver.find_element(By.XPATH, "//div[@class='login_beij_top']/div[2]/span").click()
driver.find_element(By.XPATH, "//input[@placeholder='客户编号/手机号码/邮箱']").send_keys('15040215866')
driver.find_element(By.XPATH, "//input[@placeholder='请输入密码']").send_keys('qwe55555aaa')
driver.find_element(By.XPATH, "//div[@class='x_land2']/input[@type='button']").click()
driver.find_element(By.XPATH, "//input[@class='sch-bd02']").send_keys('二极管')
driver.find_element(By.XPATH, "//input[@class='sch-bd03']").click()
time.sleep(2)
know_btn = driver.find_element(By.XPATH,"//button[@class='ok']")
if know_btn:
know_btn.click()
else:
pass
driver.execute_script("window.scrollTo(0,2000)")
time.sleep(2)
driver.execute_script("window.scrollTo(2000,0)")
time.sleep(2)
driver.find_element(By.XPATH, "//table[1]/tbody/tr/td/ul/li/button[1]").click()
time.sleep(2)
driver.find_element(By.XPATH, "//span[@class='cart-icon-a']/span[@class='cart-icon']").click()
time.sleep(2)
new_btn = driver.find_element(By.XPATH,"//div[@class='guide-box-1']/button")
if new_btn:
know_btn.click()
time.sleep(2)
else:
pass
new_btn2 = driver.find_element(By.XPATH,"//div[@class='guide-box-2']/button")
if new_btn2:
new_btn2.click()
time.sleep(2)
else:
pass
img_name = '../img/' + str(time.time()) + '.png'
driver.get_screenshot_as_file(img_name)
time.sleep(2)
driver.quit()