Home > Mobile >  Scraping a website with selenium in python: Problem in clicking "download images" Buttons
Scraping a website with selenium in python: Problem in clicking "download images" Buttons

Time:10-19

Good morning. I am new in using python and webscraping. I have to download a series of images from this link (just the last part of the urll change for the following pages): https://historisch.cbs.nl/detail.php?nav_id=0-1&index=2&id=30568043 What I want to do is click on the two download buttons in order that at the end I download the images on my laptop (https://i.stack.imgur.com/zWAJg.png). Here is my code until now:

browser = webdriver.Chrome(service=s, options=chrome_options)
for i in range(8047,8051):   
    no = str(i)
    browser.get ("https://historisch.cbs.nl/detail.php?nav_id=1-1&index=2&id=3056" str(i) ".jpeg")

download = WebDriverWait(browser, 5).until(EC.element_to_be_clickable((By.XPATH,'//*[@id="downloadDirect"]')))
download.click()
t.sleep(1)

download = WebDriverWait(browser, 3).until(EC.element_to_be_clickable((By.XPATH,'//*[@id="downloadResLink"]')))
download.click()

I recive this error:

----> 8 download = WebDriverWait(browser, 5).until(EC.element_to_be_clickable((By.XPATH,'//*[@id="downloadDirect"]')))
      9 download.click()
     10 t.sleep(1)

~\anaconda3\lib\site-packages\selenium\webdriver\support\wait.py in until(self, method, message)
     88             if time.monotonic() > end_time:
     89                 break
---> 90         raise TimeoutException(message, screen, stacktrace)
     91 
     92     def until_not(self, method, message: str = ""):

TimeoutException: Message: 
Stacktrace:
Backtrace:
    Ordinal0 [0x00371ED3 2236115]
    Ordinal0 [0x003092F1 1807089]
    Ordinal0 [0x002166FD 812797]
    Ordinal0 [0x002455DF 1005023]
    Ordinal0 [0x002457CB 1005515]
    Ordinal0 [0x00277632 1209906]
    Ordinal0 [0x00261AD4 1120980]
    Ordinal0 [0x002759E2 1202658]
    Ordinal0 [0x002618A6 1120422]
    Ordinal0 [0x0023A73D 960317]
    Ordinal0 [0x0023B71F 964383]
    GetHandleVerifier [0x0061E7E2 2743074]
    GetHandleVerifier [0x006108D4 2685972]
    GetHandleVerifier [0x00402BAA 532202]
    GetHandleVerifier [0x00401990 527568]
    Ordinal0 [0x0031080C 1837068]
    Ordinal0 [0x00314CD8 1854680]
    Ordinal0 [0x00314DC5 1854917]
    Ordinal0 [0x0031ED64 1895780]
    BaseThreadInitThunk [0x75B4FA29 25]
    RtlGetAppContainerNamedObjectPath [0x779A7BBE 286]
    RtlGetAppContainerNamedObjectPath [0x779A7B8E 238]

If someone could help me, I would be very grateful.

CodePudding user response:

You're trying to click on a button located in an iframe. You need to switch to that iframe first, then locate the button, then click on it:

WebDriverWait(browser, 5).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, '//iframe[@]')))
t.sleep(5) ## let elements within iframe load properly
[.. do your stuff locating/clicking whatever buttons are there]

If you plan on interacting /locating again some elements from the main page(outside iframe) you need to switch back to main page:

browser.switch_to.default_content()

Selenium documentation can be found here: https://www.selenium.dev/documentation/

  • Related