前提・実現したいこと
SEOにおける上位サイト20位のキーワードとタイトル、URLを取得したい
タイトルをクリックして、戻って、次のタイトルをクリックして、戻ってを繰り返そうとしたところでエラーが出ました。
発生している問題・エラーメッセージ
wifi おすすめ Traceback (most recent call last): File "test2.py", line 55, in <module> i.click() File "/Users/kitayashun/.pyenv/versions/3.6.5/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py", line 80, in click self._execute(Command.CLICK_ELEMENT) File "/Users/kitayashun/.pyenv/versions/3.6.5/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py", line 633, in _execute return self._parent.execute(command, params) File "/Users/kitayashun/.pyenv/versions/3.6.5/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute self.error_handler.check_response(response) File "/Users/kitayashun/.pyenv/versions/3.6.5/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document (Session info: chrome=83.0.4103.97)
該当のソースコード
python
1#必要なライブラリのインポート 2from selenium import webdriver 3from selenium.webdriver.common.by import By 4import gspread 5import json 6from oauth2client.service_account import ServiceAccountCredentials 7import pandas as pd 8import os 9import time 10from selenium.webdriver.common.by import By 11from selenium.webdriver.support.ui import WebDriverWait 12from selenium.webdriver.support import expected_conditions as EC 13 14 15''' 16#スプレッドシートの設定 17scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive'] 18credentials = ServiceAccountCredentials.from_json_keyfile_name('spred-api.json', scope) 19gc = gspread.authorize(credentials) 20SPREADSHEET_KEY = '1vTyCfkkwrKaoogdZmicT7_V6eKaI1t3ZgChaiRGakXE' 21worksheet = gc.open_by_key(SPREADSHEET_KEY).sheet1 22 23 24 25import_value = int(worksheet.acell('A1').value) 26export_value = import_value+100 27worksheet.update_cell(1,2, export_value) 28''' 29 30#キーワードの検索 31driver = webdriver.Chrome('/usr/local/bin/chromedriver') 32driver.implicitly_wait(30) 33 34kws = ['wifi おすすめ','wimax 比較','クラウドwifi メリット'] 35 36driver.implicitly_wait(30) 37WebDriverWait(driver, 15).until(EC.presence_of_all_elements_located) 38 39#1ページ目の情報を取得 40for kw in kws: 41 driver.get('https://www.google.com/') 42 search_box = driver.find_element_by_name('q') 43 search_box.send_keys(kw) 44 search_box.submit() 45 #キーワード取得 46 keywords = kw 47 print(keywords) 48 #タイトル取得 49 h = driver.find_elements_by_class_name('LC20lb') 50 for i in h: 51 i.click() 52 driver.back() 53 54 55 56 57 58 59 60 61''' 62#2ページ目の情報を取得 63for kw in kws: 64 driver.get('https://www.google.com/') 65 search_box = driver.find_element_by_name('q') 66 search_box.send_keys(kw) 67 search_box.submit() 68 next = driver.find_element_by_link_text('次へ') 69 next.click() 70 #キーワード取得 71 keywords = kw 72 print(keywords) 73 #タイトル取得 74 h = driver.find_elements_by_class_name('LC20lb') 75 for i in h: 76 print(i.text) 77''' 78
試したこと
driver.implicitly_wait(30)を入れてみた
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/06/13 07:27