如何使用Swift在Javascript中单击按钮 [英] How to click a Button in Javascript with Swift

查看:191
本文介绍了如何使用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)
    }

}

这篇关于如何使用Swift在Javascript中单击按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆