某掲示板の検索サイトを使用して、指定したスレッドを検索、その結果のURLを取得したいのですが下記のように「古い要素を参照している」とでてしまいます。
raise exception_class(message, screen, stacktrace) selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document (Session info: headless chrome=70.0.3538.77) (Driver info: chromedriver=2.43.600233 (523efee95e3d68b8719b3a1c83051aa63aa6b10d),platform=Linux 4.4.0-17134-Microsoft x86_64)
検索ページをロードするのに時間が足りてないのかと思い、コードの要素取得部分でWebDriverWaitを使ってみたのですが結果は変わりませんでした。
def get_url(title): options = Options() options.binary_location = '/usr/bin/google-chrome' options.add_argument('--headless') driver = webdriver.Chrome('chromedriver', chrome_options=options) driver.get('http://find.2ch.sc/') input_element = driver.find_elements_by_name('STR') input_element[0].send_keys(title) input_element[0].send_keys(Keys.RETURN) for a in driver.find_elements_by_css_selector('div.content_pane > nbsp > dl > dt > a'): url = a[0].get_attribute('href') return url
あなたの回答
tips
プレビュー