【问题标题】:button is not clickable in while loop按钮在while循环中不可点击
【发布时间】:2021-07-15 12:31:20
【问题描述】:

您好,我想尝试使用 selenium 自动进行点击速度测试,但有一些错误 主要问题是按钮可以点击,但在while循环中不能点击。

我的代码是这样的

import selenium
import os
from tkinter import *
from tkinter import filedialog
import time
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium import webdriver
from selenium import webdriver
import contextlib as textmanager
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

from urllib3.packages.six import b
PATH= "C:\chromedrivers\chromedriver.exe"
driver= webdriver.Chrome(PATH)


list_of_words=[]


driver.get("https://clickspeedtest.com/")

time.sleep(2)

driver.find_element_by_xpath("//*[@id='ez-accept-all']").click()

button= driver.find_element_by_xpath("//*[@id='clicker']").click()

k=0

time.sleep(0.5)
while k != 100:
    button.click()
    k=k+1

我得到的错误是这不知道为什么

Traceback (most recent call last):
  File "c:\Users\ramhelsinki\projects\click_text.py", line 36, in <module>
    button.click()
AttributeError: 'NoneType' object has no attribute 'click'

这是按钮或点击区域的源代码

<button id="clicker" type="button" class="rbutton" style="width: 100%; height: 250px; display: none;">Click Here to start playing</button>

【问题讨论】:

  • 嗨! .click() 方法返回 None,在这一行中:button= driver.find_element_by_xpath("//*[@id='clicker']").click() 所以因为 button 具有 None 值,并且当您尝试在下次在 None 上执行 .click() 时,它会引发错误.作为一种解决方法,使用button= driver.find_element_by_xpath("//*[@id='clicker']") 并在下一行使用button.click()
  • driver.execute_script("arguments[0].click();", button) 可能会更快、更稳定。

标签: python python-3.x selenium selenium-webdriver selenium-chromedriver


【解决方案1】:

您收到此错误是因为未找到按钮标记。尝试这样做

 button= driver.find_element_by_ID('clicker')

【讨论】:

    【解决方案2】:

    第一次点击后按钮可能会发生变化。
    试试这个:

    button= driver.find_element_by_xpath("//*[@id='clicker']")
    
    while button.is_displayed():
        button.click()
        time.sleep(0.1)
        button= driver.find_element_by_xpath("//*[@id='clicker']")
        k=k+1
    

    另外请注意,5 秒后游戏结束,您将无法再次点击该按钮。 您必须按照我的建议在单击按钮之前验证按钮是否显示。

    【讨论】:

    • 不睡觉怎么办?
    • 需要调试。不确定它会正常工作。需要查看站点响应限制。不过我想睡眠可以缩短。
    猜你喜欢
    • 2020-01-21
    • 1970-01-01
    • 1970-01-01
    • 2020-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-25
    • 2017-09-03
    相关资源
    最近更新 更多