【发布时间】: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