Flying Cat Penguin

ゆるゆる仕事、ソフトウェアテスティング関連のことについて綴ります。

「Learning Selenium Testing Tools with Python」(2014年)を読みながら、Seleniumを触る #1 (Selenium単体)

テスト自動化ツールを触る機会が巡ってきたので、体系的にやり方を学ぶためにカキカキ…。
ただ、python2系統だったのと、例題のサンプルページが新しくなっていてコードは全然動かないという…。
書籍を読みながら同じよなサンプルを実装していきます。

ちなみに…、下記のコードサンプルは、windows10の環境でのコードですので、Linux系統は別途カスタマイズしてください。

コードサンプル

[selenium_search_and_close.py]

from selenium import webdriver

if __name__ == "__main__":
    driver = webdriver.Chrome(executable_path=r'C:/tmp/selenium/chromedriver_win32/chromedriver.exe')

    driver.get('https://google.co.jp/')
    
    searchElement = driver.find_element_by_name("q")
    searchElement.send_keys('selenium')

    searchElement.submit()
    driver.quit()

「name」の要素をデベロッパーツールで見ても見つからない…。色んなHPにはあると書いてあるんだけどなぜ見つからず。動作するということは、たぶん要素があるはずなのだけど、納得できずに終えました…。

[selenium_search_and_output_results.py]

from selenium import webdriver

# create a new Chrome session
driver =webdriver.Chrome(executable_path=r'C:/tmp/selenium/chromedriver_win32/chromedriver.exe')
driver.implicitly_wait(30)
driver.maximize_window()

# navigate to page
driver.get("https://www.google.co.jp")

# get the search textbox
search_field = driver.find_element_by_name("q")
search_field.clear()

# enter search keyword and submit
search_field.send_keys("selenium")
search_field.submit()

# get all the anchor elements which have title and URL displayed
# currently on result page using find_elements_by_xpath method
for elem_h3 in driver.find_elements_by_xpath('//a/h3'):
    elem_a = elem_h3.find_element_by_xpath('..')  
    print(elem_h3.text.encode('cp932','replace'))
    print(elem_a.get_attribute('href')+'\n')

# close the browser window
driver.quit()

書籍のサンプルで初めて出てくるスクリプトをちょっとリファインして作成。検索した結果をコンソール上で出力できるようにして見た。xpathの指定方法はもう少しきちんと調べておかないと…。

ちなみに動かすとこんな感じ。

$ python searchproducts.py
b'10\x95\xaa\x82\xc5\x97\x9d\x89\xf0\x82\xb7\x82\xe9 Selenium - Qiita'
https://qiita.com/Chanmoro/items/9a3c86bb465c1cce738a

b'Selenium\x83u\x83\x89\x83E\x83U\x81[\x8e\xa9\x93\xae\x89\xbb\x83v\x83\x8d\x83W\x83F\x83N\x83g :: Selenium\x83h\x83L\x83\x85\x83\x81\x83\x93\x83g'
https://www.selenium.dev/documentation/ja/

b'Selenium (\x83\\\x83t\x83g\x83E\x83F\x83A) - Wikipedia'
https://ja.wikipedia.org/wiki/Selenium_(%E3%82%BD%E3%83%95%E3%83%88%E3%82%A6%E3%82%A7%E3%82%A2)

b'\x81y\x8a\xae\x91S\x94\xc5\x81zPython\x82\xc6Selenium\x82\xc5\x83u\x83\x89\x83E\x83U\x82\xf0\x8e\xa9\x93\xae\x91\x80\x8d\xec(\x83N\x83\x8d\x81[\x83\x8a\x83\x93\x83O ...'
https://tanuhack.com/selenium/

b'Python\x82\xc5Selenium\x82\xf0\x97\x98\x97p\x82\xb5\x82\xc4Web\x83T\x83C\x83g\x82\xcc\x83\x8d\x83O\x83C\x83\x93\x82\xf0\x8e\xa9\x93\xae\x89\xbb\x82\xb7\x82\xe9 ...'
https://techacademy.jp/magazine/28392

b'Web\x83u\x83\x89\x83E\x83U\x8e\xa9\x93\xae\x89\xbb\x83c\x81[\x83\x8b\x81uSelenium IDE\x81v\x82\xcc\x8d\xa1\x82\xdc\x82\xc5\x82\xc6\x82\xb1\x82\xea\x82\xa9\x82\xe7 ...'
https://www.valtes.co.jp/qbookplus/509

b'\x93\xfc\x96\xe5\x81ASelenium - Selenium\x82\xcc\x8ed\x91g\x82\xdd | CodeGrid'
https://app.codegrid.net/entry/selenium-1

b'[Selenium]\x83u\x83\x89\x83E\x83U\x82\xf0\x8e\xa9\x93\xae\x89\xbb\x82\xb5\x82\xc4\x83\x8b\x81[\x83e\x83B\x83\x93\x8d\xec\x8b\xc6\x82\xf0\x8c\x82\x91\xde \x8e\xc0\x91H\x95\xd2 ...'
http://ichannel.tokyo/technoracle/selenium%E3%83%96%E3%83%A9%E3%82%A6%E3%82%B6%E3%82%92%E8%87%AA%E5%8B%95%E5%8C%96%E3%81%97%E3%81%A6%E3%83%AB%E3%83%BC%E3%83%86%E3%82%A3%E3%83%B3%E4%BD%9C%E6%A5%AD%E3%82%92%E6%92%83%E9%80%80%E3%80%80/3999/

b'Selenium \x81~ Python\x82\xc5\x8a\xc7\x97\x9d\x89\xe6\x96\xca\x82\xd6\x82\xcc\x83\x8d\x83O\x83C\x83\x93\x82\xf0\x8e\xa9\x93\xae\x89\xbb\x82\xb5\x82\xc4\x82\xdd\x82\xbd ...'
https://liginc.co.jp/492266

b'Selenium with Python ? Selenium Python Bindings 2 ...'
https://selenium-python.readthedocs.io/

参考
SeleniumIDEとは - Qiita