【发布时间】:2016-01-21 18:40:36
【问题描述】:
我想编写一个假用户点击网页视图按钮来自动加载正确的网页。我在 Swift 2.0 中工作,url 不包含任何索引页面。
【问题讨论】:
标签: javascript button click swift2 simulate
我想编写一个假用户点击网页视图按钮来自动加载正确的网页。我在 Swift 2.0 中工作,url 不包含任何索引页面。
【问题讨论】:
标签: javascript button click swift2 simulate
更新的解决方案:它很丑,但有效。该网站需要您跳一些圈才能使用它,但secondURL 将加载一个只是表格的页面。
肯定有一个更简洁的解决方案,它不需要 JS 或多个页面加载,但我把它留给你。
我通过查看 Safari 的 Web Inspector 找到了secondURL
import UIKit
private let firstURL = NSURL(string: "https://legymnase67.la-vie-scolaire.fr/vsn.main/;jsessionid=E2D99A46028465A9D7F636521C64088B.node2?autolog=70caa4a471e8f6f0856735aed506b01a6307246c445beb160259f75c1c700f7978631d698cc5e0e890452bd182e5960e6153be7c4a3d2615ff0daf981a17877d46e472cdf2ef2328977c3957a58121e3556c4b9b808e22a76d0cbb994292a2144d64f367714c7dfbf4d3e24cea5f0274")!
private let secondURL = NSURL(string: "https://legymnase67.la-vie-scolaire.fr/vsn.main/releveNote/releveNotes")!
class WebController: UIViewController, UIWebViewDelegate {
let webView: UIWebView = UIWebView()
override func viewDidLoad() {
super.viewDidLoad()
self._setupWebView()
// load the webpage, wait 6 seconds then tap the button
webView.loadRequest(NSURLRequest(URL: firstURL))
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(6 * Double(NSEC_PER_SEC))), dispatch_get_main_queue(), {
self.tapButton();
// wait another 4 seconds then load the direct notes url
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(4 * Double(NSEC_PER_SEC))), dispatch_get_main_queue(), {
self.webView.loadRequest(NSURLRequest(URL: secondURL))
// allow the page to load, and print the html
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(4 * Double(NSEC_PER_SEC))), dispatch_get_main_queue(), {
print(self.webView.stringByEvaluatingJavaScriptFromString("document.body.innerHTML"))
})
})
})
}
func tapButton() {
webView.stringByEvaluatingJavaScriptFromString("changeIframeSrc(this, 'NOTES');")
}
func _setupWebView() {
webView.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(webView)
let views = ["webView":webView]
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[webView]|", options: [], metrics: nil, views: views))
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[webView]|", options: [], metrics: nil, views: views))
}
}
【讨论】:
如果你使用一个来显示页面,你可以在你的应用程序 web 视图中注入 javascript
如果你使用 UIWebView 使用方法 stringByEvaluatingJavaScriptFromString(jscodestring)
如果你使用 WKWebView 使用方法 evaluateJavaScript(jscodestring)
那就是javascript的问题了。
首先查看要单击的元素:文档。 getElementById 或 getElementByName、document.querySelector 等...
然后使用html dom方法click();
【讨论】: