You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Python Selenium自动登录失败:无法输入数据及点击登录按钮

问题:Selenium无法操作登录弹窗的输入框与登录按钮

我用Python Selenium实现自动登录功能,但无法向账号、密码输入框填入数据,也无法点击登录按钮。网站每小时会自动登出,代码需要处于循环状态,每次循环检测登录弹窗是否出现并重新登录。已尝试多种写法,但均采用同类元素定位方式,代码中其他元素定位功能正常,仅登录模块失效。相关HTML结构及Python代码如下:

HTML结构

<iframe src="https://www.googletagmanager.com/ns.html?id=GTM-TNDZLZ2&gtm_auth=&gtm_preview=&gtm_cookies_win=x"
        height="0" width="0" style="display:none;visibility:hidden" id="tag-manager"></iframe>
<div class="popup-holder-bc full-screen  content-manager ">
<div class="popup-middleware-bc">
<div class="popup-inner-bc">
<i class="e-p-close-icon-bc bc-i-close-remove"></i>
<div class="entrance-popup-bc sign-in">
<div class="e-p-content-holder-bc">
<div class="e-p-content-bc"><div class="e-p-header-bc">
<a class="popup-t-logo-w-bc" href="/pb/">
<img class="hdr-logo-bc" src="/logo.png" alt=""></a>
<div class="e-p-sections-bc">
<div class="e-p-section-item-bc">
<button class="btn s-small register " type="button" title="Inscrever-se"><span>Inscrever-se</span>
</button>
</div>
</div>
</div>
<div class="e-p-body-bc">
<form class="entrance-form-bc login popup" data-scroll-lock-scrollable=""><div class="form-sign-bc">
<div class="sg-n-text-row-1-bc">Já tem uma conta?</div>
<div class="sg-n-text-row-2-bc">Faça o login, estamos esperando por você!</div>
<div class="entrance-f-item-bc">
<div class="form-control-bc default ">
<label class="form-control-label-bc inputs">
<input type="text" class="form-control-input-bc" name="username" step="0" value="">
<i class="form-control-input-stroke-bc"></i>
<span class="form-control-title-bc ellipsis">Email / Usuário</span>
</label>
</div>
</div>
<div class="entrance-f-item-bc">
<div class="form-control-bc default has-icon ">
<label class="form-control-label-bc inputs">
<input type="password" class="form-control-input-bc" autocomplete="current-password" name="password" step="0" value="">
<i class="form-control-input-stroke-bc">
</i>
<span class="form-control-title-bc ellipsis">Senha</span>
</label>
</div>
</div>
<div class="entrance-f-item-bc entrance-f-item-checkbox-bc">
<div class="checkbox-control-bc">
<label class="checkbox-control-content-bc">
<input type="checkbox" class="checkbox-control-input-bc" name="remember_me" step="0">
<i class="checkbox-control-icon-bc bc-i-checked">
</i>
<p class="checkbox-control-text-bc">Lembre-se de mim</p>
</label>
</div>
</div>
<div class="entrance-form-actions-holder-bc login-ext-1">
<div class="entrance-form-action-item-bc right">
<button class="btn a-color " type="submit" title="Entrar"><span>Entrar</span>
</button>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>

Python代码

from selenium import webdriver 
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
import telebot
import time
import Infos

driver = webdriver.Chrome()

driver.get('https://www.playpix.com/pb/live-casino/home/-1/All?profile=open&account=profile&page=details')
time.sleep(5)

if driver.find_element(By.XPATH, '/html/body/div[1]/div[6]/div/div/div/div/div/div[2]/form/div[1]') == 1:
    time.sleep(5)

    username=driver.find_element(By.XPATH, '/html/body/div[1]/div[6]/div/div/div/div/div/div[2]/form/div[1]/div[3]/div/label/input')
    password=driver.find_element(By.XPATH, '/html/body/div[1]/div[6]/div/div/div/div/div/div[2]/form/div[1]/div[4]/div/label/input')  

    username.send_keys(Infos.username) 
    password.send_keys(Infos.password)
    time.sleep(3)

    log_in_button = driver.find_element_by_xpath('/html/body/div[1]/div[6]/div/div/div/div/div/div[2]/form/div[1]/div[6]/div/button')
    log_in_button.click()
    time.sleep(5)

解决方案

1. 替换不稳定的绝对XPATH定位

绝对路径XPATH依赖页面层级结构,一旦页面元素位置变动就会失效,改用基于元素属性的稳定定位方式:

  • 账号输入框:(By.NAME, "username") 或 (By.CSS_SELECTOR, 'input[name="username"].form-control-input-bc')
  • 密码输入框:(By.NAME, "password") 或 (By.CSS_SELECTOR, 'input[name="password"].form-control-input-bc')
  • 登录按钮:(By.CSS_SELECTOR, 'button[type="submit"].btn.a-color') 或 (By.XPATH, '//button[@title="Entrar"]')

2. 改用显式等待替代固定sleep

固定time.sleep()无法适配页面加载速度波动,用Selenium显式等待确保元素可操作后再执行动作:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# 等待登录表单出现(最多10秒)
WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.CSS_SELECTOR, 'form.entrance-form-bc.login.popup'))
)

# 等待账号输入框可点击并输入
username_input = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.NAME, "username"))
)
username_input.send_keys(Infos.username)

# 等待密码输入框可点击并输入
password_input = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.NAME, "password"))
)
password_input.send_keys(Infos.password)

# 等待登录按钮可点击并点击
login_btn = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.CSS_SELECTOR, 'button[type="submit"]'))
)
login_btn.click()

3. 修复弹窗检测逻辑

原代码中if driver.find_element(...) == 1的判断逻辑错误,find_element返回元素对象而非数字,改用异常捕获判断弹窗是否存在:

def is_login_popup_present():
    try:
        driver.find_element(By.CSS_SELECTOR, 'form.entrance-form-bc.login.popup')
        return True
    except NoSuchElementException:
        return False

4. 实现循环检测登录状态

针对每小时自动登出的场景,添加循环定期检测弹窗,出现则执行登录:

while True:
    if is_login_popup_present():
        print("检测到登录弹窗,执行登录...")
        # 插入上述登录代码
    # 每5分钟检测一次(可根据需求调整间隔)
    time.sleep(300)

5. 排除iframe干扰

页面顶部存在隐藏iframe,若后续登录元素被移入iframe,需先切换到对应iframe再操作,操作完成后切回主文档:

# 切换到iframe(仅当元素在iframe内时使用)
iframe = driver.find_element(By.ID, 'tag-manager')
driver.switch_to.frame(iframe)
# 执行元素操作...
# 切回主文档
driver.switch_to.default_content()

内容的提问来源于stack exchange,提问作者Renan Ceolim Ramos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 13:20:59