【问题标题】:Receiving NoSuchElementException while waiting for a text field to become editable with selenium and python 3.8在等待文本字段变为可使用 selenium 和 python 3.8 编辑时接收 NoSuchElementException
【发布时间】:2020-07-16 23:50:57
【问题描述】:

我只是将 selenium 机器人作为一个有趣的项目,它应该为我播放 typeracer,我在让它等待倒计时完成后再尝试开始打字时遇到了一些麻烦。我发现做到这一点的最好方法是等待文本输入字段可编辑,而不是等待倒计时弹出窗口消失,但正如我之前所说,除非我使用,否则我不能让它等待一个 time.sleep() 函数。这不会很好地工作,因为我们可能必须等待 5 到 12 秒才能启动机器人,因此它可能等待的时间太长或不够长。我已经尝试了许多其他类似问题的解决方案,例如this one,但到目前为止还没有完全奏效。我已经收到了有关此代码here 引起的先前错误的帮助,但由于这是一个不同的错误,我创建了一个新问题来处理它。

这是我目前的代码:

#!/usr/bin/env/python3

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


class TRBot:
    def __init__(self, username, passwd):
        self.username = username
        self.driver = webdriver.Safari()
        self.driver.get("https://play.typeracer.com")  # Open automated safari to typeracer
        time.sleep(2)
   

        self.driver.find_element_by_xpath("//a[@title=\"Keyboard shortcut: Ctrl+Alt+I\"]").click()  # Click the "Enter a typing race" button
        time.sleep(2)


        inputField = WebDriverWait(self.driver, 20).until(EC.visibility_of(self.driver.find_element_by_xpath("//div[@contenteditable='plaintext-only']")))

        # Find the first word of the passage to type
        text = self.driver.find_element_by_xpath("//*[@id=\"gwt - uid - 15\"]/table/tbody/tr[2]/td/table/tbody/tr[1]/td/table/tbody/tr[1]/td/div/div/span[1]").get_attribute("innerHTML")

        while text != "":
            inputField.send_keys(text)  # Type the word
            text = self.driver.find_element_by_xpath("//*[@id=\"gwt - uid - 15\"]/table/tbody/tr[2]/td/table/tbody/tr[1]/td/table/tbody/tr[1]/td/div/div/span[1]").get_attribute("innerHTML")  # Find the next word

        time.sleep(5)

        self.driver.quit()


TypeRacerBot = TRBot("TRBot", "R0b0t@")

这是错误输出:

Traceback (most recent call last):
  File "/Users/kalebrosborough/Documents/Programming/Python/TypeRacerBot.py", line 45, in <module>
    TypeRacerBot = TRBot("TRBot", "R0b0t@")
  File "/Users/kalebrosborough/Documents/Programming/Python/TypeRacerBot.py", line 29, in __init__
    inputField = WebDriverWait(self.driver, 20).until(EC.visibility_of(self.driver.find_element_by_xpath("//div[@contenteditable='plaintext-only']")))
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 394, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 976, in find_element
    return self.execute(Command.FIND_ELEMENT, {
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: 

现在,直到 inputField = WebDriverWait(... 行之前,一切都按预期工作,所以这就是我目前专注于修复的内容,但如果您在代码中看到任何无法进一步工作的内容,我也愿意接受那里的建议.

提前致谢!

【问题讨论】:

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


    【解决方案1】:

    我可以看到你已经在这里使用了预期的条件

    inputField = WebDriverWait(self.driver, 20).until(EC.visibility_of(self.driver.find_element_by_xpath("//div[@contenteditable='plaintext-only']")))
    

    如果您需要等待字段可编辑,请使用以下预期条件

    inputField = WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable(self.driver.find_element_by_xpath("//div[@contenteditable='plaintext-only']")))
    

    所以您需要做的就是用“EC.element_to_be_clickable”替换您的预期条件,这应该可以为您解决问题。

    【讨论】:

    • 我刚试过这个,我仍然收到同样的错误。还有其他可能导致此问题的原因吗?
    • 你能发帖吗,现在究竟是哪一行抛出错误
    • 回溯中的第一行引用了我对 TRBot() 类 (TypeRacerBot = TRBot("TRBot", "R0b0t@")) 的调用,这并没有那么有用。第二个是你刚刚帮助我的同一行 (inputField = WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable(self.driver.find_element_by_xpath("//div[@contenteditable='plaintext-only']")))) 回溯中的其他所有内容都只是引用 selenium WebDriver 模块中的内容。
    猜你喜欢
    • 1970-01-01
    • 2016-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-16
    • 2020-12-04
    • 2014-07-19
    • 1970-01-01
    相关资源
    最近更新 更多