Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Elm couldn't find popup download button
#1
Hi Dear Gintaras,

The line 22, Elm couldn't find the download button after waiting. 

Please help!

Besides, the chrome waiting function is still very reliable after more thousands of testing and never failed. 
one question, If I use invoke or web-invoke to click links on the chrome page(w), will be all the page wait until loaded or just the main page (p.open(url)) ?  Thanks
 
Code:
Copy      Help
// script "Notes-box.com.cs"
//.
script.setup(trayIcon: true, sleepExit: true, exitKey: KKey.MediaStop, pauseKey: KKey.MediaPlayPause);
//..

var w = wnd.find(0, "* - Google Chrome", "Chrome_WidgetWin_1");
var p = new ChromePage(w);

p.Open("https://notes-box.com/notes/classical-music/");
200.ms();
for (int i = 1; i < 83; i++) {
    p.Open($"https://notes-box.com/notes/classical-music/?PAGEN_1={i}");
    foreach (var song in w.Elm["web:IMAGE", prop: "@loading=lazy"].FindAll().Select(o => o.Navigate("pa").Value).ToArray()) {
        p.Open(song);
        200.ms();
        var composer = w.Elm["web:LINK", prop: "@ru=Классческая", navig: "pr la"].Find(1).Name;
        var download = $@"C:\notes-box\classical\{composer}";
        //filesystem.delete(download);
        filesystem.createDirectory(download);
        var click = w.Elm["web:GROUPING", prop: "@target=_blank"].Find(1);
        click.Invoke();
        100.ms();
        var button = w.Elm["web:LINK", "DOWNLOAD FILE", new("@href=/upload/iblock/*.pdf\n", "@target=_blank")].Find(18);
        //16.s();
        200.ms();
        button.Focus();
        keys.send("Apps");
        var w2 = wnd.find(5, "", "Chrome_WidgetWin_2","chrome.exe");
        var save = w2.Elm["MENUITEM", "Save link as…"].Find(3);
        save.Invoke();
        300.ms();
        var save1 = wnd.find(1, "另存为", "#32770");
        var input = w.Elm["TEXT", "文件名:", "class=Edit"].Find(1);
        input.SendKeys(download+input.Value);
        200.ms();
    
    script.debug();
    Debugger.Break();}
}

    
    
    


/// <summary>
///
Opens a web page in Chrome or Edge, and waits until loaded.
/// </summary>
public class ChromePage {
    wnd _w;
    elm _eAddress, _eReload;
    
    //may need to change these strings. They may depend on your computer's language and browser program version.
    ///
    public static string AddressBarName = "Address and search bar";
    
    ///
    public static string ReloadButtonName = "**m Reload||Refresh"; //Chrome||Edge
    
    ///
    public static string ReloadButtonDescription = "**m Reload this page||Refresh (Ctrl+R)";
    
    /// <summary>
    ///
Finds UI elements for later opening a URL and waiting.
    /// </summary>
    ///
<param name="w">Chrome or Edge window.</param>
    public ChromePage(wnd w) {
        _w = w;
        var e = _w.Elm["TOOLBAR"].Find(1);
        _eAddress = e.Elm["TEXT", AddressBarName].Find(1);
        _eReload = e.Elm["BUTTON", ReloadButtonName].Find(1);
    }

    
    /// <summary>
    ///
Opens a web page and waits until loaded or stopped.
    /// Also activates the browser window and makes the address bar focused.
    /// </summary>
    ///
<param name="url">Web page URL.</param>
    ///
<param name="secondsTimeout">How long to wait until loaded. The same as with most other "wait for" functions of the automation library (0 infinite, negative no exception).</param>
    ///
<returns>false if timeout (when <i>secondsTimeout</i> negative).</returns>
    ///
<exception cref="TimeoutException"></exception>
    ///
<exception cref="Exception">Exceptions of used functions.</exception>
    public bool Open(string url, double secondsTimeout = 60) {
        wildex desc = ReloadButtonDescription;
        _w.Activate();
        _eAddress.Focus(); //must be before setting Value
        _eAddress.Value = url;
        _w.Send(0x100, (int)KKey.Enter); //WM_KEYDOWN
        _w.Send(0x101, (int)KKey.Enter); //WM_KEYUP
        100.ms();
        return wait.forCondition(secondsTimeout, () => desc.Match(_eReload.Description));
    }
}
#2
The element has different href attribute when found in script than when captured. It's normal.

----
Invoke does not wait.
WebInvoke waits until the window name changes.
#3
Code:
Copy      Help
p.Open("https://notes-box.com/notes/classical-music/");
200.ms();
for (int i = 1; i <= 1; i++) {
    p.Open($"https://notes-box.com/notes/classical-music/?PAGEN_1={i}");
    200.ms();
    foreach (var song in w.Elm["web:IMAGE", prop: "@loading=lazy"].FindAll().Select(o => o.Navigate("pa").Value).ToArray()) {
        p.Open(song);
        200.ms();
        var composer = w.Elm["web:LINK", prop: "@ru=Классческая", navig: "pr la"].Find(1).Name;
        var click = w.Elm["web:GROUPING", prop: "@target=_blank"].Find(1);
        click.Invoke();
        //18.s(); //it seems you can cheat and don't wait. Will find the button while it's still invisible. But don't forget to donate.
        var button = w.Elm["web:LINK", "DOWNLOAD FILE"].Find(3);
        print.it(button);
        //print.it(w.Elm["web:LINK"].FindAll());
        
        200.ms();
        button.SendKeys("Apps");
        var w2 = wnd.find(5, "", "Chrome_WidgetWin_?", "chrome.exe", contains: "Save link as…");
        print.it(w2);
        var save = w2.Elm["MENUITEM", "Save link as…"].Find(3);
        save.Invoke();
        //300.ms();
        break;
    }
}
#4
It worked. Thanks a lot! Will donate to the site!
#5
Hi dear Gintaras,

What is more reliable comparing elm.find() to elm.wait(), I don't often see  elm.wait(double) in the code you posted in forum or cookbook.

Thanks
#6
The same. Probably the only difference is how the parameter is interpreted when 0.
#7
Hi dear Gintaras, 

The code (for Edge Browser) worked very well except for name syntax error occasionally.  The error code is like this 
Warning: Failed to download. IOException, The filename, directory name, or volume label syntax is incorrect.   at line 36.

Besides, Plese advise if there is anything improper in the code. Thank you very much! 

PS, Most of the code was learned from your examples in the forum, Most sincere thanks to you!  Heart

 I have made some donations to the site. lol
 
Code:
Copy      Help
// script "Notes-box.com3.cs"
///*/ role exeProgram; outputPath %folders.Workspace%\exe\Notes-box.com; /*/
using System.Net.Http;
script.setup(trayIcon: true, sleepExit: true, exitKey: KKey.MediaStop, pauseKey: KKey.MediaPlayPause);
//..
filesystem.createDirectory(folders.Desktop + @"notes-box");
var w = wnd.find(0, "*", "Chrome_WidgetWin_1");
var p = new ChromePage(w);

p.Open("https://notes-box.com/notes/classical-music/");
500.ms();
for (int i = 1; i < 85; i++) {
    dialog.show("downloading。。。", secondsTimeout: 2);
    p.Open($"https://notes-box.com/notes/classical-music/?PAGEN_1={i}");
    500.ms();
    foreach (var song in w.Elm["web:IMAGE", prop: "@loading=lazy"].FindAll().Select(o => o.Navigate("pa").Value).ToArray()) {
        p.Open(song);
        200.ms();
        var composer = w.Elm["web:LINK", prop: "@ru=Классческая", navig: "pr la"].Find(10).Name;
        var download = folders.Desktop + $@"notes-box\classical\{composer}\";
        pathname.correctName(download);
        //filesystem.delete(download);
        filesystem.createDirectory(download);
        var click = w.Elm["web:LINK", prop: "@ru=Классческая", navig: "ne6"].Find(3);
        click.Invoke();
        200.ms();
        var button = w.Elm["web:LINK", "DOWNLOAD FILE"].Wait(0);
        100.ms();
        button.SendKeys("Apps");
        var w2 = wnd.find(5, "", "Chrome_WidgetWin_?", "msedge.exe", contains: "Save link as");
        var save = w2.Elm["MENUITEM", "Copy link"].Find(3);
        save.Invoke();
        200.ms();
        var link = clipboardData.getText();
        print.it(link);
        string file = download + pathname.getName(link);
        try { internet.http.Get(link, file); }
        catch (Exception e1) { print.warning($"Failed to download. {e1.ToStringWithoutStack()}"); continue; }
        print.it("downloaded");
        
        //var w3 = wnd.find(20, "另存为", "#32770").Activate();
        //if (w3.Is0) continue;
        //var input = w3.Elm["WINDOW", "文件名:", "class=Edit"].Find(10);
        //200.ms();
        //var filename = input.Navigate("fi").Value;
        //200.ms();
        //keys.send("Ctrl+A", $"!{download + filename}");
        //100.ms();
        //keys.send("Enter");
        //var filepath = pathname.correctName(download + filename);
        //wait.forCondition(10, () => filesystem.exists(filepath));
        //if (filesystem.exists(filepath)) {
            
        //} else goto load;

        
        
    }
}


var folder = folders.Desktop + @"notes-box";
var a = filesystem.enumFiles(folder, "*.pdf", FEFlags.AllDescendants).ToArray();
dialog.show("Done", $"{a.Length} files has downloaded", secondsTimeout: 5);

/// <summary>
///
Opens a web page in Chrome or Edge, and waits until loaded.
/// </summary>
public class ChromePage {
    wnd _w;
    elm _eAddress, _eReload;
    
    //may need to change these strings. They may depend on your computer's language and browser program version.
    ///
    public static string AddressBarName = "Address and search bar";
    
    ///
    public static string ReloadButtonName = "**m Reload||Refresh"; //Chrome||Edge
    
    ///
    public static string ReloadButtonDescription = "**m Reload this page||Refresh (Ctrl+R)";
    
    /// <summary>
    ///
Finds UI elements for later opening a URL and waiting.
    /// </summary>
    ///
<param name="w">Chrome or Edge window.</param>
    public ChromePage(wnd w) {
        _w = w;
        var e = _w.Elm["TOOLBAR"].Find(1);
        _eAddress = e.Elm["TEXT", AddressBarName].Find(1);
        _eReload = e.Elm["BUTTON", ReloadButtonName].Find(1);
    }

    
    /// <summary>
    ///
Opens a web page and waits until loaded or stopped.
    /// Also activates the browser window and makes the address bar focused.
    /// </summary>
    ///
<param name="url">Web page URL.</param>
    ///
<param name="secondsTimeout">How long to wait until loaded. The same as with most other "wait for" functions of the automation library (0 infinite, negative no exception).</param>
    ///
<returns>false if timeout (when <i>secondsTimeout</i> negative).</returns>
    ///
<exception cref="TimeoutException"></exception>
    ///
<exception cref="Exception">Exceptions of used functions.</exception>
    public bool Open(string url, double secondsTimeout = 60) {
        wildex desc = ReloadButtonDescription;
        _w.Activate();
        _eAddress.Focus(); //must be before setting Value
        _eAddress.Value = url;
        _w.Send(0x100, (int)KKey.Enter); //WM_KEYDOWN
        _w.Send(0x101, (int)KKey.Enter); //WM_KEYUP
        100.ms();
        return wait.forCondition(secondsTimeout, () => desc.Match(_eReload.Description));
    }
}
#8
To avoid the exception I would use pathname.correctName.
#9
Fixed! Thanks 
Seems like it's a problem with Russian letters.
#10
Hi Gintaras, here it come again! The Russian letter even couldn't be displayed. So pathname.correctName will not work. 
[Image: d2fa8ZT.png]
#11
The picture shows that double quotes are not escaped. Don't need to escape Russian letters.
pathname.correctName would replace " with -.
#12
Maybe the script sometimes gets some old text from the clipboard. Try to clear the clipboard and wait for clipboard text.

Code:
Copy      Help
// script ""
var w = wnd.find(1, "Edvard Grieg Утро (сюита \"Пер Гюнт\") Sheet Music Downloads - Personal - Microsoft​ Edge", "Chrome_WidgetWin_1");

var button = w.Elm["web:LINK", "DOWNLOAD FILE"].Wait(0);
100.ms();
button.SendKeys("Apps");
var w2 = wnd.find(5, "", "Chrome_WidgetWin_?", "msedge.exe", contains: "Save link as");
var save = w2.Elm["MENUITEM", "Copy link"].Find(3);
clipboard.clear();
save.Invoke();
var link = wait.forCondition(3, () => clipboard.text);
print.it(link); //https://notes-box.com/upload/iblock/967/8tqmaf3zxf8ttfnnrk1oyj1z1k6oqjj1/%D0%AD%D0%B4%D0%B2%D0%B0%D1%80%D0%B4-%D0%93%D1%80%D0%B8%D0%B3-Edvard-Grieg-%D0%A3%D1%82%D1%80%D0%BE.pdf
var name = System.Net.WebUtility.UrlDecode(pathname.getName(link));
print.it(name); //Эдвард-Григ-Edvard-Grieg-Утро.pdf
#13
Works perfect! No errors anymore. Thank you so much!


Forum Jump:


Users browsing this thread: 2 Guest(s)