Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help opening certain Chrome tab
#1
Hello, I am new to the forum and to LA!

I used the input recorder to capture a task. If the Chrome tabs are in a different order or size and I run it, the mouse click will be in the wrong location to choose that particular tab. In Power Automate, it selects the right tab without using a mouse click. Is there a way to do with with LA? Thanks!
#2
Use this tool: menu Code > Find UI element.

Code generated with this tool:
Code:
Copy      Help
var w = wnd.find(1, "*Google Chrome", "Chrome_WidgetWin_1");
var e = w.Elm["PAGETAB", "Help opening certain Chrome tab*"].Find(1);
e.Invoke();
#3
Code:
Copy      Help
Chrome.ActivateTab("New Tab");
1.s();
Chrome.ActivateTab("Help opening*");
1.s();
Chrome.ActivateTab("Class elm*");

//var w = wnd.find(0, cn: "Chrome_WidgetWin_1");
//Chrome.ActivateTab(w, "ChatGPT");


static class Chrome {
    /// <summary>
    ///
Finds Chrome window that contains the specified tab and activates the tab.
    /// </summary>
    ///
<param name="name">Tab name. Format: wildcard expression.</param>
    ///
<exception cref="Exception">Exceptions of <see cref="wnd.find(Seconds, string, string, WOwner, WFlags, Func{wnd, bool}, WContains)"/> and <see cref="elmFinder.Find()"/>.</exception>
    ///
<param name="activateWindow">Activete the Chrome window. Default true.</param>
    public static void ActivateTab(string name, bool activateWindow = true) {
        wildex wname = name;
        regexp rx = new(@"^(.+) - [^-]+ - [^-]+$");
        elmFinder ef = new("PAGETAB", prop: "notin=DOCUMENT,TOOLBAR", also: o => o.Name is string s && wname.Match(rx.Replace(s, "$1")));
        var w = wnd.find(1, "*Google Chrome", "Chrome_WidgetWin_1", contains: ef);
        if (activateWindow) w.Activate();
        ef.Result.Invoke();
    }

    
    /// <summary>
    ///
Finds and activates a Chrome tab in the specified Chrome window.
    /// </summary>
    ///
<param name="wChrome">Chrome window.</param>
    ///
<param name="name">Tab name. Format: wildcard expression.</param>
    ///
<exception cref="Exception">Exceptions of <see cref="elmFinder.Find(Seconds)"/>.</exception>
    public static void ActivateTab(wnd wChrome, string name) {
        wildex wname = name;
        regexp rx = new(@"^(.+) - [^-]+ - [^-]+$");
        var e = wChrome.Elm["PAGETAB", prop: "notin=DOCUMENT,TOOLBAR", also: o => o.Name is string s && wname.Match(rx.Replace(s, "$1"))].Find(1);
        e.Invoke();
    }
}
#4
@Gintaras Thanks! I think I figured it out!
#5
Quote:var e = wChrome.Elm["PAGETAB", name + " - Memory usage *", "notin=DOCUMENT,TOOLBAR"].Find(1);

If operating on a Chinese system, the code above needs to be changed as follows. Is there a simpler way to adapt it?
Quote:var e = wChrome.Elm["PAGETAB", name + " - 内存用量 *", "notin=DOCUMENT,TOOLBAR"].Find(1);

Additionally, if I currently have two Chrome browser windows open, how can I quickly switch to the window with a specific tab name?
#6
Code updated.
#7
thank you!

I currently have multiple Chrome browser windows open. 

Only one Chrome browser window is launched with proxy parameters(@"--proxy-server=socks5://127.0.0.1:1080 chat.openai.com"), and it contains a tab named "Chatgpt"
The other Chrome browser windows do not have any tabs named "Chatgpt."

How can I identify the Chrome browser window launched with proxy parameters?
#8
Code:
Copy      Help
var partOfCommandLine = "--proxy-server=socks5://127.0.0.1:1080 chat.openai.com";
var w = wnd.find("*Google Chrome", "Chrome_WidgetWin_1",
    also: o => process.getCommandLine(o.ProcessId, true) is string s && s.Contains(partOfCommandLine));
print.it(w);


With normal apps this could work, but not with Chrome. Try Selenium or Playwright, maybe possible, but little hope.
#9
thank you!

The above code can find the window, but cannot use .Activate activate it.
 
Code:
Copy      Help
var w = wnd.find("*Google Chrome", "Chrome_WidgetWin_1", also: o => process.getCommandLine(o.ProcessId, true) is string s && s.Contains(partOfCommandLine)).Activate;
#10
Activate()
#11
thank you!

Why didn't the compiler report an error when using .Activate?
#12
It is valid code. Like in QM would be &Activate.
#13
I encountered another new issue.
The text on the ChatGPT tab randomly generates, as shown by the red box in the image below. Therefore, I cannot use the tab text to find the window or switch tabs.

However, I noticed that when hovering the mouse over the tab, tooltip text is displayed. This text can serve as an identifier, as shown by the purple box, but I am unable to locate it.

https://i.ibb.co/2YryLrR/A.png

My goal is to switch to the tab for ChatGPT without focusing on the title text.
#14
Find and click the tab icon. To create code use the Find image tool.
#15
I encountered a complex situation: several web pages opened in the browser, and their tab texts are the same (as shown in the figure below)!
https://i.ibb.co/PWxCx2q/TT.gif
I need to switch to the tab containing the text "apikeys" in the address bar,
but it seems that the link in the address bar can only be obtained when selected. Is there a better solution?
#16
This maybe a solution. I don't know! Just choose a unique element on certain page from the dropdown list.


Attached Files Image(s)
   
#17
Thanks for your help. 
I need to switch to a specific tab, but the options mentioned above seem to only locate the window containing that element.
#18
Code:
Copy      Help
// script "Chrome switch to tab by URL.cs"
//example
print.clear();
var r = Chrome.ActivateTabByURL("*libreautomate.com*") ?? throw new NotFoundException();
print.it(r.url);

class Chrome {
    /// <summary>
    ///
Finds Chrome tab by URL, and activates it.
    /// Searches in all Chrome windows.
    /// </summary>
    ///
<param name="url">URL. Format: wildcard expression.</param>
    ///
<param name="activateWindow">Activete the Chrome window. Default true.</param>
    ///
<returns>Tab info; null if not found.</returns>
    ///
<exception cref="Exception">Something failed.</exception>
    ///
<remarks>
    ///
Temporarily activates each tab (to get its URL) until finds the specified tab. There is no way to avoid it; even Selenium cannot get URL of inactive tabs.
    /// </remarks>
    public static ATBUResult ActivateTabByURL(string url, bool activateWindow = true) {
        wildex wx = url;
        foreach (var w in wnd.findAll("*Google Chrome", "Chrome_WidgetWin_1")) {
            if (_ATBU_Window(w, wx, activateWindow) is { } r) return r;
        }

        return null;
    }

    
    /// <summary>
    ///
Finds Chrome tab by URL, and activates it.
    /// </summary>
    ///
<param name="wChrome">Chrome window.</param>
    ///
<param name="url">URL. Format: wildcard expression.</param>
    ///
<param name="activateWindow">Activete the Chrome window. Default true.</param>
    ///
<returns>Tab info; null if not found.</returns>
    ///
<exception cref="Exception">Something failed.</exception>
    ///
<remarks>
    ///
Temporarily activates each tab (to get its URL) until finds the specified tab. There is no way to avoid it; even Selenium cannot get URL of inactive tabs.
    /// </remarks>
    public static ATBUResult ActivateTabByURL(wnd wChrome, string url, bool activateWindow = true) => _ATBU_Window(wChrome, url, activateWindow);
    
    static ATBUResult _ATBU_Window(wnd w, wildex wx, bool activateWindow) {
        var tablist = w.Elm["PAGETABLIST"].Find(-1);
        var tabs = tablist?.Elm["PAGETAB"].FindAll();
        if (tabs?.Any() == true) {
            var currentTab = tabs.FirstOrDefault(o => o.IsSelected);
            foreach (var tab in tabs) {
                tab.Invoke();
                var doc = w.Elm["web:DOCUMENT", prop: "value=?*"].Find(5);
                var u = doc.Value;
                if (wx.Match(u)) {
                    if (activateWindow) w.Activate();
                    return new(w, tab, doc, u);
                }
            }

            currentTab?.Invoke();
        }

        return null;
    }

    
    public record class ATBUResult(wnd window, elm tabButton, elm document, string url);
}
#19
thank you!
#20
I'd like to enhance the functionality of the above code.
add a switch in the parameters so that when it can't find a tab with the specified keyword, it opens a new tab with a specified URL. After the webpage has completely loaded, it should return.

Is there a more stable and widely applicable solution than the method of opening a new tab described below, which is only suitable for specific language operating systems?

var e = w.Elm["BUTTON", "新标签页"].Find(3); e.MouseClick();
 
Code:
Copy      Help
string url = "libreautomate.com";
var w = wnd.find(1, "* - Google Chrome", "Chrome_WidgetWin_1").Activate();
var e = w.Elm["BUTTON", "新标签页"].Find(3); e.MouseClick();
var e2 = w.Elm["TEXT", prop: "key=Ctrl+L"].Find(3);
e2.MouseClick(); keys.send("Ctrl+A", $"!{url}", "Enter");
#21
In most cases I would use run.it(url).

Or this:

Code:
Copy      Help
// script "Chrome open URL in new tab.cs"
string url = "libreautomate.com";
var w = wnd.find(0, "*Google Chrome", "Chrome_WidgetWin_1");
w.Activate();
keys.send("Ctrl+T", 100, "!" + url, "Del Enter");

//then find/wait a known UI element in the web page
var e = w.Elm["web:LINK", "Changes"].Find(15);
osdText.showTransparentText("DONE");
#22
#18 CODE
When the Chrome browser window is minimized, the code does not work. How  retrieve a specific link from multiple minimized Chrome windows?
#23
I added the following tags, but it still doesn't work.

foreach (var w in wnd.findAll("*Google Chrome", "Chrome_WidgetWin_1"))
TO:
foreach (var w in wnd.findAll("*Google Chrome", "Chrome_WidgetWin_1", flags: WFlags.HiddenToo))
#24
Code:
Copy      Help
        var tabs = tablist?.Elm["PAGETAB", flags: EFFlags.HiddenToo].FindAll();
#25
The following error messages often appear in practical use:

Au.Types.NotFoundException: Not found.
   at line 68 in _Chrome.cs, Chrome._ATBU_Window(wnd w, wildex wx, Boolean activateWindow)
   at line 40 in _Chrome.cs, Chrome.ActivateTabByURL(String url, Boolean activateWindow)


I encounter these situations while using the code:
1. The Chrome browser window may be minimized.
2. The Chrome browser window may be on the second monitor.
3. There are two Chrome browser windows, each on separate screens (on the first and second monitors).
4. The Chrome browser is not launched.

Code:
Copy      Help
/*/ c _Chrome3; /*/ //.
script.setup(trayIcon: true, sleepExit: true, exitKey: KKey.Pause);
//..

string chromeEXE = @"D:\Chrome\App\chrome.exe";
string url = "www.libreautomate.com/forum/";
string text = @"hello";

Chrome.runLAWeb(chromeEXE, url, text);
 
Code:
Copy      Help
// class "_Chrome3.cs"


//https://www.quickmacros.com/forum/showthread.php?tid=7618&pid=37397#pid37397

static class Chrome
{
    public static void runLAWeb(string chromEXE, string url, string text)
    {

        if (Chrome.ActivateTabByURL(url) == null)
        {

            wnd.findOrRun("*- Google Chrome", "Chrome_WidgetWin_1", run: () => { run.it(chromEXE, url); }, wait: -2);
        }

        
        var w = wnd.find(5, "* - Google Chrome", "Chrome_WidgetWin_1").Activate();
        
        var e = w.Elm["web:TEXT", prop: "@name=keywords"].Find(3);
        
        e.MouseClick();
        
        e.SendKeys("Ctrl+A", 100, $"!{text}", 200, "Enter"); 100.ms();
    }

    
    /// <summary>
    ///
Finds Chrome tab by URL, and activates it.
    /// Searches in all Chrome windows.
    /// </summary>
    ///
<param name="url">URL. Format: wildcard expression.</param>
    ///
<param name="activateWindow">Activete the Chrome window. Default true.</param>
    ///
<returns>Tab info; null if not found.</returns>
    ///
<exception cref="Exception">Something failed.</exception>
    ///
<remarks>
    ///
Temporarily activates each tab (to get its URL) until finds the specified tab. There is no way to avoid it; even Selenium cannot get URL of inactive tabs.
    /// </remarks>
    public static ATBUResult ActivateTabByURL(string url, bool activateWindow = true)
    {

        //wildex wx = url;
        wildex wx = $@"*{url}*";
        foreach (var w in wnd.findAll("*Google Chrome", "Chrome_WidgetWin_1"))
        {

            if (_ATBU_Window(w, wx, activateWindow) is { } r) return r;
        }

        return null;
    }

    
    /// <summary>
    ///
Finds Chrome tab by URL, and activates it.
    /// </summary>
    ///
<param name="wChrome">Chrome window.</param>
    ///
<param name="url">URL. Format: wildcard expression.</param>
    ///
<param name="activateWindow">Activete the Chrome window. Default true.</param>
    ///
<returns>Tab info; null if not found.</returns>
    ///
<exception cref="Exception">Something failed.</exception>
    ///
<remarks>
    ///
Temporarily activates each tab (to get its URL) until finds the specified tab. There is no way to avoid it; even Selenium cannot get URL of inactive tabs.
    /// </remarks>
    public static ATBUResult ActivateTabByURL(wnd wChrome, string url, bool activateWindow = true) => _ATBU_Window(wChrome, url, activateWindow);
    
    static ATBUResult _ATBU_Window(wnd w, wildex wx, bool activateWindow)
    {

        var tablist = w.Elm["PAGETABLIST"].Find(-1);
        var tabs = tablist?.Elm["PAGETAB", flags: EFFlags.HiddenToo].FindAll(); 
        if (tabs?.Any() == true)
        {

            var currentTab = tabs.FirstOrDefault(o => o.IsSelected);
            foreach (var tab in tabs)
            {

                tab.Invoke();
                var doc = w.Elm["web:DOCUMENT", prop: "value=?*"].Find(5);
                var u = doc.Value;
                if (wx.Match(u))
                {

                    if (activateWindow) w.Activate();
                    return new(w, tab, doc, u);
                }
            }

            currentTab?.Invoke();
        }

        return null;
    }

    
    public record class ATBUResult(wnd window, elm tabButton, elm document, string url);
}

The issue persists with missing windows and controls, even with delays set, and it appears randomly.
#26
After not using inactive tabs, on tab.Invoke Chrome activates the tab but it's blank. To reproduce, need to wait a day.

This workaround seems to work: after tab.Invoke(); insert tab.Focus();.

Another possible reason - a side panel is visible (eg Bookmarks). Then w.Elm["webBig GrinOCUMENT", prop: "value=?*"].Find(5) finds it instead. Fixed in next LA.
#27
Sometimes, even after numerous tests, there are no errors, but suddenly errors occur. I believe the possible reason for this might be that I have placed some operations in class files. All the code is in one main file. It seems that the probability of encountering problems is very low.
#28
Run-time exceptions are certainly unrelated to using class files or not.
#29
Originally, all my code was in one main file. Later, in order to reuse certain code, I moved it into class files, and then it seems the problem arose...
#30
Many times, a delay must be added before pressing the Ctrl+A hotkey, otherwise the text on the page will be selected.

clipboard.text = text; e2.SendKeys(200, "Ctrl+A", 200, "Ctrl+V");


Forum Jump:


Users browsing this thread: 1 Guest(s)