"python-wp自动下单脚本"
# Header
import time
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.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
# 设置循环次数
test_runs = 30
# 站点url
URL_GET = "http://user-center.te/system/select"
URL_WORDPRESS = "http://testee07.te/?post_type=product"
# 人物设定
USER_FIRST_NAME = "Myrle"
USER_LAST_NAME = "Johnson"
USER_ADDRESS = "3710 w st germain Apt 322 St cloud"
USER_EMAIL_ADDRESS = "hendrosainemt@hotmail.com"
USER_POSTAL_CODE = "123456"
USER_PHONE_NUMBER = "5555555555"
USER_CITY = "beijing"
# 操作定义
TYPE_BUTTON = 'BUTTON'
TYPE_INPUT = 'INPUT'
TYPE_TRY_BUTTON_ACTION = 'TYPE_TRY_BUTTON_ACTION'
TYPE_WAIT_SIGN = 'WAIT_SIGN'
def range_turn(num, callback):
for _ in range(num):
options = Options()
# 如果要添加代理
# options.add_argument('--proxy-server=http://123.45.67.89:1234')
callback(options)
def start_on(scheme, url):
range_turn(test_runs, lambda options: FakeMan(url, options).start(scheme))
def main():
# start_on(scheme_test, URL_GET)
start_on(scheme_order, URL_WORDPRESS)
def scheme_order(faked):
exec_by_config([
# 等待并点击按钮chart
{'type': TYPE_BUTTON, 'xpath': "//*[@id=\"wp--skip-link--target\"]/div[4]/ul/li/div[3]/button/span",
'content': ""},
# 点击购物车
{'type': TYPE_BUTTON, 'xpath': "/html/body/div[1]/header/div/div[1]/div[2]/div/div[2]/button",
'content': ""},
# 点击goto checkout
{'type': TYPE_BUTTON, 'xpath': "/html/body/div[4]/div/div/div/div/div[2]/div[4]/div[2]/a[2]",
'content': ""},
# 输入email address
{'type': TYPE_INPUT, 'xpath': "//*[@id=\"email\"]",
'content': USER_EMAIL_ADDRESS},
# 输入FIRSTNAME
{'type': TYPE_INPUT, 'xpath': "//*[@id=\"shipping-first_name\"]",
'content': USER_FIRST_NAME},
# 输入LAST NAME
{'type': TYPE_INPUT, 'xpath': "//*[@id=\"shipping-last_name\"]",
'content': USER_LAST_NAME},
# 输入address
{'type': TYPE_INPUT, 'xpath': "//*[@id=\"shipping-address_1\"]",
'content': USER_ADDRESS},
# 输入POST CODE
{'type': TYPE_INPUT, 'xpath': "//*[@id=\"shipping-postcode\"]",
'content': USER_POSTAL_CODE},
# 输入city
{'type': TYPE_INPUT, 'xpath': "//*[@id=\"shipping-city\"]",
'content': USER_CITY},
# 输入phone
{'type': TYPE_INPUT, 'xpath': "//*[@id=\"shipping-phone\"]",
'content': USER_PHONE_NUMBER},
# 点击 CASH ON DELIVERY //*[@id="radio-control-wc-payment-method-options-cod"]
{'type': TYPE_BUTTON, 'xpath': "//*[@id=\"radio-control-wc-payment-method-options-cod\"]",
'content': USER_POSTAL_CODE},
# 等待3秒
{'type': TYPE_WAIT_SIGN,
'xpath': "//*[@id=\"wp--skip-link--target\"]/div[2]/div/div[4]/div[2]/form/div[6]/div[3]/button",
'content': 3},
# 点击下单,,//*[@id="wp--skip-link--target"]/div[2]/div/div[4]/div[2]/form/div[6]/div[3]/button
{'type': TYPE_BUTTON, 'xpath': "//*[@id=\"wp--skip-link--target\"]/div[2]/div/div[4]/div[2]/form/div[6]/div[3]/button",
'content': USER_POSTAL_CODE},
], faked)
def scheme_test(faked):
config = [
# 等待并点击按钮
{'type': TYPE_BUTTON, 'xpath': "/html/body/div/div[2]/form/div[5]/a", 'content': ""},
# 等待并输入用户名
{'type': TYPE_INPUT, 'xpath': "//*[@id=\"email\"]", 'content': "adver"},
# 等待并输入密码
{'type': TYPE_INPUT, 'xpath': "//*[@id=\"password\"]", 'content': "adver"},
# 等待并点击登录按钮
{'type': TYPE_BUTTON, 'xpath': "/html/body/div/div[2]/form/div[4]/button", 'content': ""},
# 等待并处理可能的二次确认登录
{'type': TYPE_TRY_BUTTON_ACTION, 'xpath': "/html/body/div/div[2]/form/div[4]/button", 'content': ""},
# 等待并点击预登出按钮
{'type': TYPE_BUTTON, 'xpath': "/html/body/div/nav/div[1]/div/div[2]/div/div[1]/button", 'content': ""},
# 等待并点击登出按钮
{'type': TYPE_BUTTON, 'xpath': "/html/body/div/nav/div[1]/div/div[2]/div/div[2]/div/form/a", 'content': ""},
]
exec_by_config(config, faked)
def exec_by_config(config, faked):
for item in config:
if item['type'] == TYPE_BUTTON:
faked.button_click(item['xpath'])
continue
if item['type'] == TYPE_INPUT:
faked.input_text(item['xpath'], item['content'])
continue
if item['type'] == TYPE_TRY_BUTTON_ACTION:
faked.try_button_click(item['xpath'])
continue
if item['type'] == TYPE_WAIT_SIGN:
time.sleep(item['content'])
continue
return 0
class FakeMan:
# 类的构造函数或初始化方法
def __init__(self, url, options):
self.drive = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
self.url = url
def button_click(self, locator, timeout=10):
w_button = WebDriverWait(self.drive, timeout).until(
EC.element_to_be_clickable((By.XPATH, locator))
)
w_button.click()
def try_button_click(self, locator, timeout=5):
try:
w_button = WebDriverWait(self.drive, timeout).until(
EC.element_to_be_clickable((By.XPATH, locator))
)
w_button.click()
except Exception as e:
print(f"发生了一个错误:{str(e)}")
def input_text(self, locator, send, timeout=10):
w_input = WebDriverWait(self.drive, timeout).until(
EC.presence_of_element_located((By.XPATH, locator))
)
w_input.send_keys(send)
def get(self):
self.drive.get(self.url)
def quit(self):
self.drive.quit()
def start(self, callback):
self.get()
callback(self)
time.sleep(20)
self.quit()
main()
# xpath归档
//*[@id="payment-method"]/div[2]/div/div[3]/div[2]/div/div/div/div/iframe
xpath | content | site |
---|---|---|
//*[@id="payment-method"]/div[2]/div/div[3]/div[2]/div/div/div/div/iframe | wp站点无法输入iframe | https://sinceritysparkly.com/checkout/ |
| 更多文本 | 更多文本 | 更多文本 |