xcode evaluateJavascript在输入后启用按钮不会使按钮可单击

7vhp5slm  于 2023-08-07  发布在  Java
关注(0)|答案(1)|浏览(134)

我正在想办法输入收据号码,然后点击这个网站上的“检查状态”按钮:https://egov.uscis.gov/
我可以成功输入收据编号:

document.getElementById('receipt_number').value = '\(receiptNumber)';

字符串
但“检查状态”按钮仍处于禁用状态。我可以通过以下命令启用该按钮:

document.getElementsByName('initCaseSearch')[0].removeAttribute("disabled");


但即使启用了它,我仍然无法单击它(错误为“未定义”)。它看起来像网页有一些额外的逻辑或验证,防止自动表单提交,所以我尝试:

document.forms[0].submit();


但它只是重新加载页面,不接受输入的收据号码。
有什么想法吗?

3ks5zfa0

3ks5zfa01#

我不知道evaluateJS,但据我所知,由于网站使用ReactJS应用程序,你不能使用evaluateJS做任何事情。您应该考虑使用WKScriptMessageHandler来执行此操作。
在浏览器中这样做的方法是这样的:

const element = document.querySelector("input#receipt_number");
const propertyNames = Object.keys(element);

let reactProps;
for (const propName of propertyNames) {
  if (propName.startsWith('__reactProps$')) {
    reactProps = propName;
    break;
  }
}

if (reactProps) {
  const onChangeHandler = element[reactProps]?.onChange;

  if (typeof onChangeHandler === 'function') {
    const event = {
      target: {
        value: "abc1234567891", // enter your custom values here
      },
    };

    onChangeHandler.call(element, event);

    const buttonElement = document.querySelector("button[type='submit']");
    if (buttonElement && buttonElement[reactProps]?.onClick) {
      buttonElement[reactProps].onClick({preventDefault:()=>{}});
    }
  }
}

字符串
我应该提醒你用所需的值替换该值,并根据需要更改代码。

更新:

使用WKScriptMessageHandler执行此操作的示例推荐代码:

import WebKit

class MyScriptMessageHandler: NSObject, WKScriptMessageHandler {
    func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
        // Handle the message received from JavaScript
        if let messageBody = message.body as? String {
            // Perform actions based on the message content
            if messageBody == "triggerEvent" {
                // Trigger React event or modify component state here
                if let element = webView.evaluateJavaScript("document.querySelector(\"input#receipt_number\")") as? WKScriptValue,
                   let reactProps = (element.propertyNames.first { $0.hasPrefix("__reactProps$") }) as? String,
                   let onChangeHandler = element.valueForKey(reactProps + ".onChange") as? WKScriptValue {
                    let event = webView.evaluateJavaScript("new Event('change')") as? WKScriptValue
                    onChangeHandler.call(withArguments: [event])
                }

                if let buttonElement = webView.evaluateJavaScript("document.querySelector(\"button[type='submit']\")") as? WKScriptValue,
                   let reactProps = (buttonElement.propertyNames.first { $0.hasPrefix("__reactProps$") }) as? String,
                   let onClickHandler = buttonElement.valueForKey(reactProps + ".onClick") as? WKScriptValue {
                    onClickHandler.call()
                }
            }
        }
    }
}

// Create the WKWebView configuration
let webConfiguration = WKWebViewConfiguration()

// Create an instance of your custom script message handler
let scriptMessageHandler = MyScriptMessageHandler()

// Add the script message handler to the user content controller
webConfiguration.userContentController.add(scriptMessageHandler, name: "myMessageHandler")

// Create the WKWebView with the configuration
let webView = WKWebView(frame: .zero, configuration: webConfiguration)

// Inject the JavaScript code into the WKWebView
let javascriptCode = """
// Add an event listener to trigger the message to the native code
document.querySelector("button").addEventListener("click", function() {
    window.webkit.messageHandlers.myMessageHandler.postMessage("triggerEvent");
});
"""

let userScript = WKUserScript(source: javascriptCode, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
webView.configuration.userContentController.addUserScript(userScript)

// Load your desired URL in the WKWebView
if let url = URL(string: "https://egov.uscis.gov/") {
    let request = URLRequest(url: url)
    webView.load(request)
}


警告:不要使用此代码的自动化,可能会伤害网站的验证过程或做非法的事情。

相关问题