Day 48 - Selenium & Cookie Click Game


Posted by pei_______ on 2022-05-30

learning from 100 Days of Code: The Complete Python Pro Bootcamp for 2022


Selenium Documentation


導入常用Selenium模組及web driver

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import StaleElementReferenceException
import time

chrome_driver_path = "D:\chrome_driver\chromedriver.exe"
driver = webdriver.Chrome(executable_path=chrome_driver_path)

轉換與關閉控制頁面

# 第一個開啟的頁面
base_window = driver.window_handles[0]

# 第二個開啟的頁面
login_window = driver.window_handles[1]

# 轉換控制頁面
driver.switch_to.window(login_window)
print(driver.title)

# 關閉單一頁面
driver.close()

# 關閉此project中的所有頁面
driver.quit()

以python.org為目標

driver.get("https://www.python.org/")

# 以X-path選擇物件,抓取物件尺寸以及href屬性內容(即連結)
wiki_link = driver.find_element(By.XPATH, '//*[@id="container"]/li[4]/ul/li[9]/a')
print(search_bar.size)
print(link.get_attribute("href"))

# 以CSS_selector選擇物件,抓取表格內容並儲存成dictionary
dates = driver.find_elements(By.CSS_SELECTOR, ".event-widget .menu time")
events = driver.find_elements(By.CSS_SELECTOR, ".event-widget .menu a")
event_dic = {}

for index in range(0, len(dates)):
    event_dic[index] = {'time': dates[index].text, 'name': events[index].text}

print(event_dic)

以wikipedia為目標

driver.get("https://en.wikipedia.org/wiki/Main_Page")

# 以連結文字選擇物件,並自動點擊
Content_portals  = driver.find_element(By.LINK_TEXT, "Content portals")
Content_portals.click()

# 以name選擇物件,自動輸入搜尋文字並送出
search = driver.find_element(By.NAME, "search")
search.send_keys("Python")
search.send_keys(Keys.ENTER)

以(教學用)登入頁面為目標

driver.get("http://secure-retreat-92358.herokuapp.com/")

f_name = driver.find_element(By.NAME, "fName")
l_name = driver.find_element(By.NAME, "lName")
email = driver.find_element(By.NAME, "email")
btn = driver.find_element(By.CLASS_NAME, "btn")

f_name.send_keys("Lee")
l_name.send_keys("Penny")
email.send_keys("example@gmail.com")
btn.send_keys(Keys.ENTER)

Cookie Click Game

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import StaleElementReferenceException
import time

chrome_driver_path = "D:\chrome_driver\chromedriver.exe"
driver = webdriver.Chrome(executable_path=chrome_driver_path)

driver.get(url="http://orteil.dashnet.org/experiments/cookie/")


def check_money():
    return int(driver.find_element(By.ID, "money").text)


def print_score():
    print(driver.find_element(By.ID, "cps").text.split()[-1])


def buy_item():
    store_prices = driver.find_elements(By.CSS_SELECTOR, "#store b")
    try:
        for index in range(len(store_prices)-2, -1, -1):
            price = int(store_prices[index].text.split()[-1].replace(",", ""))
            if check_money() >= price:
                store_prices[index].click()
                break
    except StaleElementReferenceException as e:
        print(e)


is_clicking = True

time_out = time.time() + 5 * 60
time_after_5_sec = time.time() + 5

while is_clicking:
    cookie = driver.find_element(By.CSS_SELECTOR, "#cookie")
    cookie.click()

    if time.time() > time_after_5_sec:
        buy_item()
        time_after_5_sec = time.time() + 5

    if time.time() > time_out:
        print_score()
        is_clicking = False

#Python #課堂筆記 #100 Days of Code







Related Posts

第一章:4 策略模組化

第一章:4 策略模組化

接收來自 ROS Topic 的影像並偵測畫面中的動作

接收來自 ROS Topic 的影像並偵測畫面中的動作

MTR04 W2 D17 陣列練習題

MTR04 W2 D17 陣列練習題


Comments