在 python 的 Selenium 中使用显式等待时总是出现 TimeoutException 错误
Always got TimeoutException error when using explicit wait in Selenium in python
我有下面的代码。
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)") #
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')
这是因为你的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()
我有下面的代码。
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)") #
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')
这是因为你的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()