一、打开浏览器 (get)
案例1:打开一个浏览器
from selenium import webdriver
import time
dx=webdriver.Chrome()
dx.get("https://www.baidu.com/")
案例2:打开两个网址(覆盖)
from selenium import webdriver
import time
dx=webdriver.Chrome()
dx.get("https://www.baidu.com/")
time.sleep(2)
dx.get("https://www.jd.com/")
(3)打开两个网址(重开窗口)
from selenium import webdriver
import time
dx=webdriver.Chrome()
dx.get("https://www.baidu.com/")
time.sleep(2)
w="window.open('https://www.jd.com/')"
dx.execute_script(w)
二、时间等待
1、强制等待(不管能不能查找元素,都要等待规定的时间)
sleep
from selenium import webdriver
import time
dx=webdriver.Chrome()
dx.get("https://www.baidu.com/")
time.sleep(1)
dx.get("https://www.baidu.com/")
2、隐式等待(所在规定范围等待时间,如果提前找到元素,就节省时间)
语句:implicitly_wait
from selenium import webdriver
import time
dx=webdriver.Chrome()
dx.get("https://www.baidu.com/")
dx.implicitly_wait(10)
dx.get("https://www.baidu.com/")
3、显性等待
等待元素出现后,在进行下一步操作
三、refresh()刷新
from selenium import webdriver
import time
dx=webdriver.Chrome()
dx.get("https://www.baidu.com/")
time.sleep(3)
dx.refresh()
四、back 返回上一页
from selenium import webdriver
import time
dx=webdriver.Chrome()
dx.get("https://www.baidu.com/")
time.sleep(3)
dx.get("https://www.jd.com/")
time.sleep(3)
dx.back()
五、forward切换到下一页
from selenium import webdriver
import time
dx=webdriver.Chrome()
dx.get("https://www.baidu.com/")
time.sleep(3)
dx.get("https://www.jd.com/")
time.sleep(3)
dx.back()
time.sleep(3)
dx.forward()
六、set_window_size设置页面窗口大小
from selenium import webdriver
import time
dx=webdriver.Chrome()
dx.get("https://www.baidu.com/")
time.sleep(3)
dx.set_window_size(200,200)
七、maximize_window() 窗口最大化
from selenium import webdriver
import time
dx=webdriver.Chrome()
dx.get("https://www.baidu.com/")
time.sleep(3)
dx.maximize_window()
八、get_screenshot_as_file 截屏
from selenium import webdriver
import time
dx=webdriver.Chrome()
dx.get("https://www.baidu.com/")
time.sleep(3)
dx.get_screenshot_as_file(r"C:\Users\Administrator\Desktop\tupian\1.jpg")
九、close 关闭当前窗口
from selenium import webdriver
import time
dx=webdriver.Chrome()
dx.get("https://www.baidu.com/")
print(dx.title)
time.sleep(3)
w="window.open('https://www.jd.com/')"
dx.execute_script(w)
print(dx.title)
time.sleep(2)
dx.close()
十、quit 关闭所有的窗口
from selenium import webdriver
import time
dx=webdriver.Chrome()
dx.get("https://www.baidu.com/")
print(dx.title)
time.sleep(3)
w="window.open('https://www.jd.com/')"
dx.execute_script(w)
print(dx.title)
time.sleep(2)
dx.quit()
11、打印当前的抬头
from selenium import webdriver
import time
dx=webdriver.Chrome()
dx.get("https://www.baidu.com/")
print(dx.title)
time.sleep(3)
w="window.open('https://www.jd.com/')"
dx.execute_script(w)
print(dx.title)
time.sleep(2)