Search code examples
pythonpython-3.xseleniumselenium-webdrivergeckodriver

selenium.common.exceptions.TimeoutException: Message:


I have already looked into threads that describe the issue but couldn't come up with a solution since the already existing answers are old and don't seem to relate to the actual problem what so ever. I am trying to setup a Python script using Selenium WebDriver that would allow me to automate actions on my Telegram account using Telegram Web.

NOTE : You don't need to have a Telegram account to see where the issue happens.

What am I trying to do : The script asks you for a country code and your phone number. Then it's supposed to fill this information into the input boxes on the webpage. So far the script manages to parse the country code to the right input box and place it just fine. But when it comes to putting the phone number into the input box I get the selenium.common.exceptions.TimeoutException error. I can't understand why though.

Here's my code :

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from time import sleep
from platform import system
from os import getcwd, getlogin, getenv

cwd = getcwd()
os = system()
user = getlogin()
appdata = getenv('APPDATA')

country_code = input("Phone number country code: ")
phone_number = input("Phone number: ")

    if os == "Linux":
        if user == "root":
            print(
                "You are executing the script as root. Make sure that the Firefox profile folder is also located in the root directory.")
        driver = webdriver.Firefox(executable_path=cwd + "/geckodriver",
                                   firefox_profile="/home/" + user + "/.mozilla/firefox/kqskr2vn.default-esr")
    elif os == "Windows":
        driver = webdriver.Firefox(executable_path=cwd + "\\geckodriver.exe",
                                   firefox_profile=appdata + "\\Mozilla\\Firefox\\Profiles\\f4ymhsbu.default-esr-1")
    # elif os == "Darwin":
    
    page_number = 1
    wait = WebDriverWait(driver, 10)
    driver.get("https://web.telegram.org/#/login")
    
    wait.until(
        ec.presence_of_element_located((By.CSS_SELECTOR, "input.md-input.ng-pristine.ng-valid.ng-not-empty.ng-touched")))
    cc_ID = "input.md-input.ng-pristine.ng-valid.ng-not-empty.ng-touched"
    driver.find_element_by_css_selector(cc_ID).click()
    driver.find_element_by_css_selector(cc_ID).send_keys(Keys.CONTROL + "a")
    driver.find_element_by_css_selector(cc_ID).send_keys(Keys.BACKSPACE + country_code)
    sleep(5)
    wait.until(ec.presence_of_element_located(
        (By.CSS_SELECTOR, "input.md-input.ng-pristine.ng-untouched.ng-empty.ng-invalid.ng-invalid-required")))
    pn_ID = "input.md-input.ng-pristine.ng-untouched.ng-empty.ng-invalid.ng-invalid-required"
    driver.find_element_by_css_selector(pn_ID).click()
    driver.find_element_by_css_selector(pn_ID).send_keys(phone_number)

What I've tried :

I have checked if I'm not using the "ec.presence_of_element_located" method while the webpage would contain multiple elements under the same type and class. Certain error related to this and you had to either use "presence_of_all_elements_located" or specify which one you are trying to access perhaps using the :nth-of-type/child selector. This CSS selector occurs only once on the page so this doesn't seem to cause the issue.

I have checked if the element exists using the "wait.until" method and also tried to use "sleep(x)" out of pure despair. This didn't fix the issue and it should also produce a different exception "No Such Element Located".

Since there are no WebDriver errors and the browser opens just fine it leads me to believe that there isn't any problem related to incorrect WebDriver paths or anything of such case.

I don't know what could cause the issue anymore so if you know please feel free to correct my mistake! Thanks to anyone!


Solution

  • First of all, I can't speak English well. I'm sorry for this.

    The error occurs in this section. I guess the error is due to class..

    wait.until(ec.presence_of_element_located(
            (By.CSS_SELECTOR, "input.md-input.ng-pristine.ng-untouched.ng-empty.ng-invalid.ng-invalid-required")))
    

    Can you try it like this..

    wait.until(ec.presence_of_element_located((By.CSS_SELECTOR, "input.ng-pristine.ng-empty.ng-invalid.ng-invalid-required.ng-touched")))
    pn_ID = "input.ng-pristine.ng-empty.ng-invalid.ng-invalid-required.ng-touched"