Python Selenium 頁面操作總結

分享知識 傳遞快樂


打開瀏覽器: driver = webdriver.Chrome()

請求一個url: driver.get(“www.baidu.com”)

窗口最大化: driver.maximize_window()

窗口最小化: driver.minimize_window()

隱式等待: driver.implicitly_wait(second)

設置頁面渲染超時: browser.set_page_load_timeout(second)

設置腳本執行超時: browser.set_script_timeout(second)

設置瀏覽器寬高: browser.set_window_size(width, height)

設置全屏窗口: driver.fullscreen_window()

返回當前頁面的title: driver.title

返回當前頁面的url: driver.current_url

返回當前頁面的源碼: driver.page_source

獲取當前窗口句柄: driver.current_window_handle

獲取當前所有窗口句柄: driver.window_handles

切換到新打開的窗口: driver.switch_to.window(newhandle)

關閉當前頁面: driver.close()

註銷並關閉瀏覽器: driver.quit()

瀏覽器前進: driver.forward()

瀏覽器後退: driver.back()

刷新當前頁面: driver.refresh()

執行腳本: driver.execute_script()

獲取當前session中的全部cookie: driver.get_cookies()

獲取當前會話中的指定cookie: driver.get_cookie(name)

在當前會話中添加cookie: driver.add_cookie(self, cookie_dict)

添加設置項Chrome Options

options = webdriver.ChromeOptions()
options.add_argument('xxxx')
driver = webdriver.Chrome(chrome_options=options)

添加瀏覽器User-Agent

options.add_argument('User-Agent=Mozilla/5.0 (Linux; U; Android 4.0.2; en-us; Galaxy Nexus Build/ICL53F) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30')












————————————

如有不妥之處請留言指正。
相互學習,共同進步。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章