1

I'm using Selenium package to button click for a website. When I'm trying i'm getting an error as:

selenium.common.exceptions.TimeoutException: Message:

this is the code which trying to run.

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()
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Haz
  • 321
  • 2
  • 8
  • 16
  • 1
    Possible duplicate of [function for switching frames in python, selenium](https://stackoverflow.com/questions/28723143/function-for-switching-frames-in-python-selenium) – Guy Nov 27 '18 at 06:05

2 Answers2

1

You need to switch to iframe before clicking button:

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()
Andersson
  • 51,635
  • 17
  • 77
  • 129
0

The desired element is within an <iframe> so you have to:

  • Induce WebDriverWait for the desired frame to be available and switch to it.
  • Induce WebDriverWait for the desired element to be clickable.
  • You can use either of the following solutions:

    • Using 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()
      
    • Browser Snapshot:

selectall

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 2
    This is the exactly what @Andersson answered, only with better locators. – Guy Nov 27 '18 at 06:33
  • @Guy there are a lot of fundamental differences between the two solutions. You can ask a counter question or raise a new question. Stackoverflow contributors will be happy to help you out. – undetected Selenium Nov 27 '18 at 06:37
  • 2
    Thanks, but I don't have any question. You both used the same `expected_conditions`. But while @Andersson used `id` and `name` you used `xpath`, and on top of that you added `class` dependency to the locator instead of relaying on the unique identifiers. – Guy Nov 27 '18 at 06:42
  • Thank you for all your answers. I don't have much knowledge in selenium. I'm new to it. Still all the solutions were not working. (Button click is not working) – Haz Nov 27 '18 at 06:51
  • @Haz Checkout my updated answer and let me know the status – undetected Selenium Nov 27 '18 at 06:52
  • Yes the solution which you have gave also working. thank you very much for the answer. – Haz Nov 27 '18 at 07:06
  • @Haz Glad to be able to help you !!! Upvote the answer if this/any answer is/was helpful to you for the benefit of the future readers. – undetected Selenium Nov 27 '18 at 07:21