一、alert 弹框
from selenium import webdriver
from time import *
dx=webdriver.Chrome()
url=r"file:///E:/dcs/two/selenium/%E5%BC%B9%E6%A1%86/alert%E5%BC%B9%E6%A1%86.html"
dx.get(url)
print(dx.title)
tk=dx.switch_to.alert 切换到弹框中
tk.dismiss() 取消
二、确认性弹框
from selenium import webdriver
from time import *
url=file:///E:/dcs/two/selenium/%E5%BC%B9%E6%A1%86/enter.html
dx=webdriver.Chrome()
dx.get(url)
print(dx.title)
dx.find_element_by_class_name("alert").click()
tk=dx.switch_to.alert
tk.dismiss()
(三)输入型弹框
from selenium import webdriver
from time import *
dx=webdriver.Chrome()
url=r"file:///E:/dcs/two/selenium/%E5%BC%B9%E6%A1%86/prompt.html"
dx.get(url)
print(dx.title)
dx.find_element_by_class_name("alert").click()
tk=dx.switch_to.alert
tk.send_keys("dcs")
tk.accept()
(四)上传文件类型弹框
from selenium import webdriver
from time import *
url=file:///E:/dcs/two/selenium/%E5%BC%B9%E6%A1%86/upload_file.html
dx=webdriver.Chrome()
dx.get(url)
print(dx.title)
dx.find_element_by_id("file").send_keys(r"E:\az\f\ck\ddd.txt")
(五)iframe框
from selenium import webdriver
from time import *
dx=webdriver.Chrome()
url=r"https://graph.qq.com/oauth2.0/show?which=Login&display=pc&response_type=code&state=5DFCF10A905CCC9B086DD961CBBE1C550A9F09C40DA8C60F5B10F90423E4722D2956CD16E74FEF942C44EBB8ACC6F914&client_id=100273020&redirect_uri=https%3A%2F%2Fqq.jd.com%2Fnew%2Fqq%2Fcallback.action%3Fuuid%3Dbff66901b7ce422fb351b3b36ec54a4c"
dx.get(url)
print(dx.title)
k=dx.find_element_by_id("ptlogin_iframe")
dx.switch_to.frame(k) #进入frame框
sleep(2)
dx.find_element_by_link_text("密码登录").click()
sleep(2)
dx.switch_to.default_content() #退出frame框
dx.find_element_by_link_text("服务协议").click()
一、滚动条
(1)
from selenium import webdriver
from time import *
dx=webdriver.Chrome()
url="https://www.jd.com/"
dx.get(url)
j='window.scrollTo(0,2000)'
dx.execute_script(j)
sleep(3)
j1='window.scrollTo(0,0)' #滚动到原始点
dx.execute_script(j1)
(2)
from selenium import webdriver
from time import *
dx=webdriver.Chrome()
url="https://www.jd.com/"
dx.get(url)
dx.maximize_window()
sleep(2)
js="var d=document.documentElement.scrollTop=2000"
dx.execute_script(js)
sleep(3)
js1="var d=document.documentElement.scrollTop=0"
dx.execute_script(js1)
(3)
from selenium import webdriver
from time import *
dx=webdriver.Chrome()
url="https://www.jd.com/"
dx.get(url)
dx.maximize_window()
sleep(2)
n=0
while n<5000:
n+=1000
js="var d=document.documentElement.scrollTop="+str(n)
dx.execute_script(js)
sleep(3)
二、快捷键:
网址:https://www.baidu.com/
(1) 输入、全选、剪切、粘贴、确认
from selenium import webdriver
from time import *
from selenium.webdriver.common.keys import Keys
dx=webdriver.Chrome()
url="https://www.baidu.com/"
dx.get(url)
dx.maximize_window()
dx.find_element_by_id("kw").send_keys("dcs")
sleep(2)
dx.find_element_by_id("kw").send_keys(Keys.CONTROL,"a")
sleep(2)
dx.find_element_by_id("kw").send_keys(Keys.CONTROL,"x")
sleep(2)
dx.find_element_by_id("kw").send_keys(Keys.CONTROL,"v")
sleep(2)
dx.find_element_by_id("kw").send_keys(Keys.ENTER)
(2)通过封装(简化代码)
from selenium import webdriver
from time import *
from selenium.webdriver.common.keys import Keys
dx=webdriver.Chrome()
url="https://www.baidu.com/"
dx.get(url)
dx.maximize_window()
def kj(lj,a):
dx.find_element_by_id(lj).send_keys(a)
sleep(3)
if name == 'main':
kj("kw","dcs")
kj("kw", Keys.CONTROL,"a")
kj("kw", Keys.CONTROL, "x")
kj("kw", Keys.CONTROL, "v")
kj("kw", Keys.ENTER)