Programatically press a button in a website from an app in a phone

大憨熊 提交于 2019-12-31 05:33:09

问题


I am creating a UWP app using c#. The app is supposed to fetch information from a website and present it to the user. I've went through the DOM of the site and managed to download the HTML of the site. But some of the information is hidden and only appears when certain buttons or selections are made on the website. Is there a way that I can programmatically go into the site, make a selection, the download the new html?

2nd Question: The app should have an fb liveChat function linked an fb page. How do I link facebook liveChat into this app? I do not have an idea in my head on how to make this work. Thank you for you help.


回答1:


Here is an example for Entering text in search box and Click search button in Bing

  1. Use WebView to do all the work

    WebView webView = new WebView();
    
  2. Use InvokeScriptAsync method for WebView to use JS code

    webView.InvokeScriptAsync("eval", new string[] {});
    
  3. Get the HTML of the site using below code

    public LoadURI()
    {
        webView.Navigate(new Uri("https://www.bing.com/"));
        webView.NavigationCompleted += webView_NavigationCompletedAsync;
    }
    
    string siteHtML = null;
    
    private async void webView_NavigationCompletedAsync(WebView sender, WebViewNavigationCompletedEventArgs args)
    {
        siteHtML = await webView.InvokeScriptAsync("eval", new string[] { "document.documentElement.outerHTML;" });
    }
    
  4. Enter text in search box using below code

    private async void EnterTextAsync(string enterText)
    {
        var functionString = string.Format(@"document.getElementsByClassName('b_searchbox')[0].innerText = '{0}';", enterText);
        await webView.InvokeScriptAsync("eval", new string[] { functionString });
    }
    
  5. Simulate click using below code

    private async void SimulateClickAsync()
    {
        var functionString = string.Format(@"ddocument.getElementsByClassName('b_searchboxSubmit')[0].click();");
        await webView.InvokeScriptAsync("eval", new string[] { functionString });
    }
    
  6. Get new site's HTML using Step 3

Here is a Sample app for LogIn to StackOverflow: StackOverFlow-LogIn



来源:https://stackoverflow.com/questions/44685469/programatically-press-a-button-in-a-website-from-an-app-in-a-phone

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!