Created
October 8, 2017 01:14
-
-
Save dev001hajipro/6ecbe6460475f5d00bcda254f89b9867 to your computer and use it in GitHub Desktop.
Python3でseleniumの簡単な操作。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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