How to click a Button in Javascript with Swift

2019-07-09 07:06发布

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

enter image description here

enter image description here

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条回答
一纸荒年 Trace。
2楼-- · 2019-07-09 07:17

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

}
查看更多
登录 后发表回答