-
Notifications
You must be signed in to change notification settings - Fork 3
/
iphone_rusher.rb
138 lines (118 loc) · 4.78 KB
/
iphone_rusher.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
require "selenium-webdriver"
require "date"
class IphoneRusher
attr_reader :driver, :wait
def initialize
# configure the driver
options = Selenium::WebDriver::Chrome::Options.new
# options.add_argument('--headless')
@driver = Selenium::WebDriver.for :chrome, options: options
@wait = Selenium::WebDriver::Wait.new(:timeout => 20)
end
def rush
login
add_to_cart
purchase
refresh_until
end
def login
# 上苹果官网
driver.navigate.to "https://apple.com.cn"
# 点购物袋
driver.find_element(xpath: '//*[@id="ac-gn-bag"]/div/a').click
wait.until { driver.find_element(xpath: '//*[@id="ac-gn-bagview-content"]/nav/ul/li[5]/a') }
if element_exist?(xpath: '//*[@id="ac-gn-bagview-content"]/nav/ul/li[5]/a')
# 点登录
driver.find_element(xpath: '//*[@id="ac-gn-bagview-content"]/nav/ul/li[5]/a').click
input_account_and_click_login_button
end
if login_failed?
if element_exist?(xpath: '//*[@id="bag-content"]/div/div[2]/div/div[1]/a')
driver.find_element(xpath: '//*[@id="bag-content"]/div/div[2]/div/div[1]/a').click
end
input_account_and_click_login_button
end
end
def add_to_cart
wait.until { driver.find_element(xpath: '//*[@id="ac-gn-bag"]') }
driver.find_element(xpath: '//*[@id="ac-gn-bag"]').click
wait.until { driver.find_element(css: '#ac-gn-bag') }
driver.find_element(css: '#ac-gn-bag').click
end
def input_account_and_click_login_button
wait.until { driver.find_element(xpath: '//*[@id="signIn.customerLogin.appleId"]') }
# 如果没有用户名
if driver.find_element(xpath: '//*[@id="signIn.customerLogin.appleId"]').attribute('value').empty?
# 输入用户名
driver.find_element(xpath: '//*[@id="signIn.customerLogin.appleId"]').send_keys '<your_apple_id>'
end
# 输入密码
driver.find_element(xpath: '//*[@id="signIn.customerLogin.password"]').send_keys '<your_password>'
# 点击登录
driver.find_element(xpath: '//*[@id="signin-submit-button"]').click
sleep 5
end
def jump_to_signin_path?
driver.current_url.include?('shop/signIn')
end
def login_failed?
driver.current_url.include?('shop/bag')
end
def refresh_page
driver.navigate.refresh
end
def purchase
wait.until { driver.find_element(css: '#ac-gn-bag') }
driver.find_element(css: '#ac-gn-bag').click
wait.until { driver.find_element(xpath: '//*[@id="ac-gn-bagview-content"]/a') }
driver.find_element(xpath: '//*[@id="ac-gn-bagview-content"]/a').click
driver.navigate.to 'https://secure4.www.apple.com.cn/shop/checkout/start'
if element_exist?(xpath: '//*[@id="shoppingCart.actions.checkout"]')
driver.find_element(xpath: '//*[@id="shoppingCart.actions.checkout"]').click
else
wait.until { driver.find_element(xpath: '//*[@id="signIn.customerLogin.password"]') }
driver.find_element(xpath: '//*[@id="signIn.customerLogin.password"]').send_keys '<your_password>'
driver.find_element(xpath: '//*[@id="signin-submit-button"]').click
sleep 5
end
wait.until { driver.find_element(css: '#fulfillmentOptionButtonGroup1') }
sleep 5
driver.find_element(xpath: '//*[@id="checkout-container"]/div/div[6]/div[1]/div[2]/div/div/div[1]/div/div/div/fieldset/div[1]/div[2]').click
end
def refresh_until
while !any_store_selectable?
refresh_page
end
select_store
end
def any_store_selectable?
!element_exist?(css: '.is-error') && element_exist?(css: '.as-storelocator-searchresultlist .as-storelocator-searchitem')
end
def element_exist?(xpath: nil, css: nil)
begin
if xpath
driver.find_element(xpath: xpath)
elsif css
driver.find_element(css: css)
end
rescue Selenium::WebDriver::Error::NoSuchElementError
return false
end
end
def select_store
driver.find_elements(css: '.as-storelocator-searchresultlist .as-storelocator-searchitem').first.click
drop = driver.find_element(id: "#{Date.today.to_s}timeWindows")
choose = Selenium::WebDriver::Support::Select.new(drop)
choose.select_by(:index, 2)
driver.find_element(id: 'rs-checkout-continue-button-bottom').click
wait.until { driver.page_source.include?('你的身份证或护照号') }
driver.find_element(name: 'nationalId').send_keys '<your_id_num>'
driver.find_element(id: 'rs-checkout-continue-button-bottom').click
wait.until { driver.find_element(id: 'checkout.billing.billingOptions.options.2') }
driver.find_element(id: 'checkout.billing.billingOptions.options.2').click
driver.find_element(id: 'rs-checkout-continue-button-bottom').click
wait.until {driver.find_element(css: '.rs-review-header-text')}
driver.find_element(id: 'rs-checkout-continue-button-bottom').click
end
end
IphoneRusher.new.rush