如何使用Swift在Javascript中单击按钮
嘿,我试图弄清楚如何单击这个简单的小按钮,但失败了.
Hey I'm trying to figure out how to click this simple little button but I'm failing.
这是正确的代码实现,但是javascript中的路径错误.我需要点击a<
This is the correct code implementation but wrong path in javascript. I need to click on the a<
或者如果我只能调用连接到按钮的功能
Or if I can just call the function connected to the button
我也尝试点击td<但什么也没发生,因为该按钮实际上是a<
I've also tried to click on the td< but nothings happens because the button is actually the a<
代码:
// no errors print but nothing happens
self.webView.evaluateJavaScript("document.getElementById('participant-page-results-more').click();"){ (value, error) in
if error != nil {
print(error!)
}
}
更新后的答案仍然没有,但我想很接近,@ Michael Hulet:
self.webView.evaluateJavaScript("document.querySelector('#participant-page-results-more').getElementsByTagName('a')[0].click();"){ (value, error) in
if error != nil {
print(error!)
} else {
}
}
// Or
self.MainWebView.evaluateJavaScript("document.querySelector('#participant-page-results-more a').click();"){ (value, error) in
if error != nil {
print(error!)
} else {
}
}
但有趣的是,当我使用.innerText而不是.click时,又获得了显示更多匹配项"标签.
But interestingly enough when I use the .innerText in stead of the .click a get back the Show more matches label.
更新2:
尝试直接加载功能也无济于事
trying to load the function directly does nothing either
self.MainWebView.evaluateJavaScript("window.loadMoreGames();"){ (value, error) in
if error != nil {
print(error!)
}
}
// Or
self.MainWebView.evaluateJavaScript("window[loadMoreGames]"){ (value, error) in
if error != nil {
print(error!)
}
}
// Or
self.MainWebView.evaluateJavaScript("loadMoreGames();"){ (value, error) in
if error != nil {
print(error!)
}
}
要触发该功能,您需要单击<a>
标记(侦听点击的HTML标记),而单击id
正在查找在<table>
标记上.请注意,此处的键更改是不同的选择器/选择方法,请尝试以下操作:
In order to trigger the function, you need to click on the <a>
tag (the HTML tag that listens for clicks), but the id
you're looking up is on the <table>
tag. Note that the key change here is a different selector/selection method, but try this:
self.webView.evaluateJavaScript("document.querySelector('#participant-page-results-more a').click();"){ (value, error) in
if let err = error {
print(err)
}
}