selenium实战(淘宝购物车删除)

    科技2022-08-13  91

    这里写目录标题

    因为网速的问题,停止时间多了一些,网好的朋友可以把有些时间向下调整

    因为网速的问题,停止时间多了一些,网好的朋友可以把有些时间向下调整

    from selenium import webdriver import time # 选择浏览器 driver = webdriver.Firefox() # 输入网址 url = "http://www.baidu.com" driver.get(url) # 网址输入栏输入 文字 shuru = driver.find_element_by_id('kw').send_keys('淘宝官网') # 点击搜索 baiduyixia = driver.find_element_by_id('su').click() time.sleep(1) # 点击需要的网页 用查找文字的方法来查找 jinru = driver.find_element_by_link_text(u"淘宝网 - 淘!我喜欢").click() # 切换页面 这是当前打开的第二个页面 下标为1 haunjiemain1 = driver.switch_to.window(driver.window_handles[1]) time.sleep(1) # 进入淘宝 先登录 qing_login = driver.find_element_by_link_text(u'亲,请登录').click() time.sleep(1) # 进入登录页面 输入用户名和密码 username = driver.find_element_by_id('fm-login-id').send_keys('17758514492') password = driver.find_element_by_id('fm-login-password').send_keys('nidesheng.') time.sleep(1) # 点击登录 login = driver.find_element_by_xpath("//button[@type='submit']").click() time.sleep(30) # 进入淘宝页面搜索男装 nanzhuang = driver.find_element_by_id('q').send_keys('男装') time.sleep(1) # 点击搜索 sousuo = driver.find_element_by_class_name('btn-search').click() time.sleep(5) # 选择分类 hengyuanxiang = driver.find_element_by_link_text('恒源祥').click() time.sleep(3) # 输入最小的价格 min=driver.find_element_by_xpath('//li[1]//input[1]').send_keys('100') time.sleep(2) # 输入最大价格 max=driver.find_element_by_xpath('//li[3]//input[1]').send_keys('500') time.sleep(2) # 点击确定 queren=driver.find_element_by_xpath("//button[@class='J_SortbarPriceSubmit btn']").click() time.sleep(2) # 点击搜索出来的商品的第一个 xiku=driver.find_element_by_xpath('/html[1]/body[1]/div[1]/div[2]/div[3]/div[1]/div[21]/div[1]/div[1]/div[1]/div[1]').click() time.sleep(5) # 打印一下,这个页面的下标 print(driver.window_handles) haunjiemain2=driver.switch_to.window(driver.window_handles[2]) # 进入这个页面后 选择尺寸 chima=driver.find_element_by_xpath("//div[contains(@class,'tb-key')]//dl[1]//dd[1]//ul[1]//li[1]//a[1]").click() time.sleep(1) # 选择颜色 yanse=driver.find_element_by_xpath("//dl[contains(@class,'tb-prop tm-sale-prop tm-clear tm-img-prop')]//li[1]//a[1]//span[1]").click() time.sleep(1) # 加入购物车 cat=driver.find_element_by_css_selector('#J_LinkBasket').click() time.sleep(1) # 进入购物车 jincat=driver.find_element_by_xpath('//a[@class="sn-cart-link"]').click() time.sleep(1) # 删除商品 shancat=driver.find_element_by_xpath("//a[@class='J_Del J_MakePoint']").click() time.sleep(1) # 确定删除 queren2=driver.find_element_by_xpath("//a[@class='dialog-btn J_DialogConfirmBtn']").click() # driver.switch_to.alert.accept() time.sleep(3) driver.quit()
    Processed: 0.041, SQL: 8