Selenium 截屏、隱式等待頁面加載

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

url = 'https://www.baidu.com/'

if __name__ == '__main__':
    # 處理網頁
    # response = urllib.request.urlopen(url='http://www.baidu.com/')
    # text = response.read().decode('utf-8')
    # # 以utf-8編碼格式寫入 baidu.html數據
    # with open('./baidu.html', mode='w', encoding='utf-8') as fp:
    #     fp.write(text)
    #     print('網頁上的數據保存成功')

    # 處理圖片

    # response = urllib.request.urlopen(
    #     url='https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1589267789&di=1c3b3fc66e54381a9f81dceb00483ad2&src=http://img8.zol.com.cn/bbs/upload/23140/23139976.jpg')
    # text = response.read()
    # with open('./hehua.jpg', mode='wb') as fp:
    #     fp.write(text)
    #     print('圖片保存成功!')

    # url = 'https://www.taobao.com/'
    # request1 = request.Request(url=url)
    # response = urllib.request.urlopen(request1)
    #
    # print(response.read().decode('utf-8'))

    driver = webdriver.Chrome()
    driver.implicitly_wait(5)
    driver.get(url)
    driver.find_element_by_class_name('s_ipt').send_keys('輿情')
    driver.find_element(By.ID, 'su').click()
    time.sleep(5)

    try:
        picture_url = driver.save_screenshot('.\\baidu.png')
        print("%s :截圖成功!!!" % picture_url)
    except BaseException as msg:
        print('%s:截圖失敗!!!' % msg)
    driver.quit()

 

 

selenium + python 中文文檔 

https://python-selenium-zh.readthedocs.io/zh_CN/latest/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章