【问题标题】:selenium.common.exceptions.TimeoutException: this error will getting when I'm trying to button click using pythonselenium.common.exceptions.TimeoutException:当我尝试使用python单击按钮时会出现此错误
【发布时间】:2018-11-27 05:58:39
【问题描述】:

我正在使用 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()

【问题讨论】:

标签: python selenium selenium-webdriver iframe webdriverwait


【解决方案1】:

点击按钮前需要切换到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()

【讨论】:

  • 现在我没有收到错误但点击选项不起作用
  • @Haz 你想点击哪个按钮?
  • 全选按钮
  • @Haz ,检查更新,如果问题仍然存在,请告诉我
  • @Haz ,尝试在driver.get(...)之前添加browser.maximize_window()
【解决方案2】:

所需元素在 <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()
      
    • 浏览器快照:

【讨论】:

  • 这正是@Andersson 的回答,只有更好的定位器。
  • @Guy 这两种解决方案有很多根本的区别。您可以提出反问题或提出新问题。 Stackoverflow 贡献者将很乐意为您提供帮助。
  • 谢谢,但我没有任何问题。你们都使用了相同的expected_conditions。但是,虽然@Andersson 使用了idname,但您使用了xpath,除此之外,您还向定位器添加了class 依赖项,而不是中继唯一标识符。
  • 感谢您的所有回答。我对硒知之甚少。我是新手。仍然所有的解决方案都不起作用。 (按钮单击不起作用)
  • @Haz 查看我更新的答案并告诉我状态
猜你喜欢
  • 1970-01-01
  • 2021-10-20
  • 2022-11-07
  • 2017-06-25
  • 1970-01-01
  • 1970-01-01
  • 2020-04-23
  • 2020-10-18
  • 1970-01-01
相关资源
最近更新 更多