Python Selenium 自动登入1688

Python Selenium是一个用于自动化Web浏览器操作的库。它提供了一组功能强大的工具和API,可以模拟用户在浏览器中的行为,并执行各种任务,如点击、输入文本、提交表单等。

要使用Python Selenium登录1688网站,需要进行以下步骤:

  1. 安装Selenium库:在Python环境中,可以使用pip命令安装Selenium库。

    python 复制代码
    pip install selenium
  2. 下载并配置浏览器驱动程序:Selenium需要一个特定的浏览器驱动程序来与浏览器交互。对于Chrome浏览器,可以下载ChromeDriver;对于Firefox浏览器,可以下载geckodriver。将驱动程序放在系统路径中,或者指定其路径。

  3. 导入必要的模块:在Python脚本中,首先要导入selenium模块,以及WebDriver对象(用于控制浏览器)和Keys对象(用于模拟键盘输入)。

    python 复制代码
    from selenium import webdriver
    from selenium.webdriver.common.action_chains import ActionChains
    from selenium.webdriver.common.by import By
  4. 创建WebDriver实例:根据使用的浏览器类型,创建相应的WebDriver实例。例如,对于Chrome浏览器:

    python 复制代码
    driver = webdriver.Chrome()
  5. 打开1688网站:使用WebDriver打开1688网站的登录页面。

    python 复制代码
    driver.get("https://www.1688.com/")
  6. 定位并输入登录凭据:使用WebDriver的find_element方法找到用户名和密码的输入框,并使用send_keys方法输入相应的凭据。

  7. 提交登录表单:使用WebDriver的submit方法提交登录表单。

以下是完整代码:

python 复制代码
import time
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By

print('使用selenium模拟登陆')
# 使用selenium模拟登陆,获取并返回cookie
username = ''
password = ''
options = webdriver.ChromeOptions()  # 声明浏览器的对象
options.add_experimental_option('excludeSwitches', ['enable-automation'])
options.add_argument('--disable-blink-features')
options.add_argument('--disable-blink-features=AutomationControlled')  # 去除浏览器selenium监控
#options.add_argument('--headless')  # 浏览器不提供可视化页面
options.add_argument('--disable-gpu')  # 禁用GPU加速
browser = webdriver.Chrome(options=options)
browser.get('https://login.1688.com/')
time.sleep(1)
browser.find_element(By.XPATH, '//*[@id="fm-login-id"]').send_keys(username)
time.sleep(1)
browser.find_element(By.XPATH, '//*[@id="fm-login-password"]').send_keys(password)
time.sleep(1)
browser.find_element(By.XPATH, '//*[@type="submit"]').click()
time.sleep(4)
# 检查是否出现了滑动验证码
try:
    slider = browser.find_element(By.XPATH,  '//*[@id="nc_1_n1z"]')
    ActionChains(browser).click_and_hold(slider).perform()  # 按住滑块
    ActionChains(browser).move_by_offset(300, 0).perform()  # 移动到最右边
    ActionChains(browser).pause(0.5).release().perform()  # 松开鼠标
except:
    pass
# cookies_dict = {cookie['name']: cookie['value'] for cookie in driver.get_cookies()}
# driver.quit()
# print(cookies_dict)
相关推荐
MZ_ZXD0014 小时前
springboot汽车租赁服务管理系统-计算机毕业设计源码58196
java·c++·spring boot·python·django·flask·php
A 计算机毕业设计-小途4 小时前
大四零基础用Vue+ElementUI一周做完化妆品推荐系统?
java·大数据·hadoop·python·spark·毕业设计·毕设
念念01078 小时前
数学建模竞赛中评价类相关模型
python·数学建模·因子分析·topsis
云天徽上8 小时前
【数据可视化-94】2025 亚洲杯总决赛数据可视化分析:澳大利亚队 vs 中国队
python·信息可视化·数据挖掘·数据分析·数据可视化·pyecharts
☺����9 小时前
实现自己的AI视频监控系统-第一章-视频拉流与解码2
开发语言·人工智能·python·音视频
王者鳜錸9 小时前
PYTHON让繁琐的工作自动化-函数
开发语言·python·自动化
xiao助阵10 小时前
python实现梅尔频率倒谱系数(MFCC) 除了傅里叶变换和离散余弦变换
开发语言·python
麻辣清汤11 小时前
结合BI多维度异常分析(日期-> 商家/渠道->日期(商家/渠道))
数据库·python·sql·finebi
钢铁男儿11 小时前
Python 正则表达式(正则表达式和Python 语言)
python·mysql·正则表达式
钢铁男儿12 小时前
Python 正则表达式实战:解析系统登录与进程信息
开发语言·python·正则表达式