Selenium 控制浏览器操作

Selenium控制浏览器

方法

操作

使用场景

实现代码

get

打开浏览器

web测试第一步

driver.get(url)

refresh

刷新浏览器

模拟刷新浏览器

driver.refresh()

back

回退浏览器

模拟回退步骤

driver.back()

maxmize_window

最大化浏览器

模拟最大化浏览器

driver.maxmize_window()

minmize_window

最小化浏览器

模拟最小化浏览器

driver.minmize_window()

quit

关闭浏览器

模拟关闭浏览器

driver.quit()

get打开浏览器

from  selenium import webdriver
import time

def window_start():
    driver = webdriver.Edge()
    url = 'https://www.cnblogs.com/'
    driver.get(url)
    time.sleep(10)
    driver.quit()
    
if __name__ == '__main__':
    window_start()

refresh刷新浏览器

from  selenium import webdriver
import time

def window_refresh():
    driver = webdriver.Edge()
    url = 'https://www.cnblogs.com/'
    driver.get(url)
    time.sleep(5)
    driver.refresh()
    time.sleep(5)
    driver.quit()

if __name__ == '__main__':
    window_refresh()

back回退浏览器

from  selenium import webdriver
import time
from selenium.webdriver.common.by import By

def window_back():
    driver = webdriver.Edge()
    url = 'https://www.cnblogs.com/'
    driver.get(url)
    time.sleep(5)
    driver.find_element(By.CLASS_NAME,'navbar-anonymous').click()
    time.sleep(5)
    driver.back()
    time.sleep(5)
    driver.quit()

if __name__ == '__main__':
    window_back()

最大化浏览器

from  selenium import webdriver
import time
from selenium.webdriver.common.by import By

def window_max():
    driver = webdriver.Edge()
    url = 'https://www.cnblogs.com/'
    driver.get(url)
    driver.maximize_window()
    time.sleep(5)
    driver.quit()

if __name__ == '__main__':
    window_max()

最小化浏览器

from  selenium import webdriver
import time
from selenium.webdriver.common.by import By

def window_min():
    driver = webdriver.Edge()
    url = 'https://www.cnblogs.com/'
    driver.get(url)
    driver.minimize_window()
    time.sleep(5)
    driver.quit()

if __name__ == '__main__':
    window_min()

  

posted @ 2024-03-26 18:29  乌醍  阅读(30)  评论(0编辑  收藏  举报