I am trying to click a string of text within a web browser control that runs some javascript, I am unsure how to click the text as the method I use for buttons does not work in this instance
Function Clk(WebBrowserID, ElementID)
WebBrowserID.Document.GetElementById(ElementID).InvokeMember("click")
Return "Clicked"
End Function
This is all that is visible to the client in regards to the clickable text
<div class="task-skip pure-g">
<div class="pure-u-1">
Next
</div>
</div>
I am completely lost on how to either click the text so that the javascript script runs or to invoke the script through code so the button does not need to be pressed, any help would be greatly appreciated.
The solution that I found and worked is as below hope this will help anyone else with the issue.
For Each Element As HtmlElement In Browser.Document.All
If Element.InnerText = "TEXT HERE" Then
Element.InvokeMember("click")
End If
Next
Return "RETURN VALUE"
Related
I'm using VbScript with Selenium driver to connect to Chrome (store.steampowered.com/app/681530/NOISZ/) and want to click the "Next in Queue" button which is a div class, but nothing happens. I know I'm on the right track because the below lines does work as expected:
(Displays the text Next in queue)
msgbox(driver.FindElementsByXPath("//div[#class='next_in_queue_area']/div[#class='btn_next_in_queue btn_next_in_queue_trigger']").Item(1).text)
(Scrolls to the Next in queue button)
driver.FindElementsByXPath("//div[#class='next_in_queue_area']").Item(1).ScrollIntoView
(But trying these ones does nothing and also no error messages)
driver.FindElementsByXPath("//div[#class='queue_actions_ctn']").Item(1).Click
driver.FindElementsByClass("next_in_queue_content").Item(1).Click
Using Chrome's Inspect when highligtning this code the Next button is highlighted entirely:
<div class="btn_next_in_queue btn_next_in_queue_trigger" data-tooltip-text="Remove this product from your queue and continue to the next item.">
<div class="next_in_queue_content">
<span>Next in Queue<br>
<span class="queue_sub_text">(11 remaining )</span>
</span>
</div>
</div>
So it is just a question of finding the correct div or span to click
To click the button with text as Next in Queue you can use either of the following solution:
FindElement with XPath:
driver.FindElementByXPath("//div[#class='btn_next_in_queue btn_next_in_queue_trigger']/div[#class='next_in_queue_content']/span").Click
FindElements with XPath:
driver.FindElementsByXPath("//div[#class='btn_next_in_queue btn_next_in_queue_trigger']/div[#class='next_in_queue_content']/span").Item(1).Click
As an alternative you can use the ExecuteScript() method as follows:
Dim js As IJavaScriptExecutor = TryCast(driver, IJavaScriptExecutor)
js.ExecuteScript("arguments[0].click();", driver.find_element_by_xpath("//span[starts-with(#id, 'button-')][#class='x-btn-inner x-btn-inner-center']"))
Or
Imports OpenQA.Selenium.Support.Extensions
driver.ExecuteJavaScript("arguments[0].click();", driver.find_element_by_xpath("//span[starts-with(#id, 'button-')][#class='x-btn-inner x-btn-inner-center']"))
I am trying to use VBA to click the button: search job posting
button
The html code:
<td><br> <input type="submit" class="btn btn-primary btn-small" value="Search"></td>
<td><br>-or - Search Job Postings
I was attempting to do it by something like:
IE.document.Something.Click
Much appreciation for someone who can help me to figure out the "something"!!!
There are probably better ways to identify a button, but when I was testing this stuff for fun a long time ago checking the onclick property was the only reliable way to identify a button on the site I was interfacing with. I'm assuming you know how to reference the browser window based off what you said. I included the Microsoft HTML Object Library to get the HTMLButtonElement type.
Dim htmlElements as Variant
Dim buttonElem As HTMLButtonElement
Set htmlElements = IE.Document.getElementsByTagName("BUTTON")
For Each buttonElem In htmlElements
If InStr(1, buttonElem.onclick, "orbisApp.buildForm({action:'displayAdvancedSearch'}).submit()") > 0 Then
buttonElem.Click
End If
Next
All you need to do is simply use the form element to click the button. Your statement:
IE.document.Something.Click
Is almost spot on.
Here's the code you want to use
IE.document.Forms(0).Submit
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 have an HTA prompt with VB code. I would like a URL loaded within the HTA window when the "SUBMIT" button is clicked. However, I cannot find any information that is helpful for my situation. Can someone please help me include an iframe in my HTA prompt so that a website can be displayed? I can use https://www.google.com as an example. Let me know if you have any questions.
Here is the code I currently have for the submit button:
bodystring = bodystring & "<BR><BR><BUTTON CLASS='Bttn_Back' OnClick='PrevStage()'>BACK</BUTTON> <BUTTON CLASS='Bttn_Submit' OnClick='NextStage()'>SUBMIT SURVEY</BUTTON>"
Here is my section of code for the "NextStage()" function:
ElseIf STAGE = 62 Then
SaveResults()
Window.Close()
End If
Put this to the HTML of the HTA:
<iframe id="htmlhere" src="" style="display: none;"></iframe>
Then in nextStage():
document.getElementById("htmlhere").src = "http:/..."; // Loads a new document to iframe
document.getElementById("htmlhere").style.display = ""; // Setting style.display empty shows the iframe
Sorry for using JS, but my VB skills are more or less zero.
I have to click a HTML button programatically which is on the 3rd page of the website . The button is without id. It has just name type and value . The HTML code of the button is given below
<FORM NAME='form1' METHOD='post' action='/dflogin.php'><INPUT TYPE='hidden' NAME='txtId' value='E712050-15'><INPUT TYPE='hidden' NAME='txtassId' value='1'><INPUT TYPE='hidden' NAME='txtPsw' value='HH29'><INPUT TYPE='hidden' NAME='txtLog' value='0'><h6 align='right'><INPUT TYPE='SUBMIT' NAME='btnSub' value='Next' style='background-color:#009900; color:#fff;'></h6></FORM>
i am using the following code to click it
For Each webpageelement As HtmlElement In allButtons
If webpageelement.GetAttribute("value") = "Next" Then
webpageelement.InvokeMember("click")
End If
Next
But i cant able to click it . I am using the vb.net 2008 platform. Can anyone tell me the solution to click it?
I was having the same problem as you. The InvokeMember looks correct so I can only assume its not picking up the correct html element. Try refering to the element explicitly using the value. The following code worked for me but I have adapted it for you:
For Each webpageelement As HtmlElement In allButtons
If webpageelement.GetAttribute("value").Equals("E712050-15") Then
webpageelement.InvokeMember("click")
Exit For
End If
Next
Good Luck :)