zoukankan      html  css  js  c++  java
  • 京东模拟点击

    # !/usr/bin/env python
    # -*- coding:utf-8 -*-

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.common.exceptions import TimeoutException
    from selenium.webdriver.support import expected_conditions as EC


    # 定义浏览器
    browser = webdriver.Chrome()
    # 本来是下面的因为后面很多地方会用到所以提在前面来
    wait = WebDriverWait(browser, 10)

    def search():
    try:
    browser.get("https://www.jd.com")
    input= wait.until( EC.presence_of_element_located((By.CSS_SELECTOR, "#key")) )
    submit = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,"#search > div > div.form > button")))
    input.clear()
    input.send_keys('美食')
    submit.click()
    # 获取总页数
    total =wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,"#J_bottomPage > span.p-skip > em:nth-child(1) > b")))
    return total.text
    # 引入这个错误判断时间是因为超出时间默认抛出TimeoutExcrption
    except TimeoutException:
    # 如果出现错误重新请求一次
    return search()


    def main():
    total = search()
    print(total)

    if __name__ == '__main__':
    main()
  • 相关阅读:
    apio2018题解
    ynoi2018
    hdu2036
    Morley's Theorem
    计算几何
    luogu1355 神秘大三角
    poj2398
    洛谷---小L和小K的NOIP考后放松赛
    LibreOJ β Round #7
    python3
  • 原文地址:https://www.cnblogs.com/lmx123/p/9497970.html
Copyright © 2011-2022 走看看