尝试打印元素时收到TimeoutException - Python Selenium

问题描述 投票:0回答:1
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

chrome_options = Options()
service = Service(executable_path="chromedriver.exe")
driver = webdriver.Chrome(service=service, options=chrome_options)

driver.get('https://app.itsdispatch.com/dispatch.php')

WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "email")))

email_input = driver.find_element(By.ID, "email")
email_input.send_keys('emails')

password_input = driver.find_element(By.ID, "password")
password_input.send_keys('Pass')
time.sleep(5)
go_button = driver.find_element(By.XPATH, "//button[contains(@class, 'mdl-button--accent')]")
go_button.click()

time.sleep(5)
header =  driver.find_element(By.ID, "dboard-title")
time.sleep(4)
print(header.text)
driver.quit()

错误:

raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
Stacktrace:

尝试打印该元素

python selenium-webdriver timeout
1个回答
0
投票

既然你这里的错误是TimeoutException,是否有可能driver.find_element没有找到id=dboard-title元素?注意selenium的driver.find_element可以设置为隐式等待

from selenium import webdriver

driver = webdriver.Chrome()
driver.implicitly_wait(10)  # 设置隐式等待时间为10秒
element = driver.find_element(By.ID, "element_id")

设置隐式等待后,driver.find_element会在指定的时间内重复尝试查找元素,直到找到或等待时间耗尽。

© www.soinside.com 2019 - 2024. All rights reserved.