python-3.x 在选项卡- Selenium - Lambda之间切换

zyfwsgd6  于 2022-11-19  发布在  Python
关注(0)|答案(1)|浏览(137)

需要您的指导,请我尝试创建一个运行Selenium的Lambda函数。
创建了nessecery层(所有的包等),并将它们上传到Lambda。在本地和lambda上编写代码。
问题在于:在我的脚本中,我查询的网页有打开新标签页的按钮,当我使用Lambda在无头模式下运行代码时,我无法在标签页之间切换。有人遇到过这个问题吗?
版本有:

  1. headless-chromium-v1.0.0.55
  2. chrome-driverV2.43
    感谢任何帮助。谢谢!
    在问题中解释
bnlyeluc

bnlyeluc1#

只需遍历选项卡,然后使用下面的代码
阅读更多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

更新

如果锚链接类似于<a href="https://example.com/">click me</a>,则单击时将在当前选项卡/窗口中打开
但如果是<a href="https://example.com/" target="_blank">click me</a>,则单击时将在新选项卡中打开
查看target属性

|------------------------------------------------------------------------------------------------------|
|        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                                   |
|------------------------------------------------------------------------------------------------------|

阅读更多here

相关问题