How to wait for a text field to be editable with selenium in python 3.8

江枫思渺然 提交于 2020-12-12 05:40:31

问题


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!


回答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']")))


来源:https://stackoverflow.com/questions/62940492/how-to-wait-for-a-text-field-to-be-editable-with-selenium-in-python-3-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!