locked
Xamarin forms- Dynamically created webview not firing events RRS feed

  • Question

  • User277487 posted

    I am trying to automate a website data extraction by dynamically creating a webview and loading the source url. I want the page and webview to be NOT to be displayed to the user. Unfortunately the events seems not working. Any help appreciated

    Here is what I am trying to do:

    ContentPage cp = new ContentPage();
        cp.Title = "My Page";
        StackLayout sl = new StackLayout();
        var webView = new WebView();
    
        sl.Children.Add(webView);
        cp.Content = sl;
    
        webView.Navigating += WebView_Navigating;
        webView.Navigated += WebView_Navigated;
    
        webView.Source = "https://www.google.com/";//just a sample i tried multiple urls including http
    
    
    private async void WebView_Navigating(object sender, WebNavigatingEventArgs e)
    {
        //event not firing!
    }
    
    private async void WebView_Navigated(object sender, WebNavigatedEventArgs e)
    {
        //event not firing!
    }
    
    Tuesday, November 12, 2019 4:13 AM

All replies

  • User369978 posted

    I tried your code and everything works fine , Navigating and Navigated event fires as expected .

    Which platform(Android,iOS, UWP) did you test on and what's the version of your Xamarin.Forms ?

    PS : I test on Android with XF 4.2.0.7092449

    Tuesday, November 12, 2019 7:59 AM
  • User277487 posted

    Thanks for helping.

    But still it doesn't work for me. I just tried creating a fresh xamarin forms app(android platform) and retried, the events are not firing :-( My xamarin forms version is 4.2.0.709249. Any clues?

    Wednesday, November 13, 2019 12:14 AM
  • User369978 posted

    The Content you create which placed webview is not present on screen , you have to show it first ,then the webview event would be working .

           webView.Source = "https://www.google.com/";//just a sample i tried multiple urls including http
            this.Navigation.PushModalAsync(cp, true);   //add this line 
    
    Wednesday, November 13, 2019 1:37 PM
  • User277487 posted

    Like I mentioned earlier, I do NOT want the page and the webview to be displayed to the user. Seems like the webview events doesn't fire if it is not added to the visual tree. May be I should explain it bit more on what I am trying to achieve. What is the best way to automate a website crawling from a xamarin forms/xamarin android platform app/service? I started with the webview since it uses the native browser and has pretty good JavaScript support and looks like all webistes getting loaded as expected. Is there a headless browser which is capable enough and can be used for the same purpose? Is there a way to achieve the same from an android scheduler/ service?

    Wednesday, November 13, 2019 3:05 PM
  • User89714 posted

    @"johnsonjoseph.1834"

    You might want to consider using HttpClient and operating with HTTP messaging rather than using a WebView in order to do screen-scraping, web-crawling etc. (needless to say, do check whether the sites you are planning to use permit such operations - some do not).

    Wednesday, November 13, 2019 3:17 PM