搜尋
首頁後端開發Python教學python與selenium以及chrome初級自動化操作的實例

python與selenium以及chrome初級自動化操作的實例

May 15, 2018 am 09:30 AM
chromepythonselenium

python與selenium以及chrome初級自動化操作的實例

#coding=utf-8from selenium import webdriver
import os,time

chromedriver = "C:\Users\li.liu\AppData\Local\Google\Chrome\chromedriver.exe"'''environ是一个字符串所对应环境的映像对象;
os.environ.keys()  主目录下所有的key
os.environ 显示key+内容。

windows:
· os.environ['HOMEPATH']:当前用户主目录。
os.environ['TEMP']:临时目录路径。
os.environ[PATHEXT']:可执行文件。
os.environ['SYSTEMROOT']:系统主目录。
os.environ['LOGONSERVER']:机器名。
os.environ['PROMPT']:设置提示符。
linux:
os.environ['USER']:当前使用用户。
os.environ['LC_COLLATE']:路径扩展的结果排序时的字母顺序。
os.environ['SHELL']:使用shell的类型。
os.environ['LAN']:使用的语言。
os.environ['SSH_AUTH_SOCK']:ssh的执行路径。'''os.environ["webdriver.chrome.driver"] = chromedriver

driver =  webdriver.Chrome(chromedriver)

driver.get("http://baidu.com")
time.sleep(3)
print (u'窗口最大化')
driver.maximize_window()
a=driver.title

print a
time.sleep(1)
driver.find_element_by_id('kw').send_keys('selenium')

driver.find_element_by_id('su').click()



driver.quit()

範例2.

#coding=utf-8from selenium import webdriver
import os,time,unittest
import log
import logging
import traceback
logger = log.Logger('e:/1/web_log.log',clevel = logging.DEBUG,Flevel = logging.INFO)
def f(n):
    logger.info(n)
    print n    
    
class baidu(unittest.TestCase):
    def setUp(self):
        
        self.chromedriver='C:\Users\li.liu\AppData\Local\Google\Chrome\chromedriver.exe'
        os.environ['webdriver.chrome.driver']=self.chromedriver
        self.driver=webdriver.Chrome(self.chromedriver)
        print u'驱动定义完成'
    def test_baidu_search(self):
        u"""百度搜索"""        
        driver = self.driver
        f(u'打开百度')
        driver.maximize_window()
        driver.get('http://baidu.com')
        time.sleep(2)
        print u'打开另一个网址'
        driver.get('http://news.baidu.com')
        print u'窗口最大化'
        driver.maximize_window()
        time.sleep(1)
        print u'返回上一个网页'
        driver.back()
        time.sleep(3)
        driver.forward()
        time.sleep(2)
        driver.back()
        time.sleep(1)
        driver.close()
        driver.quit()

    def test_baidu_set(self):
        driver = self.driver
        driver.get('http://baidu.com')
        time.sleep(3)        
        driver.find_element_by_id('kw').send_keys(u'测试')
        driver.find_element_by_id('su').click()
        time.sleep(2)
        driver.find_element_by_id('kw1').clear()
        driver.find_element_by_id('kw').send_keys(u'测试用例')
        time.sleep(3)
        print u'关闭浏览器'
        driver.close()
        print u'退出浏览器服务'
        driver.quit()    

if __name__=="__main__":    
    unittest.main()

a瀏覽器開發者模式

##FireFox— —>Firebug前端工具,而對於Chrome瀏覽器->右鍵選擇'審查元素'或快速鍵F12 ,即可看到一些開發寫的code,這對於取得測試對象(測試控制項)的某些屬性很有幫助。

b瀏覽器基本操作

瀏覽器視窗的最大化以及大小設置,以及前進,後退

這裡有幾點注意:
XXXX .get(URL)前往URL位址
time.sleep(ns) 等待時間
XXXX.back( ) 返回上一個瀏覽器
XXXX.forward() 前進到下一個瀏覽器
XXXX.set_window_size(width,height) width,height 我們可以自己依照需求設定。
XXXX.maximize() 視窗最大化
XXXX.title 得到瀏覽器的標題
XXXX.quit( ) 瀏覽器退出
XXXX.close( ) 瀏覽器關閉

import time —>保證腳本運行的穩定性

browser=webdriver.Chrome() —>呼叫Chrome瀏覽器
使用print()語句可以查看腳本是否執行正確
#

以上是python與selenium以及chrome初級自動化操作的實例的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
Python:深入研究彙編和解釋Python:深入研究彙編和解釋May 12, 2025 am 12:14 AM

pythonisehybridmodeLofCompilation和interpretation:1)thepythoninterpretercompilesourcecececodeintoplatform- interpententbybytecode.2)thepythonvirtualmachine(pvm)thenexecutecutestestestestestesthisbytecode,ballancingEaseofuseEfuseWithPerformance。

Python是一種解釋或編譯語言,為什麼重要?Python是一種解釋或編譯語言,為什麼重要?May 12, 2025 am 12:09 AM

pythonisbothinterpretedAndCompiled.1)它的compiledTobyTecodeForportabilityAcrosplatforms.2)bytecodeisthenInterpreted,允許fordingfordforderynamictynamictymictymictymictyandrapiddefupment,儘管Ititmaybeslowerthananeflowerthanancompiledcompiledlanguages。

對於python中的循環時循環與循環:解釋了關鍵差異對於python中的循環時循環與循環:解釋了關鍵差異May 12, 2025 am 12:08 AM

在您的知識之際,而foroopsareideal insinAdvance中,而WhileLoopSareBetterForsituations則youneedtoloopuntilaconditionismet

循環時:實用指南循環時:實用指南May 12, 2025 am 12:07 AM

ForboopSareSusedwhenthentheneMberofiterationsiskNownInAdvance,而WhileLoopSareSareDestrationsDepportonAcondition.1)ForloopSareIdealForiteratingOverSequencesLikelistSorarrays.2)whileLeleLooleSuitableApeableableableableableableforscenarioscenarioswhereTheLeTheLeTheLeTeLoopContinusunuesuntilaspecificiccificcificCondond

Python:它是真正的解釋嗎?揭穿神話Python:它是真正的解釋嗎?揭穿神話May 12, 2025 am 12:05 AM

pythonisnotpuroly interpred; itosisehybridablectofbytecodecompilationandruntimeinterpretation.1)PythonCompiLessourceceCeceDintobyTecode,whitsthenexecececected bytybytybythepythepythepythonvirtirtualmachine(pvm).2)

與同一元素的Python串聯列表與同一元素的Python串聯列表May 11, 2025 am 12:08 AM

concatenateListSinpythonWithTheSamelements,使用:1)operatoTotakeEpduplicates,2)asettoremavelemavphicates,or3)listcompreanspherensionforcontroloverduplicates,每個methodhasdhasdifferentperferentperferentperforentperforentperforentperfornceandordorimplications。

解釋與編譯語言:Python的位置解釋與編譯語言:Python的位置May 11, 2025 am 12:07 AM

pythonisanterpretedlanguage,offeringosofuseandflexibilitybutfacingperformancelanceLimitationsInCricapplications.1)drightingedlanguageslikeLikeLikeLikeLikeLikeLikeLikeThonexecuteline-by-line,允許ImmediaMediaMediaMediaMediaMediateFeedBackAndBackAndRapidPrototypiD.2)compiledLanguagesLanguagesLagagesLikagesLikec/c thresst

循環時:您什麼時候在Python中使用?循環時:您什麼時候在Python中使用?May 11, 2025 am 12:05 AM

Useforloopswhenthenumberofiterationsisknowninadvance,andwhileloopswheniterationsdependonacondition.1)Forloopsareidealforsequenceslikelistsorranges.2)Whileloopssuitscenarioswheretheloopcontinuesuntilaspecificconditionismet,usefulforuserinputsoralgorit

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能