0

I am trying to fetch HTML element from Webview, I tried existing StackOverflow answer but it's not working for me.

Getting source HTML from a WebView in Cocoa -- not working for me.

@IBOutlet var FacebookWebview: WebView!

let someHTML = FacebookWebview.stringByEvaluatingJavaScript(from: "document.getElementsByClassName('linkWrap noCount')[0].innerHTML;") as NSString?

print("FetchUser:-\(someHTML)")
HTML element --> <div dir="ltr" class="linkWrap noCount">Result</div>

linkWrap noCount is my class name.

Result is the value of the string. -- here I am getting null.

Any help will be appreciated.

Pankaj Mundra
  • 1,401
  • 9
  • 25
  • 1
    https://stackoverflow.com/questions/5167254/getting-the-html-source-code-of-a-loaded-uiwebview – Daljeet Nov 29 '17 at 11:31

2 Answers2

0

try this:

let html = yourWebView.stringByEvaluatingJavaScriptFromString("document.documentElement.outerHTML")
Daljeet
  • 1,573
  • 2
  • 20
  • 40
0

OBJECTIVE-C

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request    navigationType:(UIWebViewNavigationType)navigationType 
{

    if (navigationType == UIWebViewNavigationTypeLinkClicked) {

        [[UIApplication sharedApplication] openURL:[request URL]];

        return NO;
    }

    return YES;

}

- (void)webViewDidFinishLoad:(UIWebView *)webView

{

    // JS Injection hack to solve the target="_blank" issue and open a real browser in such case.

    NSString *JSInjection = @"javascript: var allLinks = document.getElementsByTagName('a'); if (allLinks) {var i;for (i=0; i<allLinks.length; i++) {var link = allLinks[i];var target = link.getAttribute('target'); if (target && target == '_blank') {link.setAttribute('target','_self');link.href = 'newtab:'+link.href;}}}";

    [webView stringByEvaluatingJavaScriptFromString:JSInjection];

}


 - (void)VideoButton_Click:(id)sender{


        @try {


            NSString *url=@"Write link here";

            [videowebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];

             videowebView.scrollView.bounces = NO;
            [videowebView setMediaPlaybackRequiresUserAction:NO];

        }
        @catch (NSException *exception) {

        }

    }