Selenium's basic operation of the browser

Basic operation of the browser:

We can directly control the browser through code, such as visiting a certain URL, forward and backward, jump to the browser size, close and other commonly used functions, let’s briefly introduce

  1. get() directly access a certain URL (passing parameters to enter the URL)
  2. back() Return to the previous page
  3. forward() to the next page
  4. close () close the current tab
  5. quit() close the browser
  6. set_window_size() Set the size of the browser (pass parameters and enter the length and width of the browser)
  7. maximize_window() maximize the browser
  8. refresh() refresh the page

We can use a picture to explain the corresponding position of the browser operation method in the actual browser
Insert picture description here

from selenium import webdriver
from time import sleep 
driver = webdriver.Chrome()
# get() 进入百度页面
driver.get("https://www.baidu.com/")
sleep(1)
# get() 进入贴吧页面
driver.get("https://tieba.baidu.com/")
sleep(1) 
# back() 返回上一页:百度页面
driver.back()
sleep(1) 
# forward() 返回下一页:贴吧页面
driver.forward()
sleep(1) 
# set_window_size() 设置浏览器大小
driver.set_window_size(500, 1000)
sleep(1) 
# maximize_window() 最大化浏览器
driver.maximize_window()
sleep(1)
# 点击title为娱乐明星的<a>标签元素
driver.find_element_by_css_selector("a[title = '娱乐明星']").click()
sleep(1) 
# 关闭当前页面
driver.close()
sleep(2)
# 打印浏览器version的值
print(driver.capabilities['version']) 
# 关闭浏览器
driver.quit()

Guess you like

Origin blog.csdn.net/zhuan_long/article/details/110128424