pythonでseleniumを使う
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
単語検索
|
最終更新
|
ヘルプ
]
開始行:
* 目次 [#z23d6087]
#contents
* セットアップ [#sbf0d636]
webdriverは、現在利用しているブラウザとバージョンを合わせ...
仮にこの記事ではc:\binフォルダを作成し
環境変数のPATHで上記のパスを追加した。
* 基本コード [#n0139e9b]
簡単なサンプルコードを示す
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditio...
# ヘッドレスモードのオプションを設定
options = Options()
options.add_argument('--headless')
options.add_argument('--disable-gpu') # GPUハードウェア...
# ChromeドライバーのパスをServiceオブジェクトとして指定
service = Service('c:\\bin\\chromedriver.exe')
# Serviceオブジェクトを使用してChromeドライバーのインス...
# driver = webdriver.Chrome(service=service)
driver = webdriver.Chrome(service=service, options=optio...
# ウェブサイトにアクセス
driver.get('https://www.yahoo.co.jp/')
search_box = driver.find_element(By.NAME, 'p')
search_box.send_keys('Python')
search_box.send_keys(Keys.RETURN)
# 検索結果ページの特定の要素(例えば、特定のクラス名を持...
WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, 'sw-C...
)
# ページの高さを取得
total_height = driver.execute_script("return document.bo...
# ウィンドウサイズをページの高さに合わせる
driver.set_window_size(1920, total_height)
# スクリーンショットの取得と保存
driver.save_screenshot('screenshot.png')
# ウェブページのタイトルを取得して表示
print(driver.title)
# ブラウザを閉じる
driver.quit()
* テスト用にブラッシュアップする [#j4a4e5e3]
** 実行方法 [#t25d5e38]
pythonの標準テストフレームワークであるunittestは以下のコ...
*** すべてのテストケースの実行方法 [#b20cd0fe]
テストケースのあるフォルダで、
python -m unittest
*** 部分的なテストケースの実行方法 [#n99e4223]
上記のコマンドに続けて、以下のように記載する
python -m unittest test_yahoo_search.YahooSearchTest
** テストコードのベースクラス [#s5bea9f6]
test_base.pyとして保存
import unittest
import os
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
class TestBase(unittest.TestCase):
def setUp(self):
# ヘッドレスモードでChromeを起動するためのオプシ...
options = Options()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
service = Service('c:\\bin\\chromedriver.exe')
self.driver = webdriver.Chrome(service=service, ...
def take_screenshot(self, test_method_name):
# 現在の時刻を取得してタイムスタンプを作成
timestamp = time.strftime("%Y%m%d-%H%M%S")
# スクリーンショットの保存ディレクトリを作成(存...
directory = f'screenshots/{test_method_name}'
if not os.path.exists(directory):
os.makedirs(directory)
# スクリーンショットのファイル名を設定して保存
screenshot_file = f'{directory}/screenshot-{time...
self.driver.save_screenshot(screenshot_file)
def take_screenshot_defaultsize(self, test_method_na...
# ウィンドウサイズを合わせる
driver.set_window_size(1920, 1080)
self.driver.save_screenshot(screenshot_file)
def take_screenshot_allheight(self, test_method_name):
# ページの高さを取得
total_height = driver.execute_script("return doc...
# ウィンドウサイズをページの高さに合わせる
driver.set_window_size(1920, total_height)
take_screenshot(self, test_method_name)
def tearDown(self):
# ブラウザを閉じる
self.driver.quit()
** テストベースを利用したテストケース [#n8ed68a5]
テストベースを利用することで、テストで使う共通処理を再利...
test_yahoo_search.py
from test_base import TestBase
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditio...
class YahooSearchTest(TestBase):
def test_search_in_yahoo_japan(self):
# Yahoo Japanのページにアクセス
self.driver.get('https://www.yahoo.co.jp/')
# 検索ボックスを見つけて、キーワードを入力して検...
search_box = self.driver.find_element(By.NAME, '...
search_box.send_keys('Python')
search_box.send_keys(Keys.RETURN)
# 検索結果が表示されるのを待機
WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.CLASS_NAM...
)
# スクリーンショットを取得して保存
self.take_screenshot_allheight(self.__class__.__...
# その他のテストコード...
# その他のテストケース...
* git bashで文字化けする場合の対応方法 [#t2ebd4ab]
pythonをgitbashで使うと、windowsの場合は文字化けしていが...
&ref(スクリーンショット 2023-12-10 115206.png);
&ref(スクリーンショット 2023-12-10 115425.png);
終了行:
* 目次 [#z23d6087]
#contents
* セットアップ [#sbf0d636]
webdriverは、現在利用しているブラウザとバージョンを合わせ...
仮にこの記事ではc:\binフォルダを作成し
環境変数のPATHで上記のパスを追加した。
* 基本コード [#n0139e9b]
簡単なサンプルコードを示す
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditio...
# ヘッドレスモードのオプションを設定
options = Options()
options.add_argument('--headless')
options.add_argument('--disable-gpu') # GPUハードウェア...
# ChromeドライバーのパスをServiceオブジェクトとして指定
service = Service('c:\\bin\\chromedriver.exe')
# Serviceオブジェクトを使用してChromeドライバーのインス...
# driver = webdriver.Chrome(service=service)
driver = webdriver.Chrome(service=service, options=optio...
# ウェブサイトにアクセス
driver.get('https://www.yahoo.co.jp/')
search_box = driver.find_element(By.NAME, 'p')
search_box.send_keys('Python')
search_box.send_keys(Keys.RETURN)
# 検索結果ページの特定の要素(例えば、特定のクラス名を持...
WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, 'sw-C...
)
# ページの高さを取得
total_height = driver.execute_script("return document.bo...
# ウィンドウサイズをページの高さに合わせる
driver.set_window_size(1920, total_height)
# スクリーンショットの取得と保存
driver.save_screenshot('screenshot.png')
# ウェブページのタイトルを取得して表示
print(driver.title)
# ブラウザを閉じる
driver.quit()
* テスト用にブラッシュアップする [#j4a4e5e3]
** 実行方法 [#t25d5e38]
pythonの標準テストフレームワークであるunittestは以下のコ...
*** すべてのテストケースの実行方法 [#b20cd0fe]
テストケースのあるフォルダで、
python -m unittest
*** 部分的なテストケースの実行方法 [#n99e4223]
上記のコマンドに続けて、以下のように記載する
python -m unittest test_yahoo_search.YahooSearchTest
** テストコードのベースクラス [#s5bea9f6]
test_base.pyとして保存
import unittest
import os
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
class TestBase(unittest.TestCase):
def setUp(self):
# ヘッドレスモードでChromeを起動するためのオプシ...
options = Options()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
service = Service('c:\\bin\\chromedriver.exe')
self.driver = webdriver.Chrome(service=service, ...
def take_screenshot(self, test_method_name):
# 現在の時刻を取得してタイムスタンプを作成
timestamp = time.strftime("%Y%m%d-%H%M%S")
# スクリーンショットの保存ディレクトリを作成(存...
directory = f'screenshots/{test_method_name}'
if not os.path.exists(directory):
os.makedirs(directory)
# スクリーンショットのファイル名を設定して保存
screenshot_file = f'{directory}/screenshot-{time...
self.driver.save_screenshot(screenshot_file)
def take_screenshot_defaultsize(self, test_method_na...
# ウィンドウサイズを合わせる
driver.set_window_size(1920, 1080)
self.driver.save_screenshot(screenshot_file)
def take_screenshot_allheight(self, test_method_name):
# ページの高さを取得
total_height = driver.execute_script("return doc...
# ウィンドウサイズをページの高さに合わせる
driver.set_window_size(1920, total_height)
take_screenshot(self, test_method_name)
def tearDown(self):
# ブラウザを閉じる
self.driver.quit()
** テストベースを利用したテストケース [#n8ed68a5]
テストベースを利用することで、テストで使う共通処理を再利...
test_yahoo_search.py
from test_base import TestBase
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditio...
class YahooSearchTest(TestBase):
def test_search_in_yahoo_japan(self):
# Yahoo Japanのページにアクセス
self.driver.get('https://www.yahoo.co.jp/')
# 検索ボックスを見つけて、キーワードを入力して検...
search_box = self.driver.find_element(By.NAME, '...
search_box.send_keys('Python')
search_box.send_keys(Keys.RETURN)
# 検索結果が表示されるのを待機
WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.CLASS_NAM...
)
# スクリーンショットを取得して保存
self.take_screenshot_allheight(self.__class__.__...
# その他のテストコード...
# その他のテストケース...
* git bashで文字化けする場合の対応方法 [#t2ebd4ab]
pythonをgitbashで使うと、windowsの場合は文字化けしていが...
&ref(スクリーンショット 2023-12-10 115206.png);
&ref(スクリーンショット 2023-12-10 115425.png);
ページ名: