Search code examples
python-3.xseleniumselenium-webdriverlambda

Switch between tabs - Selenium - Lambda


Need your guidence please I try to create a Lambda function that run Selenium.

Created the nessecery layers (all the packages and etc) and upload them to the Lambda. Write the code, locally and on the lambda.

The problem is that: In my scripts I query web page that has button that open a new tab, and when I run the code in a headless mode using Lambda, I cant switch between the tabs. Anyone Encounter this issue?

The version are:

  1. headless-chromium-v1.0.0.55
  2. chrome-driverV2.43

Appriciate any help. Thanks!

explain in the question


Solution

  • just loop through the tabs, and use the code below

    read more here

       # Store the ID of the original window
        original_window = driver.current_window_handle
        
        # Loop through until we find a new window handle
        for window_handle in driver.window_handles:
            if window_handle != original_window:
                driver.switch_to.window(window_handle)
                break
    

    Update

    if the anchor link is like this <a href="https://example.com/">click me</a> then when clicked it will open in the current tab/window

    but if its <a href="https://example.com/" target="_blank">click me</a> then when clicked it will open in a new tab

    look at the target attribute

    |------------------------------------------------------------------------------------------------------|
    |        Value       | Description                                                                     |
    |------------------------------------------------------------------------------------------------------|
    |        _blank      | Opens the linked document in a new window or tab                                |
    |        _self       | Opens the linked document in the same frame as it was clicked (this is default) |
    |       _parent      | Opens the linked document in the parent frame                                   |
    |       _top         | Opens the linked document in the full body of the window                        |
    |       framename    | Opens the linked document in the named iframe                                   |
    |------------------------------------------------------------------------------------------------------|
    

    Table from here