Home > Software design >  selenium cannot locate button
selenium cannot locate button

Time:02-06

I try to automatically click on a given downloadButton id but it doesn't work on this demo website https://www.globalsqa.com/demo-site/progress-bar.

Chromedriver opens, then nothing happens on the website and at some point it closes due to timeout I imagine ?

Traceback :

Traceback (most recent call last):
  File "c:\Users\x\Dropbox\PC\Documents\Python Project\selenium tutorial\tutorial1.py", line 22, in <module>  
    my_element = driver.find_element(By.ID, "downloadButton")
  File "C:\Python310\lib\site-packages\selenium\webdriver\support\wait.py", line 89, in until
    raise TimeoutException(message, screen, stacktrace)   

I do some implicit wait supposing that the button doesn't exist but it won't work.

import os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

ser = Service("C:/Program Files (x86)/chromedriver.exe")
options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-logging'])
driver = webdriver.Chrome(service=ser, options=options)

driver.get("https://www.globalsqa.com/demo-site/progress-bar/")

# Wait for checkbox to be located
button_wait = EC.presence_of_element_located((By.ID, 'downloadButton'))
WebDriverWait(driver, 10).until(button_wait)

driver.implicitly_wait(30)
# my_element = driver.find_element_by_id('downloadButton')
my_element = driver.find_element(By.ID, "downloadButton")
my_element.click()

chrome-version 98.0.4758.81

CodePudding user response:

The element you are trying to access is inside an iframe.
So you first have to switch into that iframe in order to access elements inside it.
Like the following:

import os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

ser = Service("C:/Program Files (x86)/chromedriver.exe")
options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-logging'])
driver = webdriver.Chrome(service=ser, options=options)
wait = WebDriverWait(driver, 20)
driver.get("https://www.globalsqa.com/demo-site/progress-bar/")
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe.demo-frame.lazyloaded")))

wait.until(EC.visibility_of_element_located((By.ID, "downloadButton"))).click()

When finished working inside the iframe you will have to switch back to a defaul content with

driver.switch_to.default_content()
  •  Tags:  
  • Related