My blogs

The greatest test of courage on earth is to bear defeat without losing heart.

0%

Selenium(二)

利用 Selenium,玩小遊戲(測試)。
紀錄筆記

提示 : 以上僅作為練習測試,而非惡意破壞遊戲體驗。

  • 聲明
    如在它站利用 Selenium,被判罰或停權等等。
    一概須自行負責!

TSJ 遊戲

  • TSJ 是?
    這邊就不詳細說明,之前很紅,有興趣可自行搜詢!

TSJ 遊戲介紹

簡單滑鼠點擊遊戲,持續點擊來增加經驗點,可以購買道具加快累積經驗點,隨著次數累積解所成就!
建議可以先去察看遊戲介面,後續較好了解。
=>官方


Coding 解說

安裝

示範於 作業系統:windows 瀏覽器:Chrome
需先安裝:

  • selenium
    • 使 pip install selenium
  • webdriver
    • 需先查看瀏覽器版本

step1: 打開 Chrome 找到自訂及管理
step2: 找到說明點擊
step3: 找到關於Google Chrome點擊
step4: 查看版本
step4: 下載對應版本 ChromeDriver


載入套件
1
2
3
4
from selenium import webdriver
# import Action chains
from selenium.webdriver.common.action_chains import ActionChains

程式透過呼叫 WebDriver 來直接對瀏覽器進行操作。
WebDriver 只是一個 API 介面,實作則決定於所選用的瀏覽器 driver(本篇是以 Google Chrome)

ActionChains 是一種自動化低級交互的方法,例如鼠標移動、鼠標按鈕操作、按鍵和上下文菜單交互。這對於執行更複雜的操作(例如懸停和拖放)非常有用。
詳細內容參考至官方文件 7.2 節
=>官方文件


定義
1
2
3
4
path = ("you chromedriver.exe path ")
driver = webdriver.Chrome(path)
driver.get("https://tsj.tw/")

提示 : 如是在檔案總管複製路徑需將\換成 /否者會出錯

把 chromedriver 檔案路徑傳入,由於執行瀏覽器需chromedriver.exe執行檔。

driver.get()表示指定要去的網址


抓取所要控制區塊

提示 : 如日後網頁html中內容有修改就會無法正常執行

1
2
3
4
5
blow = driver.find_element_by_id("click")
blow_count = driver.find_element_by_xpath(
'//*[@id="app"]/div[2]/div[4]/div[2]/h4[2]')
print(blow_count.text)

driver.find_element_by_id("click")取得 html 上 class 標籤為 click,取得到點擊增加經驗點的按鈕。

driver.find_element_by_xpath()取得 html 上 xpath 位置,取得到有多經驗點的文字。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
items = []
items.append(driver.find_element_by_xpath(
'//*[@id="app"]/div[2]/div[4]/div[4]/table/tbody/tr[4]/td[5]/button[1]/i'))
items.append(driver.find_element_by_xpath(
'//*[@id="app"]/div[2]/div[4]/div[4]/table/tbody/tr[3]/td[5]/button[1]/i'))
items.append(driver.find_element_by_xpath(
'//*[@id="app"]/div[2]/div[4]/div[4]/table/tbody/tr[2]/td[5]/button[1]/i'))
prices = []
prices.append(driver.find_element_by_xpath(
'//*[@id="app"]/div[2]/div[4]/div[4]/table/tbody/tr[4]/td[4]'))
prices.append(driver.find_element_by_xpath(
'//*[@id="app"]/div[2]/div[4]/div[4]/table/tbody/tr[3]/td[4]'))
prices.append(driver.find_element_by_xpath(
'//*[@id="app"]/div[2]/div[4]/div[4]/table/tbody/tr[2]/td[4]'))

items 為陣列存放我們三個技能點的位置。
price 為陣列存放我們三個技能價格點的位置

step1: 打開 網站首頁,點擊滑鼠右鍵。連結 官方
step2: 找到檢查點擊


  • 點選左上藍色箭頭

範例


  • 拉藍色箭頭點到所要的按鈕

範例


由此可查看到Html標籤,對應 class 或是 id 等等。

就能查看到要控制的按鈕。

取得xpath,點選 上圖藍色選取線點選右鍵 => 找到copy點擊 => 找到 copyXPath點擊 。

複製 xpath 範例格式:

1
'//*[@id="app"]/div[2]/div[4]/div[4]/table/tbody/tr[4]/td[5]/button[1]/i'

driver.find_element_by_xpath()取得 html 上 xpath 位置


動作
1
2
3
actions = ActionChains(driver)
actions.click(blow)

  • 啟用ActionChains

  • actions.click(blow) click 為點擊動作。


1
2
3
4
5
6
7
8
9
10
11
12
13
while True:

actions.perform()
count = (int(blow_count.text.replace("您目前擁有", "").replace("技術點", "")))
for j in range(3):
price = int(prices[j].text.replace("您目前擁有", "").replace("技術點", ""))
if price <= count:
upgrade_action = ActionChains(driver)
upgrade_action.move_to_element(items[j])
upgrade_action.click()
upgrade_action.perform()
break

  • actions.perform()加上才會執行 actions 動作。

  • count = (int(blow_count.text.replace("您目前擁有", "").replace("技術點", "")))
    取得文字您目前擁有 x 技術點,需要取得數字來判斷,把文字部分替代為空字串,在轉換型態int來做運算。


共有三個技能欄位,所以用for來回執行,輪流購買。

move_to_element(element) ——鼠標移動元素上。

upgrade_action.click() ——鼠標點擊。

upgrade_action.perform() ——執行所有動作。

PS : 最後須加上 break 否則會跑掉另一欄位點擊(此時經驗點已不足),所以當滿足條件執行完就 break 跳出。


Popcat

Popcat 遊戲介紹

簡單滑鼠點擊遊戲,持續點擊累積分數。

這個最近算是很紅~
=>官方


Coding

1
2
3
4
5
6
7
8
9
10
11
12
13
14
from selenium import webdriver
# import Action chains
from selenium.webdriver.common.action_chains import ActionChains
import time

path = ("you chromedriver.exe path")
driver = webdriver.Chrome(path)
driver.get("https://popcat.click/")
pop = driver.find_element_by_xpath('//*[@id="app"]/div')

actions = ActionChains(driver)
actions.click(pop)
while True:
actions.perform()

結語

基本上,能在靜態小遊戲測試,無法在線上遊戲中使用會被抓。


參考教學


如果您喜歡我的文章,請幫我按五下 ,感謝大家。