Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Python3でseleniumの簡単な操作。
#!/usr/bin/env python
"""
seleniumをPython3で使う。
Pythonのseleniumの3.6.0では、FirefoxでもWebDriverが必要だった。昔は不要だった。
そのため、Chromeで試した。
--
chromeDriver - WebDriver for Chrome
https://sites.google.com/a/chromium.org/chromedriver/downloads
--
selenium 3.6.0
"""
from selenium import webdriver
import os
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
CHROME_DRIVER_PATH = 'C:\dev\chromedriver_win32\chromedriver.exe'
def main():
if not os.path.exists(CHROME_DRIVER_PATH):
print('not found web driver: %s' % CHROME_DRIVER_PATH)
return
driver = webdriver.Chrome(CHROME_DRIVER_PATH)
driver.get('http://inventwithpython.com')
try:
el = driver.find_element_by_class_name('bookcover')
print('Found <%s> element with that class name! ' % el.tag_name)
except NoSuchElementException as e:
print(e)
print('Was not able to find an element with that name.')
# ページをクリック
el_link = driver.find_element_by_link_text('Read It Online')
print(type(el_link))
el_link.click()
# キー操作
el_html = driver.find_element_by_tag_name('html')
for i in range(5):
el_html.send_keys(Keys.PAGE_DOWN)
driver.save_screenshot('hello_selenium_screen_shot.png')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.