Vba - webscraping using ng-click

故事扮演 提交于 2021-01-27 14:23:25

问题


I am using Selenium and I would like to be able to click on the following

<a ng-click="download()">download</a>'

This is an 'a' tag. I am not sure how the code would be like to click onto an 'a' tag that has got ng-click in it.

  Dim d As WebDriver
  Set d = New ChromeDriver
Const URL = "url of the website - not public"
With d
    .Start "Chrome"
    .get URL
    .Window.Maximize
    .FindElementById("Search").SendKeys "information to search"
    .Wait 1000
    .FindElementById("Submit").Click
    .Wait 1000
    'then I need to click the following <a ng-click="download()">download</a>
End With

Only step I am missing is to be able to click on that last bit. Thank you for the help in advance :)


回答1:


The desired element is an Angular element you need to induce WebDriverWait for the elementToBeClickable and you can use either of the following Locator Strategies:

  • Using FindElementByCss:

    d.FindElementByCss("a[ng-click^='download']").click
    
  • Using FindElementByXPath:

    d.FindElementByXPath("//a[starts-with(@ng-click, 'download') and text()='download']").click
    



回答2:


You can try with xpath :

.FindElementByXPath("//*[@ng-click='download()']").Click



回答3:


This is what attribute = value css selectors are for. You can target the ng-click attribute by its value:

d.FindElementByCss("[ng-click='download()']").click



回答4:


It would be easier (and more efficient) to see more relevant HTML code, but you can loop through the a tags and find the one that has the text: download. Once found, you can try to click on it then exit the loop.

Dim a As Selenium.WebElement
For Each a In d.FindElementsByTag("a")
    If a.Text = "download" Then
        a.Click
        Exit For
    End If
Next


来源:https://stackoverflow.com/questions/57531709/vba-webscraping-using-ng-click

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