I'm currently looking for a way to click on a button on a web page to automate an export.
I've already succeeded with logging in to the web page and clicking on the login button to redirect the page, but then I could not click on the next button i'm looking for.
My code :
For Each ele In IE.document.getElementsByTagName("ul")
If ele.document.getElementsByTagName("a").getAttribute("aria-labelledby") = "Exporter vers CSV" Then ele.Click
Next
Website source:
<ul class="dropdown-menu pull-left">
<!-- ngRepeat: item in secondaryItems track by item.dataAid --><li ng-repeat="item in secondaryItems track by item.dataAid" class="">
<a role="button" ng-show="item.visible" aria-labelledby="Exporter vers CSV" data-aid="tool-bar-inner-dd-btn-export" type="submit" ng-disabled="!item.enabled" ng-class="{plToolbarItemDisabled:!item.enabled, disabled:!item.enabled}" class="grid-export-item-btn" ng-click="item.callback(item, $event)" pl-toolbar-button-in-dropdown="" item="item">
I cannot test it without knowing your URL, but this maywork:
For Each ele In IE.document.getElementsByTagName("ul")
For Each ele2 In ele.getElementsByTagName("a")
If InStr(ele2.InnerHtml, "aria-labelledby=""Exporter vers CSV""") Then ele2.Click
Next ele2
Next ele
Give this a go. When you are upon instr() function then simply the below method should get you there.
For Each ele In IE.document.getElementsByTagName("a")
If InStr(ele.getAttribute("aria-labelledby"), "Exporter vers CSV") > 0 Then ele.Click: Exit For
Next ele
Related
I am trying to use a querySelector().Click to click a button in a browser.
Here is the HTML Code:
<td tabindex="0" title="Company" class="Smell" role="grid" data-table-header="some header">
<div class="classroom">
<div class="first inner classroom">
<div class="second inner classroom">
<div class="third inner classroom">
<a title="QUERYSELECTOR HERE" class="btn-link" href="/Tosomewhere/here/andthere/111">I'm not trying to grab an inner value because other tables have same inner value</a>
</div>
<div class="different sub-classroom">
<span class="some span">Mrs. Crobapole</span>
</div>
</div>
</div>
</div>
</td>
Here is what I tried:
ie.Document.getElementById("Input Simpson").Value = ActiveCell.Value
ie.Document.getElementById("Some button for Simpson").Click
Do
DoEvents
Loop Until ie.ReadyState = 4
ie.Document.querySelector("[title='QUERYSELECTOR HERE']").Click
Do
DoEvents
Loop Until ie.ReadyState = 4
This is the error message I get:
Run-time error '424': Object required
The debug highlights this portion of the code
ie.Document.querySelector("[title='QUERYSELECTOR HERE']").Click
Yes, I did assign an object name and its preceding codes work perfectly fine. But if I click the "debug" button and run the code again, it works strangely.
try catching the object by tag index, on your case index of tag <a>, you can loop between the html elements to get the one you need according the attribute on your case title="QUERYSELECTOR HERE"
; this code loops between <a> tags inside ("td")(0) and search for the title attribute.
Dim MyHtmlTable As Object
MyHtmlTable = ie.document.getElementsByTagName("td")(0)
For i = 0 To MyHtmlTable.All.tags("a").Length - 1
If MyHtmlTable.getElementsByTagName("a")(i).getAttribute("Title") = "QUERYSELECTOR HERE" Then
MyHtmlTable.getElementsByTagName("a")(i).Click
Do
DoEvents
Loop Until ie.readyState = 4
Exit For
End If
Next i
hope this helps.
I cannot simulate a click event on a div element. The HTML code is this one:
<div id="export" data-export="true" data-timespan="">
<button class="btn btn-default ladda-button" type="button" data-style="expand-right" data-spinner-color="#8899a6">
<span class="Icon Icon--featherDownload"></span>
<span class="ladda-label">
Exportar datos
</span>
<span class="ladda-spinner"></span></button>
<div class="hidden error server Callout Callout--danger">
<p>Ocurrió un problema al exportar tus datos, inténtalo de nuevo más tarde.</p>
</div>
</div>
In the webpage, is just a button that downloads a file when clicked. There is no URL. But after checking the code, is the div element what triggers the click event. Look the image.
Click to see image
That "g.handle" triggers a js.file form the website.
My vba code works perfect for doing other stuff in the website, but I cannot accomplish this task.
I've tried all this options:
Set div = HTMLDoc.getElementById("export")
div.FireEvent ("onclick")
div.Click
HTMLDoc.all.Item
Call HTMLDoc.parentWindow.execScript("g.handle", "JavaScript")
Nothing works. I'm kind of blocked right now and I have no idea of how to proceed. Maybe this has no solution? I have almost no idea of HTML and I have 0 idea of Javascript. But it must have a solution, because if I do it manually, i click on that DIV, wait some seconds and download my file.
Thanks in advance. Any help is welcome.
You didn't specify if the highlighted portion was the html code for the button or not, but it appears from looking at it that it isn't.
You can grab the class name for your button. The name may or may not be unique, and it's difficult to know that without seeing the entire html code, but you can set the btn variable below to the first instance of that class name. If there are multiple class names in your html code, then you will need to use a For Each...Next statement to iterate through them (unless you know the instance number off hand).
So we will go with the first instance of your class, hence the reason for the (0) appended on the end.
Sub Test()
Dim ie As New InternetExplorer, HTMLDoc As HTMLDocument, btn As Object
ie.navigate Url 'whatever this may be
' Some code to wait for your page to fully load
Set HTMLDoc = ie.document
Set btn = HTMLDoc.getElementsByClassName("btn btn-default ladda-button")(0)
btn.Click
End Sub
I know this question has been asked many times and I have tried quite a few and none work.
I have a WebBrowser and 1 button. When I click the form button it needs to invoke a click on the WebBrowser button.
I have tried
WebBrowser1.Document.All("start-button").InvokeMember("click")
WebBrowser1.Document.GetElementById("convert1").InvokeMember("Click")
For Each element As HtmlElement In WebBrowser1.Document.GetElementsByTagName("a")
If element.GetAttribute("id") = "convert1" Then
element.InvokeMember("click")
End If
Next
None of the above works.
Link to website
Code from the webpage source
<a class="start-button" href="javascript:;" id="convert1" onmouseover="changeImage(this, '');" onmouseout="changeImage(this, '');">
Start
<i class="fa fa-angle-right fa-5"></i>
</a>
Also the button on the WebBrowser, when I click it nothing happens for some reason.
Any help would be great.
Thanks
I have been trying to 'click' a button on a site , but I can't get it working.
This is the button I'm talking about:
<div data-expected-currency="1" data-asset-type="T-Shirt" class="PurchaseButton btn-medium btn-primary" data-se="item-buyforfree" data-item-name="Yoshi tux" data-item-id="1788861" data-expected-price="0" data-product-id="231582" data-expected-seller-id="78049" data-bc-requirement="0" data-seller-name="robosapien626">
Take One
</div>
This is what I'm trying:
For Each elem As HtmlElement In WebBrowser1.Document.GetElementsByTagName("div")
If elem.InnerText.Trim = "PurchaseButton btn-medium btn-primary" Then
elem.InvokeMember("click")
End If
Next
It does not do anything.
Other buttons with an actual ID do work fine.
You have to use GetAttribute after the tag name so you can find the button and click it:
For Each element As HtmlElement In WebBrowser1.Document.GetElementsByTagName("div")
If element.GetAttribute("class") = "PurchaseButton btn-medium btn-primary" Then
element.InvokeMember("click")
End If
Next
The .InnerText of that element is Take One with some whitespace around it, so you can try
If elem.InnerText.Trim = "Take One" Then
or
If elem.OuterHtml.Contains("PurchaseButton btn-medium btn-primary") Then
I want to go to http://ted.com/talks, click the "See All Topics" in the "Topics" dropdown, and then click the a random letter heading, like "C" or "D-E". However, I don't know how to find the element in the modal popup for that specific letter heading.
This is what the letter heading elements look like when I click "Inspect Element":
<li class="topic-select__range">
<a class = "topic-select__range__link" href="#" data-index="0">A-B</a>
</li>
<li class="topic-select__range">
<a class = "topic-select__range__link" href="#" data-index="1">C</a>
</li>
<li class="topic-select__range">
<a class = "topic-select__range__link" href="#" data-index="2">D-E</a>
</li>
... etc.
My program can get all the way to See All Topics just fine but gets Cannot Locate Element Error when I try to click on the letter headers. This is what my code snippet looks like so far:
# Each header is assigned a number. A header is picked at random, and clicked.
# [ERROR: Cannot locate element.]
random_letter = random.randint(0, 8)
topics_window = browser.find_element_by_class_name("select-modal__content topic-select") # error here, when program tries to find modal popup or tags within it
letter_headers = topics_window.find_element_by_class_name("topic-select__range__link")
letter_headers[random_letter].click()
I couldn't find a way to access modal JavaScript content with selenium. I have instead found a way to do so using web API.