Click an element of a hover button which hide/unhide to expand other selections - vba

I am trying to login on a company website to upload a file. I am successful on login process but I failed on selecting the "Text button" that expands the other selection.
[enter image description here][1]
Below is the HTML elements.
<div class="tree_margin" id="tree_root/14+" style="text-decoration: none; display: block;">
<a class="tree_hover" href="JavaScript:tree_switch_folder('tree_root/14+');">
<img src="skin/blue/sidemenu_minus.jpg" border="0" alt="資材システム" align="absmiddle"> 資材システム</a><br></div>
資材システム
I tried the code below but it gives me "error:438".
Set objIE = New InternetExplorerMedium
objIE.Document.getElementByClassId("tree_root/14+").Click
Thank you in advance for your help.

Assuming you have left a sufficient page load wait, and that you have shown the correct html, I believe you want to click the anchor tag. You can target its class via css class selector and the querySelector() method:
objIE.Document.querySelector(".tree_hover").click

Related

How to click on toggle button using VBA Selenium

Can sameone help me to click on a toogle button using VBA/Selenium:
HTML:
<img name="toggleFiltro" src="https://curyempreendimentos.sienge.com.br/sienge/imagens/base/toggleDown.png" onmouseover="IH_toggleOver(this)" onmouseout="IH_toggleOut(this)" style="cursor: pointer;">
Here is what I tried so far, but it's not displaying the information:
ChromoLink.FindElementByXPath("//img[#src='https://curyempreendimentos.sienge.com.br/sienge/imagens/base/toggleDown.png']").Click
and this one:
ChromoLink.FindElementByXPath("//*[#name=""divFiltro""]").Click
I got it:
ChromoLink.FindElementByXPath("//*#id=""holderConteudo2""]/form/div[9]/span/img").Click

VBA IE Automation - cannot find "export" element ID

I'm working in Microsoft Excel 2010 VBA and I'm trying to automate pdf report downloads. I can navigate to where I need to be but I'm stuck on the last step;
There's an "Export" element that behaves like a link. If I were to click on it, it would initiate a pdf download. I want to automate this and direct the download to a specified folder, but when I inspect the element it doesn't appear to have an element ID by which I can call it (ie. getElementById().Click).
I'm new to VBA IE automation. Does anyone have suggestions on how to initiate that "Export" function?
I can't share the full HTML detail, as it is for a site with sensitive data, but below are snippets of the HTML, my VBA code, and a screenshot of the HTML section that is in question:
<DIV style="FONT-SIZE: 8pt; HEIGHT: 30px; FONT-FAMILY: Verdana; DISPLAY: inline"><TABLE style="DISPLAY: inline" cellSpacing=0 cellPadding=0>
<TBODY>
<TR>
<TD height=28><SELECT id=PrintFormat><OPTION value="">Select a format</OPTION><OPTION selected value=Pdf>Acrobat (PDF) file</OPTION></SELECT></TD>
<TD width=4></TD>
<TD height=28><A onclick="if (document.getElementById('PrintFormat').value != '') {var url = document.getElementById('PrintFormat').value + 'Stream.aspx'; document.location.href = url;}" style="FONT-SIZE: 8pt; TEXT-DECORATION: none; FONT-FAMILY: Verdana" href="#" shape="">Export</A></TD></TR></TBODY></TABLE></DIV>
My vba code;
Sub pdfdownloadautomation()
Set objIE = New InternetExplorer
objIE.Visible = True
objIE.navigate Sheet3.Range("A1").Value 'A1 contains url
objIE.document.getElementById("PrintFormat").Value = "Pdf"
'works fine to this point - just not sure how to call the export
objIE.document.getElementById("?").Click
End Sub
screenshot of inspected element in IE
You may try a CSS selector as follows to target the element. I can't test and don't know if there are parent frame/iframe/form tags to navigate. There appears to be one form element at least visible. Please provide the html including this form tag if possible. I have given variations without parent form and including parent form (which looks to be the case) - the assumption with the parent form element is that it is the first form element on the page.
ie.document.querySelector("td a[onclick^='if']").Click
Which might become:
ie.document.getElementsByTagName("form")(0).querySelector("td a[onclick^='if']").Click
The CSS selector is likely more complex than required but I am trying to localize using the visible cues in your image i.e. that there is an element with an a tag, that has onclick attribute starting with 'if', and is inside a parent td tag.
You could also gather all the a tag elements and loop until the Export innerText is found:
Dim list As Object, item As Object
Set list = ie.document.getElementsByTagName("a")
For Each item In list
If item.innerText = "Export" Then
item.Click
Exit For
End If
Next
Again, this may become
Set list = ie.document.getElementsByTagName("form")(0).getElementsByTagName("a")

Simulate Click event on a DIV element

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

Locating a button one more time, whose text has been changed by an User click

I've located a button, first time around when it is rendered with a text on it as 'Kill' with---> .//tr[1]/td[4]/div/button[text()='Kill'].
My html was like...
<tbody>
<tr class="GPBYFDECG GPBYFDEJG" __gwt_row="0" __gwt_subrow="0">
<td class="GPBYFDEBG GPBYFDEDG GPBYFDEEG GPBYFDEKG">
<td class="GPBYFDEBG GPBYFDEDG GPBYFDEKG">
<td class="GPBYFDEBG GPBYFDEDG GPBYFDEKG">
<td class="GPBYFDEBG GPBYFDEDG cellkillbutton GPBYFDEKG">
<div style="outline-style:none;" __gwt_cell="cell-gwt-uid-339" tabindex="0">
<button type="button" style="width: 50px;color:white;" tabindex="-1">Kill</button>
</div>
</td>
When user clicks on the button, the text on it just changes to 'UnKill' and nothing in HTML changes, but when I modify the xpath to
.//tr[1]/td[4]/div/button[text()='UnKill'].
It doesn't work at all. Why ? Even when I refresh the page and go in to the firebug and check it, it does not work.
Some time there might be chances that your element state get changed if you are doing actions with same element . So for your case if it is doing click on button labeled as "Kill"
then check that your button is clickable or not by putting some ExplicitWait like
WebDriverWait wait = new WebDriverWait(driver, 120);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//tr[1]/td[4]/div/button[contains(text(),'Kill')]")));
driver.findElement(By.xpath(".//tr[1]/td[4]/div/button[contains(text(),'Kill')]")).click();
When you click on the button as "Kill" it becomes hidden and some other button gets activated at the same UI location with the text "UnKill". In this case the xpath for this two elements are different. For your second run try finding the xpath of the button/element with text "Unkill" & try to click. It should work.

Selenium: Click on element in div container doesn't work

Fyi: I'm using the selenium package for R, the selection code is equal to javascript or python so I'm asking a general selenium question.
I have a container which I have to make visible by a click, this works.
Then I try to select an element inside this container, I think I find it correctly but the click on the element only makes the popup window disappear.
Example code:
<div class="dateRanges" style="top: 275.313px; display: block;">
<a class="top dateOption CUSTOM" id="id32" href="javascript:;">
Benutzerdefinierter Zeitraum
</a>
<a class="dateOption TODAY" id="id33" href="javascript:;">
Heute
</a>
...
</div>
Element I try to find is "top dateOption CUSTOM".
My different tries which all failed:
remDr$findElement(using = 'xpath','//a[contains(#class,"top")]')$clickElement()
remDr$findElement(value = '//a[#class = "top dateOption CUSTOM"]')$clickElement()
remDr$findElements(using = 'css selector','a[class="top dateOption CUSTOM"]')[[1]]$clickElement()
I don't get any error message, the click seems to happen as the popup disappears, but the effect of the button does not.
I tried to save the object, wait a few seconds and click afterwards with no different effect.
I also tried different approaches with "idc4" with the same outcome.
Would appreciate any help, thank you.