question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Python - Function `execute_cdp_cmd` only work for first tab in chromedriver

See original GitHub issue

🐛 Bug Report

I have use execute_cdp_cmd to excute some script before loading website. But execute_cdp_cmd only work for first tab. Step:

  • When browser start and first tab open, execute_cdp_cmd work perfect.
  • I click plus icon to open new tab. In second tab, execute_cdp_cmd not working - not run script.
  • I close all tab and open new tab (first tab - only one tab), execute_cdp_cmd work perfect

My code: (source is js script like console.log)

import os
import sys
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait, Select
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.remote.webdriver import WebDriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

source = """
  alert('test')
"""

def init_webdriver():
    dir_path = os.path.dirname(os.path.realpath(__file__))
    driver_path = '/usr/local/bin/chromedriver'
    driver = webdriver.Chrome(executable_path=driver_path)
    driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
      "source": source
    })
    return driver

if __name__ == '__main__':
    init_webdriver().get('https://github.com')
    sleep(5000000)

Video demo: https://gyazo.com/3a54b80bbd94a71cbfd9133d646dd3ec

Environment

OS: MacOs 11.2.3 Browser: Chrome Browser version: 89 Browser Driver version: ChromeDriver 89 Language Bindings version: python 3.9 Selenium Grid version (if applicable): lasted

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
diemolcommented, Mar 26, 2021

I can see the behaviour you mention but I am not sure if this is an issue or not. After checking https://chromedevtools.github.io/devtools-protocol/tot/Page/#method-addScriptToEvaluateOnNewDocument, it says:

Evaluates given script in every frame upon creation (before loading frame’s scripts).

I am not sure what every frame means in this context. I am assuming this needs to be done per tab. Do you have any official docs mentioning that it should work for all tabs? No matter if the tab is open manually?

One important thing to note is that Selenium WebDriver allows you to interact with CDP, but it does not guarantee the behaviour in CDP is correct. That is why I recommend you to create an issue here to understand what the expected behaviour is.

0reactions
diemolcommented, Apr 16, 2021

Closing this as we did not get more information or feedback after the previous comment.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python: Why does my script on selenium only work if I am with ...
I list some possibilities; def moving_on(self, energy): while True: if self.driver.find_element_by_id("farmers1").is_displayed(): try: if ...
Read more >
Opening and Closing Tabs Using Selenium - GeeksforGeeks
Python selenium module can be installed using the below command: pip install selenium. Chrome Driver can be downloaded from Chrome Driver ...
Read more >
WebDriver for Chrome - Downloads
Resolved issue 4198: ChromeDriver BiDi should not assume that BiDiMapper is always in the first tab [Pri-]. Resolved issue 4207: WebDriver error when ......
Read more >
How To Switch Tabs In A Browser Using Selenium Python?
In this Python Selenium switch tabs tutorial, we will be focusing on opening and switching between the tabs using Python & Selenium.
Read more >
How To Add And Close Tabs With Selenium Python - YouTube
In this tutorial, I will be covering how to add and remove tabs of a browser with Selenium in Python.▻ Buy Me a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found