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)
相关推荐
星晨雪海24 分钟前
企业标准 DTO 传参 + Controller + Service + 拷贝工具类完整版
java·开发语言·python
yuanmazhiwu25 分钟前
计算机毕业设计:Python全国空气质量与气象监测平台 Flask框架 可视化 数据分析 机器学习 天气 深度学习 AI 空气质量分析(建议收藏)✅
人工智能·python·深度学习·数据挖掘·flask·汽车·课程设计
Mr_Xuhhh8 小时前
Java泛型进阶:从基础到高级特性完全指南
开发语言·windows·python
老天文学家了9 小时前
蓝桥杯备战Python
开发语言·python
ID_1800790547310 小时前
除了 Python,还有哪些语言可以解析 JSON 数据?
开发语言·python·json
FreakStudio10 小时前
小作坊 GitHub 协作闭环:fork-sync-dev-pr-merge 实战指南
python·单片机·嵌入式·面向对象·电子diy
普通网友11 小时前
阿里云国际版服务器,真的是学生党的性价比之选吗?
后端·python·阿里云·flask·云计算
小陈工12 小时前
2026年4月2日技术资讯洞察:数据库融合革命、端侧AI突破与脑机接口产业化
开发语言·前端·数据库·人工智能·python·安全
陈晓明start12 小时前
【python】豆包模型,自动生成测试用例初探索
python
阿kun要赚马内12 小时前
Python中元组和列表差异:底层结构分析
开发语言·python