首页 > 解决方案 > 我正在尝试使用 python 为 firefox 上的站点创建自动登录,但单击按钮时我不断收到此错误,请帮帮我

问题描述

这是代码(在输入用户名和密码之前一切正常,但单击“提交”/“播放”按钮时出错):-

from selenium import webdriver # Used to import the driver
from selenium.webdriver import ActionChains
#from pynput.mouse import Button, Controller
import time
#mouse = Controller()
def bot(usr,pas):
    
    br=webdriver.Firefox() 
    br.get("https://tankionline.com/play/")
    time.sleep(10)
    br.find_element_by_link_text("Sign in").click()
    time.sleep(2)
    #user=br.find_element_by_css_selector("username")
    username = br.find_element_by_id("username")
    username.send_keys(usr)
    password=br.find_element_by_id("password")
    password.send_keys(pas)
    ActionChains(webdriver).click("<span>Play </span>").perform()
    
#main driver code
bot("Ricochet_Master","********")

这是错误(是否有替代按钮自动单击以避免此错误的方法?):-

Traceback (most recent call last):
  File "/home/aaryan/selenium auto login 2.py", line 24, in <module>
    bot("Ricochet_Master","********")
  File "/home/aaryan/selenium auto login 2.py", line 20, in bot
    ActionChains(webdriver).click("<span>Play </span>").perform()
  File "/home/aaryan/.local/lib/python3.8/site-packages/selenium/webdriver/common/action_chains.py", line 72, in __init__
    if self._driver.w3c:
AttributeError: module 'selenium.webdriver' has no attribute 'w3c'

标签: pythonselenium

解决方案


我认为您根本不需要移至元素。

您只需要一个 webdriver 等待。因为当您填写用户名和密码时,我们看到播放按钮已启用。

wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.ID, "username"))).send_keys('username')
wait.until(EC.element_to_be_clickable((By.ID, "password"))).send_keys('password')
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div[data-style='LoginComponentStyle-buttonPlay']"))).click()

进口:

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

推荐阅读