1、文本框、密码框、输入、点击、 按钮
网址:http://cms.duoceshi.cn/manage/login.do
from selenium import webdriver
from time import *
dx=webdriver.Chrome()
dx.get("http://cms.duoceshi.cn/manage/login.do")
dx.maximize_window()
sleep(2)
dx.find_element_by_id("userAccount").send_keys("admin")
sleep(2)
dx.find_element_by_id("loginPwd").send_keys("123456")
sleep(2)
dx.find_element_by_id("loginBtn").click()
2、链接
网址:https://www.baidu.com/
from selenium import webdriver
from time import *
dx=webdriver.Chrome()
dx.get("http://baidu.com")
dx.maximize_window()
sleep(2)
dx.find_element_by_link_text("hao123").click()
3、隐藏框
from selenium import webdriver
from time import *
from selenium.webdriver.common.action_chains import ActionChains
dx=webdriver.Chrome()
dx.get("http://baidu.com")
dx.maximize_window()
sleep(2)
wz=dx.find_element_by_link_text("更多")
ActionChains(dx).move_to_element(wz).perform()
dx.find_element_by_xpath('//*[@id="s-top-more"]/div[2]/a/div').click()
更多案例:
from time import *
from selenium.webdriver.common.action_chains import ActionChains
dx=webdriver.Chrome()
dx.get("http://baidu.com")
dx.maximize_window()
sleep(2)
wz=dx.find_element_by_id("s-usersetting-top")
ActionChains(dx).move_to_element(wz).perform()
dx.find_element_by_link_text("关闭预测").click()
4、获取元素的文本(text)
from selenium import webdriver
from time import *
from selenium.webdriver.common.action_chains import ActionChains
dx=webdriver.Chrome()
dx.get("http://baidu.com")
dx.maximize_window()
sleep(2)
wb=dx.find_element_by_link_text("登录").text
if wb=="登录":
print("断言成功")
else:
print("断言失败")
5、断言(if 、assert)
(1)if断言:
from selenium import webdriver
from time import *
from selenium.webdriver.common.action_chains import ActionChains
dx=webdriver.Chrome()
dx.get("http://baidu.com")
dx.maximize_window()
sleep(2)
wb=dx.find_element_by_link_text("登录").text
if wb=="登录":
print("断言成功")
else:
print("断言失败")
(2)assert 断言
from selenium import webdriver
from time import *
from selenium.webdriver.common.action_chains import ActionChains
dx=webdriver.Chrome()
dx.get("http://baidu.com")
dx.maximize_window()
sleep(2)
wb=dx.find_element_by_link_text("登录").text
assert wb=="登录qq"
print(1)
常用的场景:日志
6、获取窗口的大小(get_window_size())
from selenium import webdriver
from time import *
from selenium.webdriver.common.action_chains import ActionChains
dx=webdriver.Chrome()
dx.get("http://baidu.com")
dx.maximize_window()
sleep(2)
m=dx.get_window_size()
print(m)
一、切换窗口
(1) 获取当前句柄(current_window_handle)
获取所有的句柄 (window_handles)
from selenium import webdriver
from time import *
from selenium.webdriver.common.action_chains import ActionChains
dx=webdriver.Chrome()
dx.get("http://baidu.com")
print(dx.title)
dx.maximize_window()
sleep(2)
w='window.open("http://jd.com")'
dx.execute_script(w)
print(dx.title)
jb=dx.current_window_handle
print(jb)
jbs=dx.window_handles
print(jbs)
(2)通过句柄切换窗口(两个窗口的排序0,1)
from selenium import webdriver
from time import *
from selenium.webdriver.common.action_chains import ActionChains
dx=webdriver.Chrome()
dx.get("https://www.baidu.com/")
print(dx.title)
dx.maximize_window()
sleep(2)
w='window.open("https://www.jd.com/")'
dx.execute_script(w)
print(dx.title)
jb=dx.current_window_handle #获取当前窗口句柄
print(jb) #CDwindow-279E9F0F55B7B2F2AFA9245B9A580D86
jbs=dx.window_handles #获取所有窗口句柄
print(jbs)
dx.switch_to.window(jbs[1])
sleep(2)
dx.find_element_by_link_text("我的订单").click()
(2)通过句柄切换窗口(三个窗口的排序0,2,1)
from selenium import webdriver
from time import *
from selenium.webdriver.common.action_chains import ActionChains
dx=webdriver.Chrome()
dx.get("https://www.baidu.com/")
print(dx.title)
dx.maximize_window()
sleep(2)
w='window.open("https://www.jd.com/")'
dx.execute_script(w)
print(dx.title)
w1='window.open("https://www.hao.com/")'
dx.execute_script(w1)
jb=dx.current_window_handle #获取当前窗口句柄
print(jb) #CDwindow-279E9F0F55B7B2F2AFA9245B9A580D86
jbs=dx.window_handles #获取所有窗口句柄
print(jbs)
dx.switch_to.window(jbs[2])
sleep(2)
dx.find_element_by_link_text("我的订单").click()
(3)如果有多个窗口通过for 循环判断,确定窗口
from selenium import webdriver
from time import *
from selenium.webdriver.common.action_chains import ActionChains
dx=webdriver.Chrome()
dx.get("https://www.baidu.com/")
print(dx.title)
dx.maximize_window()
sleep(2)
w='window.open("https://www.jd.com/")'
dx.execute_script(w)
print(dx.title)
w1='window.open("https://www.hao.com/")'
dx.execute_script(w1)
jb=dx.current_window_handle #获取当前窗口句柄
print(jb) #CDwindow-279E9F0F55B7B2F2AFA9245B9A580D86
jbs=dx.window_handles #获取所有窗口句柄
for i in jbs:
dx.switch_to.window(i)
if "京东(JD.COM)-正品低价、品质保障、配送及时、轻松购物!" in dx.title:
break
print(dx.title)
sleep(2)
dx.find_element_by_link_text("我的订单").click()
二、下拉框
网址:
https://yz.chsi.com.cn/zsml/zyfx_search.jsp
from selenium import webdriver
from time import *
from selenium.webdriver.support.select import Select
dx=webdriver.Chrome()
dx.get("https://yz.chsi.com.cn/zsml/zyfx_search.jsp")
print(dx.title)
dx.maximize_window()
sleep(2)
wz=dx.find_element_by_id("ssdm")
Select(wz).select_by_index(2) #通过索引选择
Select(wz).select_by_value("12") #通过value值选择
Select(wz).select_by_visible_text("(12)天津市") #通文本内容定位
标签:实战,webdriver,selenium,window,dx,print,import From: https://www.cnblogs.com/hutongxue/p/18423082