python - 我正在尝试使用 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'
解决方案
我认为您根本不需要移至元素。
您只需要一个 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
推荐阅读
- ios - 如何在 iOS 应用程序的设置中显示通知设置选项
- amazon-web-services - 删除 EKS 集群
- postgresql - PostgreSQL ts_headline 与 ts_query 不正确匹配
- nginx - nginx如何匹配以下规则?
- python - 从 sqlalchemy 中定义的表中获取 CREATE 查询字符串
- node.js - 使用 Postman 和 NodeJS 上传和移动后如何删除源目录中的图像
- powershell - 如何通过使用 PowerShell 选择文件夹将 ComboBox 作为触发器来显示文件?
- ios - 为 keyboardWillShowNotification 设置 UIScrollView 的 contentInset 不能正常工作
- r - 使用 renderTable 时删除一列
- c++ - 在 wxWidgets 的事件中包含我自己的数据的最佳方式是什么?