r-pointgtr/app/r_kuji.py

61 lines
2.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
import time,os
rkuji_list_url = "https://rakucoin.appspot.com/rakuten/kuji/"
rkuji_list_xpath = "//table/tbody/tr/td/a"
class class_rkuji:
def __init__(self, profile_dir="/tmp/profile"):
options = webdriver.ChromeOptions()
options.add_argument("--window-size=1920,1080")
options.add_argument("--user-data-dir=" + profile_dir )
self.driver = webdriver.Remote(
command_executor="http://selenium:4444/wd/hub",
options=options
)
def get_rkuji_urls(self, url, xpath):
self.driver.get(url)
self.urls = self.driver.find_elements(By.XPATH, xpath)
self.url_txts = [url.get_attribute("href") for url in self.urls ]
def challenge_rkuji(self):
for u in self.url_txts:
if 'redirect' in u:
print(f'ラッキーくじ:「{u}」はスキップ。(理由redirect)')
continue
try:
self.driver.get(u)
kuji_title = self.driver.title
except:
print(f'ラッキーくじ:「{u}」はスキップ。(理由:アクセス失敗)')
continue
if(len(self.driver.find_elements(By.ID, "entry"))>0):
print(f'ラッキーくじ:くじを引きます。({kuji_title})')
time.sleep(5)
try:
start_button = self.driver.find_element(By.ID, "entry")
start_button.click()
WebDriverWait(self.driver,60).until(EC.url_changes(u))
print(f'ラッキーくじ:くじを引きました。({kuji_title})')
self.driver.save_screenshot('./ss/r_kuji.png')
time.sleep(5)
except:
(f'ラッキーくじ:「{kuji_title}」はスキップ。(理由:くじ引き失敗)')
else:
print(f'ラッキーくじ:「{kuji_title}」はスキップ。(理由:くじ引き不能)')
def close(self):
self.driver.quit()
def rkuji():
driver = class_rkuji()
driver.get_rkuji_urls(rkuji_list_url, rkuji_list_xpath)
driver.challenge_rkuji()
driver.close()