#!/usr/bin/python3
from selenium import webdriver
~~~~
chrome = webdriver.Chrome()
chrome.maximize_window()
chrome.get(‘https://www.baidu.com‘)
chrome.find_element_by_id("kw").send_keys(‘selenium‘)
chrome.find_element_by_id(‘su‘).click()
print(‘url:\n%s‘%chrome.current_url)
#!/usr/bin/python3
from selenium import webdriver
import time
chrome = webdriver.Chrome()
chrome.maximize_window()
chrome.get(‘https://www.baidu.com‘)
print(‘html:\n%s‘%chrome.page_source)
结果:
html:
<html><head><script type="text/javascript" charset="utf-8" src="https://dss0.bdstatic.com/5aV1bjqh_Q
.......
.......
.......
</body></html>
#!/usr/bin/python3
from selenium import webdriver
import time
chrome = webdriver.Chrome()
chrome.maximize_window()
chrome.get(‘https://www.baidu.com‘)
print(‘title:\n%s‘%chrome.title)
结果:
title:
百度一下,你就知道
#!/usr/bin/python3
from selenium import webdriver
import time
chrome = webdriver.Chrome()
chrome.maximize_window()
chrome.get(‘https://www.baidu.com‘)
print(‘当前页面url:\n%s‘%chrome.current_url)
chrome.find_element_by_id("kw").send_keys(‘selenium‘)
chrome.find_element_by_id(‘su‘).click()
print(‘搜索后url:\n%s‘%chrome.current_url)
#页面后退
chrome.back()
print(‘页面后退:\n%s‘%chrome.current_url)
#页面前进
chrome.forward()
print(‘页面前进:\n%s‘%chrome.current_url)
结果:
当前页面url:
https://www.baidu.com/
搜索后url:
https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=selenium&fenlei=256&rsv_pq=9b63d9230007b880&rsv_t=47b1FxKvVwqi1pyaL6pzZPDJXOzBrhGtwG2Pek8qI74PCaScK3DjcRX7f7I&rqlang=cn&rsv_enter=0&rsv_dl=tb&rsv_sug3=8&rsv_sug1=1&rsv_sug7=100&rsv_btype=i&inputT=249&rsv_sug4=250
页面后退:
https://www.baidu.com/
页面前进:
https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=selenium&fenlei=256&rsv_pq=9b63d9230007b880&rsv_t=47b1FxKvVwqi1pyaL6pzZPDJXOzBrhGtwG2Pek8qI74PCaScK3DjcRX7f7I&rqlang=cn&rsv_enter=0&rsv_dl=tb&rsv_sug3=8&rsv_sug1=1&rsv_sug7=100&rsv_btype=i&inputT=249&rsv_sug4=250
#quit:退出浏览器驱动并关闭浏览器
driver.quit()
#close:只关毕浏览器
driver.close()
#设置窗口大小
dirver.set_window_size(480,800)
#全屏显示
dirver.maximize_window()
dirver.refresh()
driver.name()
#!/usr/bin/python3
from selenium import webdriver
import time
chrome = webdriver.Chrome()
chrome.maximize_window()
chrome.get(‘https://www.baidu.com‘)
print(‘当前浏览器:\n%s‘%chrome.name)
结果:
当前浏览器:
chrome
原文:https://www.cnblogs.com/jingxindeyi/p/13041081.html