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