python3 selenium使用,python3selenium

來源:互聯網
上載者:User

python3 selenium使用,python3selenium

其實這個就相當於類比人的點擊事件來連續的訪問瀏覽器。如果你玩過王者榮耀的話在2016年一月份的版本裡面就有一個bug。

安卓手機下載一個按鍵精靈就可以在冒險模式裡面設定按鍵,讓手機自動玩闖關,一局19個金幣,一晚上就一個英雄了。不過

程式員也不是吃素的。給一個星期設定了大概4000金幣上限。有興趣的可以去試試。(註:手機需要root)

進入正題:

from selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.common.keys import Keysfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.webdriver.support.wait import WebDriverWait

在寫之前需要下載selenium模組

brguge=webdriver.Chrome()#聲明驅動對象try:    brguge.get('https://www.baidu.com')#發送get請求    input=brguge.find_element_by_id('kw')#找到目標    input.send_keys('python')#輸入python關鍵字    input.send_keys(Keys.ENTER)#敲入斷行符號    wait=WebDriverWait(brguge,10)#等待元素載入出來    wait.until(EC.presence_of_element_located(By.ID,'content_left'))#載入    print(brguge.current_url)#輸出搜尋的路徑    print(brguge.get_cookie())#輸出cookie    print(brguge.page_source)#輸出結果原始碼finally:    brguge.close()#關閉Google瀏覽器

下面是一些selenium模組的基本用法

尋找元素
單個元素 (from selenium import webdriver)
brguge.find_element_by_id('q')用這個元素找id是q的元素
brguge.find_element_by_css_selector('#q')找css樣式是q的
brguge.find_element_by_xpath('//*[ @id="q"]')三個效果一樣
brguge.find_element_by_name()通過name來尋找
brguge.find_element_by_link_text()通過link來尋找
brguge.find_element_by_partial_link_text()
brguge.find_element_by_tag_name()
brguge.find_element_by_class_name()通過class尋找

from selenium import webdriver
from selenium.webdriver.common.by import By
brguge.find_element(By.ID,'Q')通用尋找方式
多個元素(find_elements)加了個s
他會以列表的形式列印出來
brguge.find_elements_by_css_selector('.service-bd li')css樣式為li的元素
brguge.find_elements(By.css_selector,'.service-bd li')兩個作用一樣
(利用索引就可以擷取單個或多個元素了)

元素互動操作(擷取元素然後再給他指令)
選擇輸入框 --》send_keys('輸入文字')--》clear()清空輸入框--在輸入別的--》找到搜尋--》click(點擊)
input.clear()清空按鈕
互動動作(將動作附加到動作鏈中串列執行)
switch_to_frame('iframeResult')
用css樣式分別找到兩個要互動
調用ActionChains(調用Google的)
drag_and_drop(source,target)第一個到第二個上面
perform()

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.