検索
ホームページバックエンド開発Python チュートリアルPython、Selenium、Chrome を使用した主要な自動化操作の例

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()

ブラウザ開発者モード

FireFox——>Firebug フロントエンド ツール、および Chrome ブラウザの場合——> 右クリック選択「要素の検査」またはショートカット キー F12 を押すと、開発者が作成したコードが表示されます。これは、テスト オブジェクト (テスト コントロール) の特定のプロパティを取得するのに非常に役立ちます。

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() ブラウザを閉じる

スクリプト動作の安定性を確保する
browser=webdriver.Chrome() —>Chrome ブラウザを呼び出します
print() ステートメントを使用して、スクリプトが正しく実行されているかどうかを確認します

以上がPython、Selenium、Chrome を使用した主要な自動化操作の例の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
Python:コンパイラまたはインタープリター?Python:コンパイラまたはインタープリター?May 13, 2025 am 12:10 AM

Pythonは解釈された言語ですが、コンパイルプロセスも含まれています。 1)Pythonコードは最初にBytecodeにコンパイルされます。 2)ByteCodeは、Python Virtual Machineによって解釈および実行されます。 3)このハイブリッドメカニズムにより、Pythonは柔軟で効率的になりますが、完全にコンパイルされた言語ほど高速ではありません。

ループvs whileループ用のpython:いつ使用するか?ループvs whileループ用のpython:いつ使用するか?May 13, 2025 am 12:07 AM

useaforloopwhenteratingoverasequenceor foraspificnumberoftimes; useawhileloopwhentinuninguntinuntilaConditionismet.forloopsareidealforknownownownownownownoptinuptinuptinuptinuptinutionsituations whileoopsuitsituations withinterminedationations。

Pythonループ:最も一般的なエラーPythonループ:最も一般的なエラーMay 13, 2025 am 12:07 AM

pythonloopscanleadtoErrorslikeinfiniteloops、ModifiningListsDuringiteration、Off-Oneerrors、Zero-dexingissues、およびNestededLoopinefficiencies.toavoidhese:1)use'i

ループの場合、およびPythonのループ:それぞれの利点は何ですか?ループの場合、およびPythonのループ:それぞれの利点は何ですか?May 13, 2025 am 12:01 AM

forloopsareadvastountousforknowterations and sequences、offeringsimplicityandeadability;

Python:編集と解釈に深く掘り下げますPython:編集と解釈に深く掘り下げますMay 12, 2025 am 12:14 AM

pythonusesahybridmodelofcompilation andtertation:1)thepythoninterpretercompilessourcodeodeplatform-indopent bytecode.2)thepythonvirtualmachine(pvm)thenexecuteTesthisbytecode、balancingeaseoputhswithporformance。

Pythonは解釈されたものですか、それとも編集された言語であり、なぜそれが重要なのですか?Pythonは解釈されたものですか、それとも編集された言語であり、なぜそれが重要なのですか?May 12, 2025 am 12:09 AM

pythonisbothintersedand compiled.1)it'scompiledtobytecode forportabalityacrossplatforms.2)bytecodeisthenは解釈され、開発を許可します。

ループ対pythonのループの場合:説明されたキーの違いループ対pythonのループの場合:説明されたキーの違いMay 12, 2025 am 12:08 AM

loopsareideal whenyouwhenyouknumberofiterationsinadvance、foreleloopsarebetterforsituationsは、loopsaremoreedilaConditionismetを使用します

ループのために:実用的なガイドループのために:実用的なガイドMay 12, 2025 am 12:07 AM

henthenumber ofiterationsisknown advanceの場合、dopendonacondition.1)forloopsareideal foriterating over for -for -for -saredaverseversives likelistorarrays.2)whileopsaresupasiable forsaresutable forscenarioswheretheloopcontinupcontinuspificcond

See all articles

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

このプロジェクトは osdn.net/projects/mingw に移行中です。引き続きそこでフォローしていただけます。 MinGW: GNU Compiler Collection (GCC) のネイティブ Windows ポートであり、ネイティブ Windows アプリケーションを構築するための自由に配布可能なインポート ライブラリとヘッダー ファイルであり、C99 機能をサポートする MSVC ランタイムの拡張機能が含まれています。すべての MinGW ソフトウェアは 64 ビット Windows プラットフォームで実行できます。

Dreamweaver Mac版

Dreamweaver Mac版

ビジュアル Web 開発ツール

MantisBT

MantisBT

Mantis は、製品の欠陥追跡を支援するために設計された、導入が簡単な Web ベースの欠陥追跡ツールです。 PHP、MySQL、Web サーバーが必要です。デモおよびホスティング サービスをチェックしてください。

WebStorm Mac版

WebStorm Mac版

便利なJavaScript開発ツール

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境