【问题标题】:Always got TimeoutException error when using explicit wait in Selenium in python在 python 中的 Selenium 中使用显式等待时总是出现 TimeoutException 错误
【发布时间】:2020-04-16 08:17:49
【问题描述】:

我有下面的代码。

from selenium import  webdriver
import time
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

driver = webdriver.Chrome() #
driver.implicitly_wait(10) #https://blog.csdn.net/u010895119/article/details/77005886
driver.get('http://www.aim.env.uea.ac.uk/aim/model4/model4d.php') #
button = driver.find_element_by_id('add_organic')#
button.click()#
input = driver.find_element_by_id("selection_box_id")#
input.click()
driver.find_element_by_xpath('//*[@id="selection_box_id"]/option[3]').click() #
driver.find_element_by_xpath('//*[@id="selection_box_div"]/p/input').click()#
input = driver.find_element_by_id("selection_box_id")#
input.click()
driver.find_element_by_xpath('//*[@id="selection_box_id"]/option[7]').click()#
driver.find_element_by_xpath('//*[@id="selection_box_div"]/p/input').click()# 
driver.find_element_by_xpath('//*[@id="BacktoCalculation_id"]').click() #
#driver.execute_script("window.history.go(-1)") #https://stackoverflow.com/questions/27626783/python-selenium-browser-driver-back

wait = WebDriverWait(driver, 10) # 
element = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="mainForm"]/div[2]/table/tbody/|tr/td[3]/input')))# 
element.click()

我总是遇到超时错误:

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

但是当我尝试直接找到元素时,它确实有效。但我需要在这里明确等待以确保安全。任何人都可以帮助找出上面代码中的问题吗?非常感谢!

element = driver.find_element_by_xpath('//*[@id="mainForm"]/div[2]/table/tbody/tr/td[3]/input')

【问题讨论】:

    标签: python selenium webdriverwait


    【解决方案1】:

    这是因为你的 xpath 中有错字

    '//*[@id="mainForm"]/div[2]/table/tbody/|tr/td[3]/input' # error
    '//*[@id="mainForm"]/div[2]/table/tbody/tr/td[3]/input'  # ok
    

    这很好用

    wait = WebDriverWait(driver, 10) #
    element = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="mainForm"]/div[2]/table/tbody/tr/td[3]/input')))#
    element.click()
    

    【讨论】:

    • 非常感谢。我花了这么多时间,但找不到它......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-02
    相关资源
    最近更新 更多