0

Please tell me, I'm trying to take input value from webview. Why do I get "nil". What could be the problem? Screen provided below

enter image description here Code provided below

import SwiftUI
import WebKit

struct ContentView: View {
  var body: some View {
    WebView().edgesIgnoringSafeArea(.all)
  }
}

struct WebView: UIViewRepresentable {
  func makeUIView(context: Context) - > WKWebView {
    let webView = WKWebView()
    webView.scrollView.isScrollEnabled = false
    return webView
  }


  func updateUIView(_ webView: WKWebView, context: Context) {
    let liveView = "https://**/"
    if let url = URL(string: liveView) {
      let request = URLRequest(url: url)
      webView.load(request)
      webView.evaluateJavaScript("document.getElementById('ses').value") {
        (result, error) in
        print(result)
      }
    }
  }
}
4
  • AFAIK getElementsByName returns array which doesnt have value as property. try document.getElementsByName('ses')[0].value to get the value of first element or loop through those to get values of all elements Commented Dec 19, 2019 at 10:24
  • @Cerlin Sorry there should be getElementsById Commented Dec 19, 2019 at 10:25
  • There is no method as getElementsById. You can get only one element with id so the method name is getElementById (singular) Commented Dec 19, 2019 at 10:28
  • @Cerlin Yes, I know, but the answer is still nil Commented Dec 19, 2019 at 10:30

3 Answers 3

2

You need to wait until the page loads.

let timer = Timer.scheduledTimer(withTimeInterval: 5.0, repeats: true) {
  (timer) in
  webView.evaluateJavaScript("document.getElementById('ses').value") {
    (result, error) in
    print(result!)
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Actually you don't need that. Usually you should call evaluateJavascript in a method called didFinish (or something similar). This is why you declare a completion handler in your { } brackets. The completion handler will be called when the page is loaded and javascript evaluated.
1

You need to check if webView is completely loaded.

To check if webView is loaded:

class ViewController: WKNavigationDelegate{

    let webView = WKWebView()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        webView.navigationDelegate = self
        
        let myURL = URL(string:"https://someWebSite.com")
        let myRequest = URLRequest(url: myURL!)
        webView.load(myRequest)
        
        
    }

    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        print("Loaded")
    }
}

for more info about navigationDelegate

Comments

-1

It seems like you don't have this element in your document.

// element exists
console.log(document.getElementById('demo').value)

// element doesn't exist
console.log(document.getElementById('foo'))
<div id="demo" value="demo-value">Demo</div>

1 Comment

This item is on the page!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.