import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
# Path to Chrome executable and Chrome DevTools port
chrome_path = 'C:\Users\tejveDownloads\chromedriver-win64\chromedriver-win64\chromedriver.exe'
devtools_port = 9222
# Same port you used to start Chrome #"C:Program FilesGoogleChromeApplicationchrome.exe" --remote-debugging-port=9222
extension_path = 'C:\Users\tejve\AppData\Local\Google\Chrome\User Data\Default\Extensions\ofaokhiedipichpaobibbnahnkdoiiah\1.2.0_0'
# Set up Chrome options
options = Options()
# Add the extension by specifying its path
options.add_argument("--load-extension=" + extension_path)
# Set up debugger address
options.add_experimental_option("debuggerAddress", "localhost:{}".format(devtools_port))
# Create a new Chrome session
service = Service(chrome_path)
# Create a new Chrome session
driver = webdriver.Chrome(service=service, options=options)
# Navigate to a webpage
driver.get("https://economictimes.indiatimes.com/marketstats/duration-1d,exchange-nse,pageno-1,pid-0,sort-intraday,sortby-percentchange,sortorder-desc.cms")
# Wait for the page to load completely (adjust the sleep time as needed)
time.sleep(5) # Wait for 5 seconds, adjust as needed
# CLEAR FILTERS
driver.find_element(By.XPATH, "//span[@class='clearAll']").click()
time.sleep(3)
driver.find_element(By.XPATH, '//*[@id="pageContent"]/div[6]/div[1]/div[4]/button[3]').click()
time.sleep(3)
driver.find_element(By.XPATH, "//a[normalize-space()='100 rows']").click()
print("Before performing action")
actions = ActionChains(driver)
actions.key_down(Keys.CONTROL).send_keys('l').key_up(Keys.CONTROL).perform()
print("After performing action")
Output of the above code-
Before performing action
After performing action
i want the selenium script to press the shrotcut key Control+L but it is not working and almost skipping the actionchain code also not showing any error. Whereas when manually pressing Control+L runs the extension. Please help how to do this.
New contributor
HADLA BHATIYAN is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.