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
- get() directly access a certain URL (passing parameters to enter the URL)
- back() Return to the previous page
- forward() to the next page
- close () close the current tab
- quit() close the browser
- set_window_size() Set the size of the browser (pass parameters and enter the length and width of the browser)
- maximize_window() maximize the browser
- refresh() refresh the page
We can use a picture to explain the corresponding position of the browser operation method in the actual browser
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()