【Python】唯品会购买商品(未完待续……)

# coding=utf-8
from selenium import webdriver
from time import sleep
import keyword
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support.ui import Select
import csv
import random
import io

#滚动滚动条`
def MoveScorllBar(driver,down,range1,range2):
moneCount=random.randint(range1, range2)
for num in range(1,moneCount):
if(down=="down"):
driver.find_element_by_xpath("//body").send_keys(Keys.DOWN)
sleep(random.uniform(0.1,0.5))
else:
driver.find_element_by_xpath("//body").send_keys(Keys.UP)
sleep(random.uniform(0.1,0.5))
return
#弹出新的浏览器窗口后,将之后的操作指向新的浏览器窗口
def SwitchWindow():
windows = driver.window_handles
driver.switch_to_window(windows[1])
if __name__=="__main__":
a = '耐克'
num = '3'
driver = webdriver.Chrome()
driver.get("https://www.vip.com")
MoveScorllBar(driver, "down", 10, 30)
MoveScorllBar(driver, "up", 10, 30)
driver.find_element_by_xpath("// *[ @ id = 'J-search'] / div[1] / input").send_keys(a)
driver.find_element_by_xpath("// *[ @ id = 'J-search'] / div[1] / a / span").click()
MoveScorllBar(driver, "down", 10, 30)
#选择商品
driver.find_element_by_css_selector('section#J_searchCatList div:nth-child(2)').click()
SwitchWindow()
MoveScorllBar(driver, "down", 10, 30)
MoveScorllBar(driver, "up", 10, 30)
driver.find_element_by_xpath("//*[@id='J-cartAdd-sizeID-1612349898']").click()

如果class中含有disable那么继续循环,如果不包含disabled那么break循环,执行选中,然后输入购买数量后点击购买。
site = ' http://www.outofmemory.cn/'
if "sharejs"  in site:    
 
          print('site contains sharejs') 

driver.find_element_by_xpath("//*[@id='J-num-select'']/dd[1]/em").send_keys(num)#修正后的代码,xpath后()中双引号("")里面不能套用双引号(""),把里面的双引号改成单引号('')报错就没有了。
#driver.find_element_by_class_name("amount num-input J-pro-num-txt").send_keys(num)


猜你喜欢

转载自www.cnblogs.com/zhuzhubaoya/p/9286311.html