Bye Bye Moore

PoCソルジャーな零細事業主が作業メモを残すブログ

Ubuntu 20.10 on Raspberry Pi 4B にSeleniumの環境を構築してスクレイピング その3:クリックしてダウンロードする

続きものの本丸。
APIなり、あるいはダウンロードキーでも入れてくれてれば楽なのに
今回のターゲットはログインしてボタンクリックしないとダウンロードできない仕様です。
実にダルいので、こいつを何とかやっつけたいと思います。

実際のところ

ボタンを押して展開する蛇腹ニューから、特定の領域をクリックしてファイルをダウンロードする。

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains

#....

print("NextPage....\r\n")
driver.get('TARGET URL')
print(driver.get_window_rect())

#マウスの挙動を再現するActionChainsのインスタンス
actions = ActionChains(driver)

# 参考元をベースにhtmlタグをベースに
targetElement = driver.find_element_by_tag_name('html')
# 左上を基準点に。move_to_elementでは重心になる。
actions.move_to_element_with_offset(targetElement,0,0)
# target_x、target_yの分だけ移動
actions.move_by_offset(target_x,target_y)
#左クリック。右クリックはcontext_click、ダブルクリックはdouble_click
actions.click()
# スタックしたコマンド群を実行
actions.perform()

# 展開した蛇腹メニューからdownload_item_yだけ動いてダウンロード
actions.move_by_offset(0,download_item_y)
actions.click()
actions.perform()

# インスタンスを片付ける。整理整頓は基本。
driver.quit()