1.浏览器的前进与后退
1 # _*_ coding:utf-8 _*_ 2 # 导入webdriver包 3 from selenium import webdriver 4 from time import sleep 5 driver = webdriver.Firefox() 6 driver.get("https://www.baidu.com/") 7 sleep(5) 8 print("访问news页") 9 driver.get("http://news.baidu.com/") 10 sleep(5) 11 driver.back()#浏览器后退 12 sleep(5) 13 driver.forward()#浏览器前进 14 sleep(5) 15 driver.refresh()#浏览器刷新 16 sleep(5) 17 driver.quit()
2.浏览器窗口大小控制
1 #!/usr/bin/env python 2 # _*_ coding:utf-8 _*_ 3 # 导入webdriver包 4 from selenium import webdriver 5 from time import sleep 6 driver = webdriver.Firefox() 7 driver.get("https://www.baidu.com/") 8 sleep(5) 9 driver.set_window_size(800,600) #浏览器设置大小 10 sleep(5) 11 driver.maximize_window() #浏览器全屏 12 sleep(5) 13 driver.quit()
3.页面iframe窗口切换
iframe.html示例
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>iframe实例</title> </head> <body> <div class = "alert" align = "center">The link <a class = "alert-link" href="https://www.baidu.com/">baidu</a> </div> <div class = "row-fluid" align = "center"> <div class = "span-iframe" align = "center"> <h4 align = "center">iframe</h4> <iframe id ="iname" name = "nf" src = "https://www.baidu.com/" width = "800" height = "600"></iframe> </div> </div> </body> </html>
python代码控制
1 # iframe表单切换 2 from selenium import webdriver 3 from time import sleep 4 driver = webdriver.Firefox() #创建火狐对象 5 driver.get("file:///C:/Users/Administrator/Desktop/iframe.html") 6 driver.switch_to.frame("iname") #跳到指定的frame 7 driver.find_element_by_xpath('//*[@id="kw"]').send_keys("selenium") 8 driver.find_element_by_xpath('//*[@id="su"]').click() 9 sleep(10) 10 driver.switch_to.parent_frame() #跳到上一级 11 driver.find_element_by_xpath('/html/body/div[1]/a').click() 12 sleep(10) 13 driver.quit() #关闭浏览器
4.Alert弹框处理
alert.html示例
<!DOCTYPE html PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html> <head> <title>Alert Test</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> </head> <body> <h2>Alert Test</h2> <script type="text/javascript"> function showName(){ alert("此处展示姓名"); } function showPassword(){ alert("此处展示密码"); } function showMessage(){ alert("这里是注意事项"); } </script> <form name="f1"> <input type="text" name="t1" value="请确认您的信息"><br><br> <input type="button" name="b1" value="姓名确认" onclick="showName()"><br> <input type="button" name="b2" value="密码确认" onclick="showPassword()"><br> <input type="button" name="b3" value="提示" onclick="showMessage()"><br> </form> </body> </html>
python代码控制
1 # Alert弹框处理 2 from selenium import webdriver 3 from time import sleep 4 driver = webdriver.Firefox() #创建火狐对象 5 driver.get("file:///C:/Users/Administrator/Desktop/alert.html") 6 driver.find_element_by_xpath('/html/body/form/input[2]').click() 7 print(driver.switch_to.alert.text) #输出alert弹框中的文本 8 # driver.switch_to.alert.accept() #接受警示框 9 driver.switch_to.alert.dismiss() #解散警告框 10 sleep(5) 11 driver.quit() #关闭浏览器
5.cookie控制
百度为例
1 # cookie 2 from selenium import webdriver 3 from time import sleep 4 driver = webdriver.Firefox() #创建火狐对象 5 driver.get("https://www.baidu.com/") 6 cookies = driver.get_cookies() 7 print(cookies) 8 for cookie in cookies: 9 print("name = %s"%cookie["name"]) 10 driver.quit()
6.JS操作(滚动条处理)
百度为例
1 # JS操作 2 from selenium import webdriver 3 from time import sleep 4 driver = webdriver.Firefox() #创建火狐对象 5 driver.get("https://www.baidu.com/") 6 driver.find_element_by_xpath("//*[@id='kw']").send_keys("selenium")#通过xpath定位 7 driver.find_element_by_xpath("//*[@id='su']").click() 8 driver.set_window_size(800,600) 9 sleep(5) 10 print("要执行了") 11 js = "window.scrollTo(200,0)" #100,500对应的是x,y轴 12 driver.execute_script(js) 13 sleep(10) 14 driver.quit()
7.截图
1 # 截图 2 from selenium import webdriver 3 from time import sleep 4 driver = webdriver.Firefox() #创建火狐对象 5 driver.get("https://www.baidu.com/") 6 driver.find_element_by_xpath("//*[@id='kw']").send_keys("selenium")#通过xpath定位 7 driver.find_element_by_xpath("//*[@id='su']").click() 8 sleep(5) 9 driver.get_screenshot_as_file("D:\用户目录\下载\baidu.png") 10 sleep(10) 11 driver.quit()
8.文件上传
uploadfile.html示例
<html> <body> <div> <form name="form1" action="fileUpload.php" method="post" enctype="multipart/form-data"> <label for="file">File:</label> <input type="file" name="file" id="file" /> <br /> <input type="hidden" name="multi" value="false"/> <input type="submit" name="submit" value="Submit Single" /> </form> </div> </body> </html>
python代码控制
1 # 文件上传 2 from selenium import webdriver 3 driver = webdriver.Firefox() #创建火狐对象 4 driver.get("file:///C:/Users/Administrator/Desktop/uploadfile.html") 5 driver.find_element_by_xpath("//*[@id='file']").send_keys("D:用户目录下载我是个算命先生.txt")#打开文件
结果展示