2

I am trying to get the current Tab URL the user is in. I was referring to similar question on SO Previously asked question but both first and second answers are no longer working as you can't really access elements of Chrome window using win32gui anymore

Maybe there is a way to take the latest URL from the History file stored in the pc..

Please make sure the answer works for Chrome latest version with Python 3.x

yarin Cohen
  • 995
  • 1
  • 13
  • 39

2 Answers2

1

You may consider usink keys / hotkeys.

Selecting

F6 - acces your url for focused active tab

import pyautogui
pyautogui.click(0, 200) # a random click for focusing the browser
pyautogui.press('f6')

CTRL + TAB - next tab

pyautogui.hotkey('ctrl', 'tab')

CTRL + SHIFT + TAB - previous tab

pyautogui.hotkey('ctrl', 'shift', 'tab')

ALT + TAB - for another chrome window ( usefull if you know you are only chrome opened)

pyautogui.hotkey('alt', 'tab')

CTRL + T - open new tab

pyautogui.hotkey('ctrl', 't')

CTRL + W - close curent tab

pyautogui.hotkey('ctrl', 'w')

Copying

For copy the url, you can use either pyperclip...

pyautogui.hotkey('ctrl', 'c') # for copying the selected url
import pyperclip # pip install pyperclip required
url = pyperclip.paste()
print(url)

either clipboard module...

pyautogui.hotkey('ctrl', 'c') # for copying the selected url
import clipboard # pip install clipboard required
url = clipboard.paste()
print(url)

More about hotkeys into : documentation1, documentation2

PS: I have Chrome x78.0 on windows 64 bit os, and it's working for me. :)

Cristian F.
  • 328
  • 2
  • 12
0
o = sh.tail(sh.grep(sh.cat("/home/user/.config/google- 
chrome/Default/Sessions/Session_somelatestfile"), "-a", "-Eo", " 
(http|https)://[^/']+"), "-1")
print(o)
Amulya Acharya
  • 701
  • 14
  • 17