繁体   English   中英

selenium.common.exceptions.TimeoutException:当我尝试使用python进行按钮单击时,将出现此错误

[英]selenium.common.exceptions.TimeoutException: this error will getting when I'm trying to button click using python

我正在使用Selenium软件包来单击网站的按钮。 当我尝试时出现以下错误:

selenium.common.exceptions.TimeoutException: Message:

这是试图运行的代码。

import time 
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC 
from bs4 import BeautifulSoup as BSoup
from datetime import date, timedelta
import pyodbc 
import datetime

browser =  webdriver.Firefox()
browser.get("https://www.cbsl.gov.lk/rates-and-indicators/exchange-rates/daily-buy-and-sell-exchange-rates")
#time.sleep(10)

#browser.find_element_by_xpath('//*[@id="dailyexchange"]/div[2]/div/button[1]').click()

wait = WebDriverWait(browser, 20)
element = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="dailyexchange"]/div[2]/div/button[1]')))
element.click()

您需要先切换到iframe,然后再单击按钮:

browser.get("https://www.cbsl.gov.lk/rates-and-indicators/exchange-rates/daily-buy-and-sell-exchange-rates")
wait = WebDriverWait(browser, 20)
wait.until(EC.frame_to_be_available_and_switch_to_it('iFrameResizer2'))
element = wait.until(EC.element_to_be_clickable((By.NAME, 'select_button')))
element.location_once_scrolled_into_view
element.click()

所需的元素在<iframe>因此您必须:

  • 诱导WebDriverWait获得所需的帧并切换到该帧
  • 诱导WebDriverWait使所需的元素可单击
  • 您可以使用以下解决方案之一:

    • 使用CSS_SELECTOR

       from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC options = Options() options.add_argument("start-maximized") browser = webdriver.Chrome(chrome_options=options, executable_path=r'C:\\Utility\\BrowserDrivers\\chromedriver.exe') browser.get("https://www.cbsl.gov.lk/rates-and-indicators/exchange-rates/daily-buy-and-sell-exchange-rates") WebDriverWait(browser, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#iFrameResizer2[src='/cbsl_custom/exratestt/exratestt.php']"))) WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn.btn-default[name='select_button']"))).click() 
    • 浏览器快照:

全选

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM