示例#1
0
    def ajax_has_loaded(self):
        global global_ajax_flag_taobao
        xpath = '//html'
        print("begin")
        element = driver.find_element_by_xpath(xpath)
        s = str(element.get_attribute("class"))
        # print(s)

        #如果有这个 代表还没有加载结束 否则代表加载结束  看看有没有出错
        if (s.find("nprogress-busy") != -1):
            print("正在加载")

            self.flag = 1
            return False
        elif (self.flag == 1):
            xpath = '//*[@id="srp-error"]'
            element = driver.find_element_by_xpath(xpath)
            child_number = len(
                element.find_elements_by_xpath(
                    ".//*[contains(text(),'请刷新或重新点击尝试')]"))
            s = element.value_of_css_property("display")
            print(s)
            print(child_number)
            #if里面是出现错误的条件
            if child_number != 0 and s == 'block':
                #等待错误消失  因为出现错误到消失还要一点时间 他是一个动画
                wait_for(wait_wrong_taobao, 0.2, 30)
                #代表点击出错了 没有到下一页  而是出现 请刷新或重新点击尝试
                global_ajax_flag_taobao = 1

            #进入else 代表一定加载结束了
            return True
        else:
            return False
示例#2
0
def taobao_serach(name):
    url = "https://www.taobao.com";
    with wait_for_page_load(driver):
        driver.get(url);

    xpath = '//*[@id="q"]';
    wait.until(EC.visibility_of_element_located((By.XPATH,xpath)));
    driver.find_element_by_xpath(xpath).send_keys(name);

    xpath = '//*[@id="J_TSearchForm"]/div[1]/button';
    wait.until(EC.visibility_of_element_located((By.XPATH,xpath)));
    with wait_for_page_load(driver):
        driver.find_element_by_xpath(xpath).click();
示例#3
0
def loginagain():
    # 这个地方有可能会出现登录框  需要处理

    wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'mnl-ifr')))
    frame = driver.find_element_by_class_name(("mnl-ifr"))
    # frameInner = frame.find_element_by_tag_name("iframe");
    name = frame.get_attribute('id')
    print(name)
    driver.switch_to.frame(name)
    driver.find_element_by_xpath('//*[@id="TPL_username_1"]').send_keys(
        "15957120592")
    driver.find_element_by_xpath('//*[@id="TPL_password_1"]').send_keys(
        "yanli194612")
    # print(driver.find_element_by_xpath('//*[@id="nocaptcha"]'))

    # 等待滑块出现
    wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="nocaptcha"]')))
    driver.find_element_by_xpath('//*[@id="nocaptcha"]')
    huakuai1 = driver.find_element_by_xpath('//*[@id="_n1z"]')

    # 得到父节点 很重要
    print("huakuai parent")
    parent = huakuai1.find_element_by_xpath("..")
    # print(parent.tag_name);
    # print(parent.get_attribute("id"));

    # 得到宽度
    width = parent.value_of_css_property("width")
    # 上面会得到298px  下面就是去掉px这个字母
    width = str(width)[0:str(width).find("px")]
    print(width)

    ActionChains(driver).click_and_hold(huakuai1).move_by_offset(
        xoffset=width, yoffset=0).release(huakuai1).perform()

    wait.until(
        EC.visibility_of_element_located(
            (By.XPATH, '//*[@id="clickCaptcha"]/div[2]/img')))
    img = driver.find_element_by_xpath('//*[@id="clickCaptcha"]/div[2]/img')
    wenzi = driver.find_element_by_xpath('//*[@id="clickCaptcha"]/div[4]/p')

    #wenzi.text是要点击的内容
    print(wenzi.text)

    url = img.get_attribute("src")
    print(url)
    downjpg(url)
    """
    ***这个代表在图片的那个位置点击
    """
    # 这个用于测试  点击login的关闭按钮 有效  测试过了
    # ActionChains(driver).move_to_element_with_offset(img1,263,-106).click().perform();
    print("切换过")
示例#4
0
def goto_number_page(pagenumber):
    xpath = '//*[text()="下一页"]';
    wait.until(EC.visibility_of_element_located((By.XPATH,xpath)));
    element = driver.find_element_by_xpath(xpath);

    #查找输入框
    parent = get_element_parent(element);
    number = len(parent.find_elements_by_xpath('.//input'));
    while(number == 0):
        parent = get_element_parent(parent);
        number = len(parent.find_elements_by_xpath('.//input'));

    parent = parent.find_element_by_xpath('.//input')
    parent.clear();
    parent.send_keys(pagenumber)

    #查找确定按钮
    parent = get_element_parent(element);
    number = len(parent.find_elements_by_xpath(".//*[contains(text(),'确定')]"));
    while(number == 0):
        parent = get_element_parent(parent);
        number = len(parent.find_elements_by_xpath(".//*[contains(text(),'确定')]"));

    parent = parent.find_element_by_xpath(".//*[contains(text(),'确定')]");
    print("goto")
    with wait_for_ajax_load_taobao(driver):
        parent.click();
示例#5
0
def go_next_page():
    xpath = '//*[text()="下一页"]';
    wait.until(EC.visibility_of_element_located((By.XPATH,xpath)));
    next_page = driver.find_element_by_xpath(xpath);

    parent = next_page.find_element_by_xpath("..");
    # print("asdfasdf")
    print(parent.tag_name)
    # 如果可以点击的话 那么下一页的父节点是a标签
    if(parent.tag_name == 'a'):

        with  wait_for_ajax_load_taobao(driver):
            next_page.click();
        #刷单时间长了 淘宝在点击到下一页的时候会跳出登录框
        # time.sleep(2);
        # driver.switch_to.frame(driver.find_elements_by_tag_name("iframe")[0])
        # xpath = '//*[@id="TPL_username_1"]';
        # driver.find_element_by_xpath(xpath).send_keys("15957120592");
        #
        # xpath = '//*[@id="TPL_password_1"]';
        # driver.find_element_by_xpath(xpath).send_keys("yanli194612");
        # wait_for_ajax();


        return True;
    else:
        return False
示例#6
0
def loginagain():
    # 这个地方有可能会出现登录框  需要处理

    wait.until(EC.element_to_be_clickable((By.CLASS_NAME,'mnl-ifr')));
    frame=driver.find_element_by_class_name(("mnl-ifr"));
    # frameInner = frame.find_element_by_tag_name("iframe");
    name = frame.get_attribute('id');
    print(name);
    driver.switch_to.frame(name)
    driver.find_element_by_xpath('//*[@id="TPL_username_1"]').send_keys("15957120592");
    driver.find_element_by_xpath('//*[@id="TPL_password_1"]').send_keys("yanli194612");
    # print(driver.find_element_by_xpath('//*[@id="nocaptcha"]'))

    # 等待滑块出现
    wait.until(EC.element_to_be_clickable((By.XPATH,'//*[@id="nocaptcha"]')));
    driver.find_element_by_xpath('//*[@id="nocaptcha"]');
    huakuai1 = driver.find_element_by_xpath('//*[@id="_n1z"]');

     # 得到父节点 很重要
    print("huakuai parent")
    parent = huakuai1.find_element_by_xpath("..");
    # print(parent.tag_name);
    # print(parent.get_attribute("id"));

    # 得到宽度
    width = parent.value_of_css_property("width");
    # 上面会得到298px  下面就是去掉px这个字母
    width = str(width)[0:str(width).find("px")];
    print(width)

    ActionChains(driver).click_and_hold(huakuai1).move_by_offset(xoffset=width,yoffset=0).release(huakuai1).perform();

    wait.until(EC.visibility_of_element_located((By.XPATH,'//*[@id="clickCaptcha"]/div[2]/img')));
    img = driver.find_element_by_xpath('//*[@id="clickCaptcha"]/div[2]/img');
    wenzi = driver.find_element_by_xpath('//*[@id="clickCaptcha"]/div[4]/p');

    #wenzi.text是要点击的内容
    print(wenzi.text);

    url = img.get_attribute("src");
    print(url);
    downjpg(url);

    """
    ***这个代表在图片的那个位置点击
    """
    # 这个用于测试  点击login的关闭按钮 有效  测试过了
    # ActionChains(driver).move_to_element_with_offset(img1,263,-106).click().perform();
    print("切换过")
def _select_color():
    liList = _getAllLiList("颜色", "li")
    length = len(liList)
    flagList = checkClickable(liList)
    print(flagList)
    s = '//*[@id="J_BrandAttr"]/a/span'
    e = driver.find_element_by_xpath(s)
    print(e.location)
    print("*********************")
    selectOneLi(liList, flagList)
def find_goods():
    img_id = 'J_Itemlist_TLink_525569772774'

    xpath = '//*[@id="%s"]' % img_id
    # print(xpath)
    # wait.until(EC.visibility_of_element_located((By.XPATH,xpath)));
    try:
        goods = driver.find_element_by_xpath(xpath)
        return (True, goods)
    except NoSuchElementException as e:
        return (False, None)
示例#9
0
def get_total_page():
    xpath = '//*[text()="下一页"]';
    wait.until(EC.visibility_of_element_located((By.XPATH,xpath)));
    next_page = driver.find_element_by_xpath(xpath);

    parent = next_page.find_element_by_xpath("..");
    number = len(parent.find_elements_by_xpath(".//*[contains(text(),'共')]"))
    while number == 0:
        parent = parent.find_element_by_xpath("..");
        number = len(parent.find_elements_by_xpath(".//*[contains(text(),'共')]"))
    total = parent.find_element_by_xpath(".//*[contains(text(),'共')]")
    text = total.text;
    res = re.findall(r"\d{1,}",text);
    print("total")
    print(res);
    return int(res[0]);
示例#10
0
def get_current_page_number(digui_number):
    try:
        xpath = '//*[text()="下一页"]'
        wait.until(EC.visibility_of_element_located((By.XPATH, xpath)))
        next_page = driver.find_element_by_xpath(xpath)

        parent = get_element_parent(next_page)

        while parent.tag_name != 'li':
            parent = get_element_parent(parent)

        parent = get_element_parent(parent)
        # print(parent.tag_name);
        xpath = './/*[contains(@class,"active")]/span'
        current_number = parent.find_element_by_xpath(xpath).text
        # print("current_number");
        # print(current_number);
        return (current_number, digui_number)
    except StaleElementReferenceException as e:
        print("again")
        return get_current_page_number(digui_number + 1)
def _BuyNow():
    #加入购物车
    xpath = '//*[contains(text(),"加入购物车")]'
    wait.until(EC.element_to_be_clickable((By.XPATH, xpath)))
    element = driver.find_element_by_xpath(xpath)

    xpath = './/*[contains(text(),"立即购买")]'
    parent = get_element_parent(element)
    number = len(parent.find_elements_by_xpath(xpath))

    #flag 是用来判断往上走了多少层  因为立即购买一定与 加入购物车 有一个相同祖先  这个相同祖先一定不远
    flag = 5
    while number == 0 and flag >= 0:
        parent = get_element_parent(parent)
        number = len(
            parent.find_elements_by_xpath('.//*[contains(text(),"立即购买")]'))
        flag -= 1

    if (flag >= 0):
        (parent.find_element_by_xpath('.//*[contains(text(),"立即购买")]')
         ).click()
    else:
        raise CanotFindElement("加入购物车找不到")
示例#12
0
def login():

    driver.get("https://login.taobao.com/member/login.jhtml?redirectURL=https%3A%2F%2Fi.taobao.com%2Fmy_taobao.htm%3Fnekot%3DMTE5MzYxMDMyMnlhbmc%253D1447826354910");
    driver.maximize_window();
    # elem = driver.find_element_by_link_text("亲,请登录").click();
    # driver.implicitly_wait(5)
    wait.until(lambda driver:driver.find_element_by_id("TPL_username_1"));
    driver.find_element_by_id("TPL_username_1").send_keys("15957120592");
    wait.until(lambda driver:driver.find_element_by_id("TPL_password_1"));
    driver.find_element_by_id("TPL_password_1").send_keys("yanli194612");

    wait.until(lambda driver:driver.find_element_by_xpath('//*[@id="_scale_text"]'));
    kuang = driver.find_element_by_xpath('//*[@id="_scale_text"]');

    wait.until(lambda driver:driver.find_element_by_xpath('//*[@id="_n1z"]'));
    huakuai = driver.find_element_by_xpath('//*[@id="_n1z"]');

    # 得到父节点 很重要
    print("huakuai parent")
    parent = huakuai.find_element_by_xpath("..");
    # print(parent.tag_name);
    # print(parent.get_attribute("id"));

    width = parent.value_of_css_property("width");
    width = str(width)[0:str(width).find("px")];
    print(width)

    # print(driver.current_url)
    # driver.title 返回的是html中title 使用查看网页源代码就可以看得到
    # print(driver.title)

    # 获得parent 下的所有节点  必须使用".//*" 而不是//*
    # elems = parent.find_elements_by_xpath('.//*')
    # for i in elems:
    #     print(i.get_attribute("id"))

    #
    # parent = parent.find_element_by_xpath("..");
    # print(parent.tag_name);
    # print(parent.get_attribute("id"));

    # wait.until(lambda driver:driver.find_element_by_xpath('//*[@id="_bg"]'));
    # div = driver.find_element_by_xpath('//*[@id="_bg"]');

    ActionChains(driver).click_and_hold(huakuai).move_by_offset(xoffset=width,yoffset=0).release(huakuai).perform();
    # js = "document.getElementById(\"_bg\").style.width = \"210px\"; document.getElementById(\"_n1z\").style.left = \"215px\""
    # driver.execute_script(js);

    # driver.implicitly_wait(1);
    # img = driver.find_element_by_xpath('//*[@id="clickCaptcha"]/div[2]/img');
    # urllib.request.urlretrieve(img.get_attribute('src'), "d:\captcha.png")

    # print(img.get_attribute('src'));
    #下载图片
    # driver.get_screenshot_as_file("d:\ceshishiyong.jpg");

    # ActionChains(driver).drag_and_drop(kuang,huakuai);
    # driver.find_element_by_id("J_SubmitStatic").click();
    # print(elem.text);
    # print(elem.is_displayed());
    # elem.click();
    # time.sleep(10);
    # driver.get("http://www.baidu.com");
    # driver.maximize_window();
    # elem = driver.find_element_by_link_text("登录").click();
    # driver.implicitly_wait(30)
    # driver.find_element_by_id("TANGRAM__PSP_8__userName").send_keys("*****@*****.**");
    # driver.find_element_by_id("TANGRAM__PSP_8__password").send_keys("chenyang");
    # driver.find_element_by_id("TANGRAM__PSP_8__submit").click();
    # print(elem.text);
    # print(elem.is_displayed());
    # # elem.click();
    # time.sleep(100);
    # assert "No results found." not in driver.page_source;
    # driver.close();
    print("滑动结束");
示例#13
0
def bank():
    # time.sleep(5);
    wait.until(EC.element_to_be_clickable((By.XPATH,'/html/body/div[6]/div[2]/div[2]/iframe')));
    frame=driver.find_element_by_xpath(("/html/body/div[6]/div[2]/div[2]/iframe"));
    name = frame.get_attribute('name');
    print(name);

    driver.switch_to.frame(name)
    print("切换过")
    #银行卡交钱
    wait.until(EC.element_to_be_clickable((By.XPATH,'//*[@id="card-num"]')));
    driver.find_element_by_xpath('//*[@id="card-num"]').send_keys('6222600170013444988');

    # 点击下一步
    wait.until(EC.element_to_be_clickable((By.XPATH,'//*[@id="submit"]')));
    driver.find_element_by_xpath('//*[@id="submit"]').click();

    # 选择快捷支付
    wait.until(EC.element_to_be_clickable((By.XPATH,'//*[@id="DEBIT_EXPRESS-comm907"]')));
    driver.find_element_by_xpath('//*[@id="DEBIT_EXPRESS-comm907"]').click();

    # 点击下一步
    wait.until(EC.element_to_be_clickable((By.XPATH,'//*[@id="submitbtn"]')));
    driver.find_element_by_xpath('//*[@id="submitbtn"]').click();


    #输入手机号码
    wait.until(EC.element_to_be_clickable((By.XPATH,'//*[@id="mobileNo"]')));
    driver.find_element_by_xpath('//*[@id="mobileNo"]').send_keys('15957120592');

    # 点击获取验证码
    wait.until(EC.element_to_be_clickable((By.XPATH,'//*[@id="sms-validate-first"]')));
    driver.find_element_by_xpath('//*[@id="sms-validate-first"]').click();
    driver.find_elements_by_xpath()
示例#14
0
def wait_wrong_taobao():
    xpath = '//*[@id="srp-error"]'
    element = driver.find_element_by_xpath(xpath)
    s = element.value_of_css_property("display")
    print(s)
    return s == "none"
示例#15
0
def bank():
    # time.sleep(5);
    wait.until(
        EC.element_to_be_clickable(
            (By.XPATH, '/html/body/div[6]/div[2]/div[2]/iframe')))
    frame = driver.find_element_by_xpath(
        ("/html/body/div[6]/div[2]/div[2]/iframe"))
    name = frame.get_attribute('name')
    print(name)

    driver.switch_to.frame(name)
    print("切换过")
    #银行卡交钱
    wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="card-num"]')))
    driver.find_element_by_xpath('//*[@id="card-num"]').send_keys(
        '6222600170013444988')

    # 点击下一步
    wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="submit"]')))
    driver.find_element_by_xpath('//*[@id="submit"]').click()

    # 选择快捷支付
    wait.until(
        EC.element_to_be_clickable(
            (By.XPATH, '//*[@id="DEBIT_EXPRESS-comm907"]')))
    driver.find_element_by_xpath('//*[@id="DEBIT_EXPRESS-comm907"]').click()

    # 点击下一步
    wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="submitbtn"]')))
    driver.find_element_by_xpath('//*[@id="submitbtn"]').click()

    #输入手机号码
    wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="mobileNo"]')))
    driver.find_element_by_xpath('//*[@id="mobileNo"]').send_keys(
        '15957120592')

    # 点击获取验证码
    wait.until(
        EC.element_to_be_clickable(
            (By.XPATH, '//*[@id="sms-validate-first"]')))
    driver.find_element_by_xpath('//*[@id="sms-validate-first"]').click()
    driver.find_elements_by_xpath()
示例#16
0
def login():

    driver.get(
        "https://login.taobao.com/member/login.jhtml?redirectURL=https%3A%2F%2Fi.taobao.com%2Fmy_taobao.htm%3Fnekot%3DMTE5MzYxMDMyMnlhbmc%253D1447826354910"
    )
    driver.maximize_window()
    # elem = driver.find_element_by_link_text("亲,请登录").click();
    # driver.implicitly_wait(5)
    wait.until(lambda driver: driver.find_element_by_id("TPL_username_1"))
    driver.find_element_by_id("TPL_username_1").send_keys("15957120592")
    wait.until(lambda driver: driver.find_element_by_id("TPL_password_1"))
    driver.find_element_by_id("TPL_password_1").send_keys("yanli194612")

    wait.until(
        lambda driver: driver.find_element_by_xpath('//*[@id="_scale_text"]'))
    kuang = driver.find_element_by_xpath('//*[@id="_scale_text"]')

    wait.until(lambda driver: driver.find_element_by_xpath('//*[@id="_n1z"]'))
    huakuai = driver.find_element_by_xpath('//*[@id="_n1z"]')

    # 得到父节点 很重要
    print("huakuai parent")
    parent = huakuai.find_element_by_xpath("..")
    # print(parent.tag_name);
    # print(parent.get_attribute("id"));

    width = parent.value_of_css_property("width")
    width = str(width)[0:str(width).find("px")]
    print(width)

    # print(driver.current_url)
    # driver.title 返回的是html中title 使用查看网页源代码就可以看得到
    # print(driver.title)

    # 获得parent 下的所有节点  必须使用".//*" 而不是//*
    # elems = parent.find_elements_by_xpath('.//*')
    # for i in elems:
    #     print(i.get_attribute("id"))

    #
    # parent = parent.find_element_by_xpath("..");
    # print(parent.tag_name);
    # print(parent.get_attribute("id"));

    # wait.until(lambda driver:driver.find_element_by_xpath('//*[@id="_bg"]'));
    # div = driver.find_element_by_xpath('//*[@id="_bg"]');

    ActionChains(driver).click_and_hold(huakuai).move_by_offset(
        xoffset=width, yoffset=0).release(huakuai).perform()
    # js = "document.getElementById(\"_bg\").style.width = \"210px\"; document.getElementById(\"_n1z\").style.left = \"215px\""
    # driver.execute_script(js);

    # driver.implicitly_wait(1);
    # img = driver.find_element_by_xpath('//*[@id="clickCaptcha"]/div[2]/img');
    # urllib.request.urlretrieve(img.get_attribute('src'), "d:\captcha.png")

    # print(img.get_attribute('src'));
    #下载图片
    # driver.get_screenshot_as_file("d:\ceshishiyong.jpg");

    # ActionChains(driver).drag_and_drop(kuang,huakuai);
    # driver.find_element_by_id("J_SubmitStatic").click();
    # print(elem.text);
    # print(elem.is_displayed());
    # elem.click();
    # time.sleep(10);
    # driver.get("http://www.baidu.com");
    # driver.maximize_window();
    # elem = driver.find_element_by_link_text("登录").click();
    # driver.implicitly_wait(30)
    # driver.find_element_by_id("TANGRAM__PSP_8__userName").send_keys("*****@*****.**");
    # driver.find_element_by_id("TANGRAM__PSP_8__password").send_keys("chenyang");
    # driver.find_element_by_id("TANGRAM__PSP_8__submit").click();
    # print(elem.text);
    # print(elem.is_displayed());
    # # elem.click();
    # time.sleep(100);
    # assert "No results found." not in driver.page_source;
    # driver.close();
    print("滑动结束")
def submitAndBuy():

    # 去购物车结算
    wait.until(
        EC.visibility_of_element_located(
            (By.XPATH, '//*[@id="J_ResultSummary"]/div[4]/a[2]')))
    driver.find_element_by_xpath(
        '//*[@id="J_ResultSummary"]/div[4]/a[2]').click()

    # 选择所有物品
    wait.until(
        EC.element_to_be_clickable((By.XPATH, '//*[@id="J_SelectAll1"]')))
    driver.find_element_by_xpath('//*[@id="J_SelectAll1"]').click()

    # 去结算
    wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="J_Go"]')))
    driver.find_element_by_xpath('//*[@id="J_Go"]').click()

    #提交订单
    wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="J_Go"]')))
    driver.find_element_by_xpath('//*[@id="J_Go"]').click()

    # 点击其他付款方式
    wait.until(
        EC.element_to_be_clickable(
            (By.XPATH, '//*[@id="J-rcChannels"]/div/div/a[1]')))
    driver.find_element_by_xpath(
        '//*[@id="J-rcChannels"]/div/div/a[1]').click()

    # 添加银行卡
    wait.until(
        EC.element_to_be_clickable(
            (By.XPATH, '//*[@id="J-rcChannels"]/div/div/a')))
    driver.find_element_by_xpath('//*[@id="J-rcChannels"]/div/div/a').click()
def getElementShoppingCart():
    xpath = '//*[contains(text(),"加入购物车")]'
    wait.until(EC.element_to_be_clickable((By.XPATH, xpath)))
    element = driver.find_element_by_xpath(xpath)
    return element