selenium一些用法

基本使用方法

from selenium import webdriver  # 瀏覽器驅動器
from selenium.webdriver import ActionChains  # 拖動,滑動驗證
from selenium.webdriver.common.by import By #按照什麼方式查找,By.ID,By.CSS_SELECTOR
from selenium.webdriver.common.keys import Keys #鍵盤按鍵操作
from selenium.webdriver.support import expected_conditions as EC #場景判斷用的,一般和下面的等待加載元素一起使用
from selenium.webdriver.support.wait import WebDriverWait #等待頁面加載某些元素

browser=webdriver.Chrome()
try:
    browser.get('https://www.baidu.com')

    input_tag=browser.find_element_by_id('kw')
    input_tag.send_keys('美女') #python2中輸入中文錯誤,字符串前加個u
    input_tag.send_keys(Keys.ENTER) #輸入回車

    wait=WebDriverWait(browser,10)

    wait.until(EC.presence_of_element_located((By.ID,'content_left'))) #等到id爲content_left的元素加載完畢,最多等10秒

    print(browser.page_source)
    print(browser.current_url)
    print(browser.get_cookies())

finally:
    browser.close()

其它基本方法鏈接:https://www.cnblogs.com/jokerbj/p/8258902.html

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