How to click a Button in Javascript with Swift

余生长醉 提交于 2019-12-12 18:42:13

问题


Hey I'm trying to figure out how to click this simple little button but I'm failing.

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

I've also tried to click on the td< but nothings happens because the button is actually the a<

Code:

 // no errors print but nothing happens
self.webView.evaluateJavaScript("document.getElementById('participant-page-results-more').click();"){ (value, error) in

        if error != nil {
            print(error!)
        }

    }

UPDATED Answer still nothing but I think were close, @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 {
        }
    }

But interestingly enough when I use the .innerText in stead of the .click a get back the Show more matches label.

UPDATE 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!)
        }
    }

回答1:


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)
    }

}


来源:https://stackoverflow.com/questions/47523277/how-to-click-a-button-in-javascript-with-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!