3

I am just making a selenium bot as a fun project that is supposed to play typeracer for me, and I am having a bit of trouble getting it to wait for the countdown to be done before it tries to start typing. The best way that I have found to do this is to just wait for the text input field to be editable instead of waiting for the countdown popup to be gone, but as I said before, I can't get it to wait unless I use a time.sleep() function. This wouldn't work well because of the fact that we could have to wait for anywhere from 5ish-12ish seconds before the bot can start so it could wait too long or not long enough. I have tried the solutions from many other similar questions such as this one, but so far nothing has worked.

Here is my code at the moment:

#!/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, 10).until(EC.visibility_of((By.XPATH, "<div contenteditable=\"plaintext-only\"></div>")))

        # 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@")

and here is the error output:

Traceback (most recent call last):
  File "/Users/myuser/Documents/Programming/Python/TypeRacerBot.py", line 45, in <module>
    TypeRacerBot = TRBot("TRBot", "R0b0t@")
  File "/Users/myuser/Documents/Programming/Python/TypeRacerBot.py", line 29, in __init__
    inputField = WebDriverWait(self.driver, 10).until(EC.visibility_of((By.XPATH, "<div contenteditable=\"plaintext-only\">\*</div>")))
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/support/wait.py", line 71, in until
    value = method(self._driver)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/support/expected_conditions.py", line 144, in __call__
    return _element_if_visible(self.element)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/support/expected_conditions.py", line 148, in _element_if_visible
    return element if element.is_displayed() == visibility else False
AttributeError: 'tuple' object has no attribute 'is_displayed'

Right now, everything works as expected up to the inputField = WebDriverWait(... line so that's what I'm currently focused on fixing, but if you see anything that won't work further along in the code I am open to suggestions there too.

Thanks in advance!

Shock9616
  • 116
  • 9

1 Answers1

1

You need to replace the line:

inputField = WebDriverWait(self.driver, 10).until(EC.visibility_of((By.XPATH, "<div contenteditable=\"plaintext-only\"></div>")))

with:

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

or:

inputField = WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//div[@contenteditable='plaintext-only']")))
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I just tried this and while it did fix my previous error, I am now getting the `selenium.common.exceptions.NoSuchElementException: Message:` error... – Shock9616 Jul 16 '20 at 18:15
  • @Shock9616 `NoSuchElementException: Message:` sounds all together a different issue. Can you raise a new question with your new requirement please? – undetected Selenium Jul 16 '20 at 18:18