python+selenium中实战应用
1、打开网页
(1)get方法:
场景1:一个网址覆盖另一个网址
from selenium import webdriver
import time
d=webdriver.Chrome()
d.get("https://www.baidu.com/")
time.sleep(2)
d.get("https://www.jd.com/")
(2)windo.wopen
场景2:
window.open 一个网址打开,另开一个窗口打开
from selenium import webdriver
import time
d=webdriver.Chrome()
d.get("https://www.baidu.com/")
time.sleep(2)
w="window.open('https://www.jd.com/')"
d.execute_script(w)
2、休眠:
1.强制等待 sleep(秒数)
2.隐性等待 implicitly_wait(秒数 )
3.显性等待 WebDriverWait
==============================
页面刷新
案例:
from selenium import webdriver
from time import sleep
d=webdriver.Chrome()
d.get("https://www.baidu.com/")
sleep(2)
d.refresh()
==============================
页面切换
back()返回上一页
forward 切换下一页
案例:
from selenium import webdriver
from time import sleep
d=webdriver.Chrome()
d.get("https://www.baidu.com/")
sleep(2)
d.get("https://www.jd.com/")
sleep(3)
d.back()
sleep(4)
d.forward()
==============================
设置窗口大小
命令:set_window_size(长,宽)
from selenium import webdriver
from time import sleep
d=webdriver.Chrome()
d.get("https://www.baidu.com/")
sleep(2)
d.set_window_size(500,500)
==============================
窗口最大化
命令:d.maximize_window()
案例:
from selenium import webdriver
from time import sleep
d=webdriver.Chrome()
d.get("https://www.baidu.com/")
sleep(2)
d.maximize_window()
============================
截屏
driver.get_screenshot_as_file(保存路径+图片名称)
案例
from selenium import webdriver
from time import sleep
d=webdriver.Chrome()
d.get("https://www.baidu.com/")
sleep(2)
d.get_screenshot_as_file(r"D:\f\hh\aa.png")
===========================
退出有两种方式:
第一种是 close 关闭当前窗口
from selenium import webdriver
from time import sleep
d=webdriver.Chrome()
d.get("https://www.baidu.com/")
sleep(2)
d.get("https://www.jd.com/")
sleep(2)
d.close()
第二种是 quit 关闭所有窗口
from selenium import webdriver
from time import sleep
d=webdriver.Chrome()
d.get("https://www.baidu.com/")
sleep(2)
d.get("https://www.jd.com/")
sleep(2)
d.quit()