簡體   English   中英

selenium.common.exceptions.TimeoutException:消息:

[英]selenium.common.exceptions.TimeoutException: Message:

我已經查看了描述該問題的線程,但無法提出解決方案,因為已經存在的答案很舊,並且似乎與實際問題無關。 我正在嘗試使用 Selenium WebDriver 設置 Python 腳本,這將允許我使用 Telegram Web 在我的 Telegram 帳戶上自動執行操作。

注意:您無需擁有 Telegram 帳戶即可查看問題發生的位置。

我要做什么:腳本要求您輸入國家代碼和電話號碼。 然后它應該將此信息填充到網頁上的輸入框中。 到目前為止,該腳本設法將國家代碼解析到正確的輸入框並將其放置得很好。 但是在將電話號碼放入輸入框中時,我得到了 selenium.common.exceptions.TimeoutException 錯誤。 我不明白為什么。

這是我的代碼:

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)

我試過的:

我檢查了我是否沒有使用“ec.presence_of_element_located”方法,而網頁將包含相同類型和 class 下的多個元素。 與此相關的某些錯誤,您必須使用“presence_of_all_elements_located”或指定您嘗試訪問的錯誤,可能使用:nth-of-type/child 選擇器。 此 CSS 選擇器僅在頁面上出現一次,因此這似乎不會導致問題。

我已經使用“wait.until”方法檢查了元素是否存在,並且出於純粹的絕望還嘗試使用“sleep(x)”。 這並沒有解決問題,它還應該產生一個不同的異常“沒有這樣的元素”。

由於沒有 WebDriver 錯誤並且瀏覽器打開得很好,這讓我相信不存在與不正確的 WebDriver 路徑或任何此類情況相關的任何問題。

我不知道是什么導致了這個問題,所以如果你知道,請隨時糾正我的錯誤! 感謝任何人!

首先,我英語說得不好。 對此我很抱歉。

此部分出現錯誤。 我猜這個錯誤是由於 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")))

可以這樣試試嗎。。

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"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM