【问题标题】:Selenium sometimes doesn't click checkbox sometimesSelenium 有时不单击复选框
【发布时间】:2020-02-06 18:32:09
【问题描述】:

我是 Selenium 的新手,所以我正在制作一个机器人来自动完成琐事,这样我就可以理解这个模块,这是一个很好的做法。有时 .click() 函数不起作用。

我试过了:

  • 使用 JavaScript 点击
  • 使用 WebDriverWait
  • 退出并关闭驱动并重新打开页面
  • 继续点击,直到复选框 ID 有效(继续循环)
  • 刷新页面并尝试再次单击(Java 和普通 .click()
  • 改用 time.sleep(time) 等待
  • 根本不使用 webdriver 等待,只是设置 var

我以前确实有过时的元素错误,但我已经解决了。

注意:它大部分时间都会点击,但随机只是不起作用,不是错误或任何东西

进口:

from selenium import webdriver
from time import *
from data import *
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException, StaleElementReferenceException
from winsound import *
import os

部分代码:

def AutoAnswer(self, answers_list, link):
    global i
    if i == 1:
        self.driver.get(link)
    # sends browser to first trivia
    # sets the variable to the current question so that the correct answer can be located through answer lists

self.current_question = (WebDriverWait(self.driver, 120).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="quizContainer"]/div[2]')))).text

#setting variables for boxes and their text values. The text can be corrolated to the button allowing the bot to click the correct answer
#/html//div[@id="quizContainer"]/div[@class="answersContainer"]/div[1]/span[@class="answerBox"]/a[@name="checkboxtag"]

for i in range(0, len(answers_list)):
    if answers_list[i]["question"] == self.current_question:
        self.current_answer = answers_list[i]["answer"]


self.xpath_1  = '//*[@id="quizContainer"]/div[3]/div[1]/span[1]/a'
self.text_1   = ((WebDriverWait(self.driver, 120)).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="quizContainer"]/div[3]/div[1]/span[2]')))).text


self.xpath_2  = '//*[@id="quizContainer"]/div[3]/div[2]/span[1]/a'
self.text_2   = ((WebDriverWait(self.driver, 120)).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="quizContainer"]/div[3]/div[2]/span[2]')))).text


self.xpath_3  = '//*[@id="quizContainer"]/div[3]/div[3]/span[1]/a'
self.text_3   = ((WebDriverWait(self.driver, 120)).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="quizContainer"]/div[3]/div[3]/span[2]')))).text


self.xpath_4  = '//*[@id="quizContainer"]/div[3]/div[4]/span[1]/a'
self.text_4   = ((WebDriverWait(self.driver, 120)).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="quizContainer"]/div[3]/div[4]/span[2]')))).text

while True:
    if self.text_1 == self.current_answer:
        (WebDriverWait(self.driver, 120).until(EC.element_to_be_clickable((By.XPATH, self.xpath_1)))).click()
    if self.text_2 == self.current_answer:
        (WebDriverWait(self.driver, 120).until(EC.element_to_be_clickable((By.XPATH, self.xpath_2)))).click()
    if self.text_3 == self.current_answer:
        (WebDriverWait(self.driver, 120).until(EC.element_to_be_clickable((By.XPATH, self.xpath_3)))).click()
    if self.text_4 == self.current_answer:
        (WebDriverWait(self.driver, 120).until(EC.element_to_be_clickable((By.XPATH, self.xpath_4)))).click()

    if bot.driver.find_elements_by_class_name("largecheckboxselected") != []:
        print("all good")
        break
    else:
        print("problemo re-running")
        if self.text_1 == self.current_answer:
            (WebDriverWait(self.driver, 120).until(EC.element_to_be_clickable((By.XPATH, self.xpath_1)))).click()
        if self.text_2 == self.current_answer:
            (WebDriverWait(self.driver, 120).until(EC.element_to_be_clickable((By.XPATH, self.xpath_2)))).click()
        if self.text_3 == self.current_answer:
            (WebDriverWait(self.driver, 120).until(EC.element_to_be_clickable((By.XPATH, self.xpath_3)))).click()
        if self.text_4 == self.current_answer:
            (WebDriverWait(self.driver, 120).until(EC.element_to_be_clickable((By.XPATH, self.xpath_4)))).click()
# selects and clicks the "Next Question!" button
nextquestion_btn = WebDriverWait(self.driver, 120).until(EC.element_to_be_clickable((By.XPATH,('//*[@id="nextQuestion"]'))))
nextquestion_btn.click()

运行代码:

bot = TriviaBot()
for i in range(1, 13):
    print("Run: "+str(i))
    bot.AutoAnswer(american_presidents, "https://www.freekigames.com/american-presidents-trivia")
bot.claimRewards()

data.py

username = "username"
password = "password"
american_presidents = [
    {"question" : "Who was the 1st president of the United States?",
     "answer"   : "George Washington"},
    {"question" : "Who was the 2nd president of the United States?",
     "answer"   : "John Adams"},
    {"question" : "Who was the 3rd president of the United States?",
     "answer"   : "Thomas Jefferson"},
    {"question" : "Who was the 4th president of the United States?",
     "answer"   : "James Madison"},
    {"question" : "Who was the 5th president of the United States?",
     "answer"   : "James Monroe"},
    {"question" : "Who was the 6th president of the United States?",
     "answer"   : "John Quincy Adams"},
    {"question" : "Who was the 7th president of the United States?",
     "answer"   : "Andrew Jackson"},
    {"question" : "Who was the 8th president of the United States?",
     "answer"   : "Martin Van Buren"},
    {"question" : "Who was the 9th president of the United States?",
     "answer"   : "William Henry Harrison"},
    {"question" : "Who was the 10th president of the United States?",
     "answer"   : "John Tyler"},
    {"question" : "Who was the 11th president of the United States?",
     "answer"   : "James K. Polk"},
    {"question" : "Who was the 12th president of the United States?",
     "answer"   : "Zachary Taylor"},
    {"question" : "Who was the 13th president of the United States?",
     "answer"   : "Millard Fillmore"},
    {"question" : "Who was the 14th president of the United States?",
     "answer"   : "Franklin Pierce"},
    {"question" : "Who was the 15th president of the United States?",
     "answer"   : "James Buchanan"},
    {"question" : "Who was the 16th president of the United States?",
     "answer"   : "Abraham Lincoln"},
    {"question" : "Who was the 17th president of the United States?",
     "answer"   : "Andrew Johnson"},
    {"question" : "Who was the 18th president of the United States?",
     "answer"   : "Ulysses S. Grant"},
    {"question" : "Who was the 19th president of the United States?",
     "answer"   : "Rutherford B. Hayes"},
    {"question" : "Who was the 20th president of the United States?",
     "answer"   : "James A. Garfield"},
    {"question" : "Who was the 21st president of the United States?",
     "answer"   : "Chester A. Arthur"},
    {"question" : "Who was the 22nd president of the United States?",
     "answer"   : "Grover Cleveland"},
    {"question" : "Who was the 23rd president of the United States?",
     "answer"   : "Benjamin Harrison"},
    {"question" : "Who was the 24th president of the United States?",
     "answer"   : "Grover Cleveland"},
    {"question" : "Who was the 25th president of the United States?",
     "answer"   : "William McKinley"},
    {"question" : "Who was the 26th president of the United States?",
     "answer"   : "Theodore Roosevelt"},
    {"question" : "Who was the 27th president of the United States?",
     "answer"   : "William Howard Taft"},
    {"question" : "Who was the 28th president of the United States?",
     "answer"   : "Woodrow Wilson"},
    {"question" : "Who was the 29th president of the United States?",
     "answer"   : "Warren G. Harding"},
    {"question" : "Who was the 30th president of the United States?",
     "answer"   : "Calvin Coolidge"},
    {"question" : "Who was the 31st president of the United States?",
     "answer"   : "Herbert Hoover"},
    {"question" : "Who was the 32nd president of the United States?",
     "answer"   : "Franklin D. Roosevelt"},
    {"question" : "Who was the 33rd president of the United States?",
     "answer"   : "Harry S. Truman"},
    {"question" : "Who was the 34th president of the United States?",
     "answer"   : "Dwight D. Eisenhower"},
    {"question" : "Who was the 35th president of the United States?",
     "answer"   : "John F. Kennedy"},
    {"question" : "Who was the 36th president of the United States?",
     "answer"   : "Lyndon B. Johnson"},
    {"question" : "Who was the 37th president of the United States?",
     "answer"   : "Richard Nixon"},
    {"question" : "Who was the 38th president of the United States?",
     "answer"   : "Gerald Ford"},
    {"question" : "Who was the 39th president of the United States?",
     "answer"   : "Jimmy Carter"},
    {"question" : "Who was the 40th president of the United States?",
     "answer"   : "Ronald Reagan"},
    {"question" : "Who was the 41st president of the United States?",
     "answer"   : "George H. W. Bush"},
    {"question" : "Who was the 42nd president of the United States?",
     "answer"   : "Bill Clinton"},
    {"question" : "Who was the 43rd president of the United States?",
     "answer"   : "George W. Bush"},
    {"question" : "Who was the 44th president of the United States?",
     "answer"   : "Barack Obama"}

]

chemical_elements = [
    {"question" : "Most of the earth's atmosphere consists of this gas.",
     "answer"   : "N"},
    {"question" : "The symbol 'Au' refers to which chemical element?",
     "answer"   : "Gold"},
    {"question" : "The symbol 'Co' refers to which chemical element?",
     "answer"   : "Cobalt"},
    {"question" : "The symbol 'Cu' refers to which chemical element?",
     "answer"   : "Copper"},
    {"question" : "The symbol 'F' refers to which chemical element?",
     "answer"   : "Fluorine"},
    {"question" : "The symbol 'H' refers to which chemical element?",
     "answer"   : "Hydrogen"},
    {"question" : "The symbol 'Pb' refers to which chemical element",
     "answer"   : "Lead"},
    {"question" : "The symbol 'S' refers to which chemical element?",
     "answer"   : "Sulfur"},
    {"question" : "This element give plants the energy they need to grow.",
     "answer"   : "P"},
    {"question" : "This element is the building block of life.",
     "answer"   : "C"},
    {"question" : "This element was discovered by Hans Christian Oersted in 1825.",
     "answer"   : "Al"},
    {"question" : "This element was discovered by Joseph Priestly and Carl Scheele in 1774.",
     "answer"   : "O"},
    {"question" : "This element was discovered in 1808.",
     "answer"   : "B"},
    {"question" : "This element when combined with Chlorine makes table salt.",
     "answer"   : "Na"},
    {"question" : "What is the symbol for Potassium?",
     "answer"   : "K"},
    {"question" : "Which element has a reddish color in a gas and liquid state?",
     "answer"   : "Br"},
    {"question" : "Which element has a silver-gray appearance?",
     "answer"   : "Zn"},
    {"question" : "Which of these elements is considered a Metal.",
     "answer"   : "Fe"},
    {"question" : "Which of these elements is NOT considered a Metalloid.",
     "answer"   : "Sn"},
    {"question" : "Which of these elements is NOT considered a Noble Gas.",
     "answer"   : "H"}

]

我已经看到了一些以前的答案,它们与已解决的过时元素错误有关。我相信这是一个不同的问题。

【问题讨论】:

  • 可能是同步问题。尝试在检查之前等待一些时间。
  • 你能详细说明一下吗?什么样的等待,什么样的检查。我应该插入或命名您正在谈论的支票的哪一行。谢谢
  • 能否提供复选框 HTML 代码?
  • 选中时:

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


【解决方案1】:

非常抱歉。我专注于我的代码而不是其他任何事情,我没有注意到它总是在“化学元素琐事”上失败。我也没有注意到它总是在一个问题上失败;这是因为我忘记在 data.py 字典问题中输入一个单词。现在一切正常。

感谢大家的帮助。我再次道歉

我现在感觉很愚蠢:/

【讨论】:

  • 让我指出另一个您可能会发现的问题,如果您在点击之前不稍等片刻,一些动态构建 DOM 树的网站会给您带来问题。例如,您想要单击执行某个操作后出现的按钮。您执行该操作,然后等待按钮出现然后单击,但是,单击事件可能尚未绑定(您正在与网站脚本竞争)并且单击将被触发但什么也不做,然后在下一步行动。
【解决方案2】:

另外三个想法:

  1. 流畅的等待:

    Wait<WebDriver> fluentWait = new FluentWait<>(driver) .withTimeout(30, TimeUnit.SECONDS) .pollingEvery(300, TimeUnit.MILLISECONDS) .ignoring(NoSuchElementException.class) .ignoring(StaleElementReferenceException.class);

    public WebElement fluentWait(By by) { WebElement waitElement = null; try { waitElement = fluentWait.until(webDriver -> getDriver().findElement(by)); } catch (TimeoutException ex) {} return waitElement; }

  2. 使用动作类

    WebElement element = fluentWait(by); Actions actions = new Actions(getDriver()); actions.moveToElement(element); actions.click(); actions.build().perform();

  3. 找出您应该等待的事件。应用程序是用 Angular 编写的吗?反应? jQuery?

https://www.swtestacademy.com/selenium-wait-javascript-angular-ajax/

【讨论】:

  • 感谢回复,请问这是Java还是JavaScript?我正在使用 Python。如果这是python那么我不知道如何使用它
猜你喜欢
  • 2017-09-24
  • 2021-12-11
  • 2015-12-03
  • 1970-01-01
  • 2018-04-24
  • 2020-10-06
  • 1970-01-01
  • 1970-01-01
  • 2019-10-10
相关资源
最近更新 更多