Skip to content

Instantly share code, notes, and snippets.

@yumaueno
Created August 11, 2020 00:23
Show Gist options
  • Save yumaueno/e5190585e0927c3b5e267355c1fa8feb to your computer and use it in GitHub Desktop.
Save yumaueno/e5190585e0927c3b5e267355c1fa8feb to your computer and use it in GitHub Desktop.
Seleniumを使って色々
from selenium import webdriver
import time
driver = webdriver.Chrome()
#Googleのブラウザを開く
driver.get('https://www.google.com/')
time.sleep(2)
#スタビジを検索
search_box = driver.find_element_by_name('q')
search_box.send_keys('スタビジ')
search_box.submit()
time.sleep(2)
#検索結果1位をクリック
g = driver.find_elements_by_class_name("g")[0]
r = g.find_element_by_class_name("r")
r.click()
#h2の要素を取得
for h2 in driver.find_elements_by_tag_name("h2"):
print(h2.text)
#画面キャプチャ
driver.maximize_window()
driver.save_screenshot('スタビジ.png')
#ブラウザを閉じる
driver.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment