【问题标题】:Getting an input element through xpath in Objective C在Objective C中通过xpath获取输入元素
【发布时间】:2016-04-07 02:55:03
【问题描述】:

所以我正在玩一个客观的 c 桌面应用程序(不是 iOS)。我正在将网页的内容加载到 WebView 中,并且我想用代码中的硬编码文本填写用户名等输入字段。

之后

- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame

被调用了,如何才能基于 XPath 获得一个元素?

例如对于我想访问的 XPaths

/html/body/div[3]/div[3]/div[2]/div[2]/div/form/div[1]/div[2]/div[2]/div/div[8]/div/input
//*[@id="username"]

【问题讨论】:

    标签: objective-c macos xpath webview domdocument


    【解决方案1】:

    IMO,出于多种原因,使用 JavaScript 操作 HTML 比使用 XPath 更加健壮和灵活。我建议您在 Web 视图中执行适当的 JavaScript。这是一个示例实现:

    - (BOOL) insertFormFieldValues: (UIWebView *) webView
    {
        NSString *result = [webView stringByEvaluatingJavaScriptFromString:@"insert your javascript here"];
        return result == RESULT_OK;
    }
    

    【讨论】:

    • 感谢安吉拉。是的,我知道 stringByEvaluatingJavaScriptFromString,即使在这种情况下我有一个 WebView 而不是 UIWebView 仍然可以工作。我不知道的是如何通过该路径获取该元素。路径我一直都知道,形式也一样,只是不知道怎么走。
    • 在 JavaScript 中的代码是:document.getElementById('username').value = 'myusername';
    • 感谢安吉拉。作品。 - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame [sender stringByEvaluatingJavaScriptFromString: @"document.getElementById(\"username\").value = \"xyz\""];
    猜你喜欢
    • 2015-09-02
    • 2020-08-14
    • 2021-02-13
    • 1970-01-01
    • 2020-07-19
    • 2015-04-02
    • 2021-12-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多