selenim 自动登录淘宝 购物车里面添加商品

-- coding: utf-8 --

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re

class Taobao(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.base_url = “https://www.taobao.com/”
self.verificationErrors = []
self.accept_next_alert = True

def test_taobao(self):
    driver = self.driver
    driver.get(self.base_url + "/")
    driver.find_element_by_link_text(u"登录").click()

    driver.switch_to.window(driver.window_handles[1])
    页面切换

    driver.find_element_by_css_selector("#fm-login-id").send_keys("用户名*******")
    driver.find_element_by_id("fm-login-password").click()
    driver.find_element_by_id("fm-login-password").clear()
    driver.find_element_by_id("fm-login-password").send_keys("密码********")
    driver.find_element_by_xpath("//button[@type='submit']").click()
    driver.switch_to.window(driver.window_handles[1])
    
    driver.find_element_by_id("q").send_keys(u"裤子男")
  
    driver.find_element_by_class_name('btn-search').click()
    driver.find_element_by_css_selector('#J_NavCommonRowItems_0 > a:nth-child(3)').click()
    driver.find_element_by_id('J_Itemlist_PLink_614856040133').click()
    driver.find_element_by_xpath(".//*[@id='J_DetailMeta']/div[1]/div[1]/div/div[4]/div/div/dl[1]/dd/ul/li[2]").click()
    driver.find_element_by_xpath(".//*[@id='J_DetailMeta']/div[1]/div[1]/div/div[4]/div/div/dl[2]/dd/ul/li[1]").click()
    driver.find_element_by_id('J_LinkBasket').click()
    time.sleep(5)   
def is_element_present(self, how, what):
    try: self.driver.find_element(by=how, value=what)
    except NoSuchElementException as e: return False
    return True

def is_alert_present(self):
    try: self.driver.switch_to_alert()
    except NoAlertPresentException as e: return False
    return True

def close_alert_and_get_its_text(self):
    try:
        alert = self.driver.switch_to_alert()
        alert_text = alert.text
        if self.accept_next_alert:
            alert.accept()
        else:
            alert.dismiss()
        return alert_text
    finally: self.accept_next_alert = True

def tearDown(self):
    self.driver.quit()
    self.assertEqual([], self.verificationErrors)

if name == “main”:
unittest.main()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值