selenium.common.异常.TimeoutException:当我尝试使用python单击按钮时,会出现此错误

2024-05-03 13:19:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我用Selenium包来点击一个网站。当我尝试时,我得到一个错误:

selenium.common.exceptions.TimeoutException: Message:

这是试图运行的代码。在

^{pr2}$

Tags: 代码message网站selenium错误commonexceptionspr2
2条回答

单击按钮前,需要切换到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()
      
    • 浏览器快照:

selectall

相关问题 更多 >