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:
- Created 2 years ago
- Comments:6 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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:
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.
Closing this as we did not get more information or feedback after the previous comment.