Skip to content

Instantly share code, notes, and snippets.

@kurozumi
Created March 11, 2016 12:20
Show Gist options
  • Save kurozumi/0906ac7c3152efae2539 to your computer and use it in GitHub Desktop.
Save kurozumi/0906ac7c3152efae2539 to your computer and use it in GitHub Desktop.
【Python】Seleniumを使ってGoogle Search ConsoleのFetch as Googleに自動で登録する方法
# coding: utf-8
from selenium import webdriver
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_conditions as EC
import time, csv, os, re
driver = webdriver.Firefox()
driver.implicitly_wait(10)
# Google Search Consoleへ
driver.get("https://www.google.com/webmasters/tools/home?hl=ja")
# メールアドレスを入力
email = driver.find_element_by_name("Email")
email.send_keys("email")
email.send_keys(Keys.RETURN)
# パスワードを入力
passwd = driver.find_element_by_name("Passwd")
passwd.send_keys("password")
passwd.send_keys(Keys.RETURN)
# ドメイン選択
elem = driver.find_element_by_link_text("a-zumi.net")
elem.click()
# 左メニューのクロールをクリック
elem = driver.find_element_by_xpath("//*[text() = 'クロール']")
elem.click()
# Fetch as Googleをクリック
elem = driver.find_element_by_link_text(u"Fetch as Google")
elem.click()
# ここからURLの登録開始
urls = [
"http://a-zumi.net/1",
"http://a-zumi.net/2",
"http://a-zumi.net/3",
"http://a-zumi.net/4",
"http://a-zumi.net/5"
]
for url in urls:
# ドメインを削除してフォームに入力
path = driver.find_element_by_name("path")
path.clear()
path.send_keys(url.replace("http://a-zumi.net/", ""))
# 取得ボタンをクリック
button = WebDriverWait(driver, 60).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "#wmxbot-submit-crawl-button div:not(.jfk-button-disabled)"))
)
button.click()
# インデックスに送信ボタンが表示されたらクリック(待機時間60秒)
button = WebDriverWait(driver, 60).until(
EC.presence_of_element_located((By.XPATH, "//*[text() = 'インデックスに送信']"))
)
button.click()
# 送信方法の選択ダイアログが表示されたら「このURLのみクロールする」をチェック(待機時間60秒)
button = WebDriverWait(driver, 60).until(
EC.presence_of_element_located((By.CSS_SELECTOR, ".verified-addurl-dialog"))
)
button.find_element_by_css_selector("input#verified-addurl-dialog-radio-url").click()
# reCAPTCHAをチェックして送信ボタンを押してダイアログが閉じられるまで待つ(待機時間60秒)
WebDriverWait(driver, 60).until(
EC.invisibility_of_element_located((By.CSS_SELECTOR, ".verified-addurl-dialog"))
)
# 次のURLへ
driver.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment