Webbrowser 2 similar buttons how to click only 1? - vb.net

I'm doing an automation bot for my website using vb.net
I'm facing a problem with button click I have these 2 buttons
Button 1
<button type="submit" id="import_btn" class="btn btn-primary w-sm waves-effect waves-light"> Fetch </button>
button 2
<button type="submit" class="btn btn-primary waves-effect"> <span class="btn-label"><i class="fa fa-plus"></i></span>CREATE </button>
for automation I'm using this code:
For Each elem2 As HtmlElement In Webbrowser1.Document.GetElementsByTagName("button")
Dim valueArrtibute As String = elem2.GetAttribute("type")
If valueArrtibute = "submit" Then
elem2.InvokeMember("click")
End If
Next
I want to click button 2 but with my code is clicking both clicks

You could add a check for the button's InnerText, which represents all text between the button's start and end tag (<button> and </button>).
If valueArrtibute = "submit" AndAlso elem2.InnerText.Contains("CREATE") Then
There are many ways to identify a specific element on a website. For more in-depth examples I recommend seeing my answer to this question: Is there a possibility to address elements on a website which have no ID?

Related

How to click on hidden button via VBA Selenium Chrome?

I am trying to click on a button on a website using VBA Selenium via Chrome.
When I use VBA code to click the button, ElementNotVisibleError pops up:
VBA code:
Set CD = New Selenium.ChromeDriver
CD.Start
CD.Get Url
Application.Wait (Now + TimeValue("0:00:05"))
CD.FindElementByCss(".inline_cancel").Click
When I move the mouse to the place where button is, the button appears and HTML code style display changes to block and before it was none.
When set to "none" VBA cannot find the element.
When set to "block" VBA can detect the element.
HTML code:
<span class="edit-hover" style="display: none;"><input type="hidden" name="field_root_name" id="order_header_change_attachments_attributes__field_name" value="order_header_change[attachments_attributes][]" class="field_root_name"><input type="hidden" value="365995938" name="order_header_change[attachments_attributes][365995938][id]" id="order_header_change_attachments_attributes_365995938_id"><input value="false" class="delete-field" type="hidden" name="order_header_change[attachments_attributes][365995938][_delete]" id="order_header_change_attachments_attributes_365995938__delete"><span class="intent-field"><span class="attachment-intent-checkbox"><input name="order_header_change[attachments_attributes][365995938][intent]" type="hidden" value="Internal"><input class="intent_checkbox_field" id="attachment_365995938_intent" type="checkbox" value="Supplier" name="order_header_change[attachments_attributes][365995938][intent]">→ <label for="attachment_365995938_intent">Supplier</label></span></span><a aria-label="Delete Attachment" class="inline_cancel" href="javascript:void(0);" role="button" tabindex="0"> </a></span>
You saw it right. Clearly the desired element:
<a aria-label="Delete Attachment" class="inline_cancel" href="javascript:void(0);" role="button" tabindex="0"> </a>
have the ancestor <span> with attribute style="display: none;"
<span class="edit-hover" style="display: none;">
which implies the element is hidden i.e. either not visible or not enabled.
To click on the element you have make the desired element clickable by taking some actions on some other element first.

How to click IE button with similar class names and attributes

I'm currently stuck trying to click on buttons on a private website. The buttons I'm trying to click have the same attributes and I cannot distinguish which button I need to click. Here are the three buttons I'm trying to click. As you can see, they are similar and except for the text on the button. How can I select for specific button and click it?
<button ng-repeat="punch in $ctrl.nextPunches" ng-disabled="punch.sendingPunch || $ctrl.sendingPunch" type="button" class="btn btn-default ng-binding ng-scope" ng-click="$ctrl.addPunch(punch)" style="">
<!-- ngIf: punch.sendingPunch -->
Check In
</button>
===========================================================================
<button ng-repeat="punch in $ctrl.nextPunches" ng-disabled="punch.sendingPunch || $ctrl.sendingPunch" type="button" class="btn btn-default ng-binding ng-scope" ng-click="$ctrl.addPunch(punch)">
<!-- ngIf: punch.sendingPunch -->
Start Meal
</button>
==========================================================================
<button ng-repeat="punch in $ctrl.nextPunches" ng-disabled="punch.sendingPunch || $ctrl.sendingPunch" type="button" class="btn btn-default ng-binding ng-scope" ng-click="$ctrl.addPunch(punch)">
<!-- ngIf: punch.sendingPunch -->
Check Out
</button>
I have tried using this:
Set HTMLButtons = HTMLDoc.getElementsByTagName("button")
For Each HTMLbutton In HTMLButtons
Debug.Print HTMLbutton.getAttribute("ng-click"), HTMLbutton.getAttribute("type"), HTMLbutton.getAttribute("ng-repeat"), HTMLbutton.getAttribute("ng-disabled")
Next HTMLbutton
HTMLButtons(2).Click
However, the buttons that show up on the immediate window is not helpful and it doesn't click any button when I use the .Click command. These buttons are under the UI element on the webpage. I'm not sure if that is helpful but I can definitely provide more of the webpage code if needed.
Thanks!
You could use document.getElementsByClassName method or document.getElementsByTagName method to get the button (it will return an array), then, loop through this array and distinguish them based on the innerText property. More detail, you could check this article.
Besides, you could also add some custom attributes and set different value, then use it to distinguish them.

Submit a form when Bootstrap radio button clicked

I have a form with a radio-button group:
<form name="search" class="form-inline" asp-controller="controller" asp-action="action" method="get">
<div class="btn-group" data-toggle="buttons">
<label onclick="document.search.submit()" class="btn btn-default #ActiveTime("AM")" onclick="document.search.submit()">
<input type="radio" name="time" value="am" autocomplete="off">AM
</label>
<label onclick="document.search.submit()" class="btn btn-default #ActiveTime("PM")" onclick="document.search.submit()">
<input type="radio" name="time" value="pm" autocomplete="off">PM
</label>
</div>
</form>
#ActiveTime:
public string ActiveTime(string time)
{
if (ViewData["time"].ToString() == time) { return "active"; }
return "";
}
Relevant section of controller\action:
string time = ( (String.IsNullOrEmpty(HttpContext.Request.Query["time"].ToString())) ? "am" : HttpContext.Request.Query["time"].ToString() ).ToUpper();
ViewData["time"] = time;
Clicking the AM or PM label will submit the form, but doesn't include the radio button's value. What am I missing?
It is not sending the value of the radio button because when user clicks on the label, the browsers javascript engine will execute the onclick event handler you wired up on that element. Your current handler will execute this code document.search.submit() which submits the form immediately, even before bootstrap library could adjust the radio button state.
You can solve the issue by hijacking the click handler and set the radio button value yourself and then submit the form (all in javascript).
Another option i would try is, to remove the form submit behavior from the radio button selection/click event. What if user want to change his selection ? I would create a separate "Submit" button for the form submission.
I also noticed you have onclick handler registered two times on your element. clean that up.

How do I click on a web button that appears multiple times on a webpage?

How do I click on a web button that appears multiple times on a webpage? How do I click all of them?
I am testing the "Like" button on a website (Webstagram.com) that is meant for you to view and operate your Instagram page from your desktop.
20 different pictures are displayed on a page and each picture has its own "like" button assigned to it. I can't identify it by "like" and in the outerhtml there are different values for each one. How do I write a script to identify each one?
Here is some of the info on the properties/values.
class: btn btn-default btn-xs likeButton
htmlid: N/a
htmltag: Button
innerhtml: <I class="fa fa-heart"></I>Like
outterhtml (Like button for pic #1): <BUTTON class="btn btn-default btn-xs likeButton" type=button data-target="1194558981914665301_8054519"><I class="fa fa-heart"></I>Like</BUTTON>
outerhtml (like button for pic#2)
<BUTTON class="btn btn-default btn-xs likeButton" type=button data-target="1194558967727891183_339837919"><I class="fa fa-heart"></I>Like</BUTTON>
NOTE I listed the outerhtml property values for two different pictures to show where the values differ. This is also the outerhtml code I tried to write to click on any like button and bypass any specific values:*
outerhtml: <BUTTON class="btn btn-default btn-xs likeButton" type=button data-target=".*_.*"><I class="fa fa-heart"></I>Like</BUTTON>
This is the script I tried to run that failed
1) systemutil.Run "websta.me/tag/graffiti";
2) Browser("#graffiti Instagram photos").Page("#graffiti Instagram photos").WebButton("<BUTTON class="btn btn-default btn-xs likeButton" type=button data-target=".*.*"><I class="fa fa-heart"></I>Like</BUTTON> ").Click
3)wait(1)
4) Browser("#graffiti Instagram photos").Page("#graffiti Instagram photos").WebButton("<BUTTON class="btn btn-default btn-xs likeButton" type=button data-target=".*.*"><I class="fa fa-heart"></I>Like</BUTTON> ").Click
5) wait(1)
repeat... –
Try something like this using Descriptive programming approach of QTP
Set oDesc = Description.Create
oDesc("micclass").value = "WebButton"
oDesc("html tag").value = "BUTTON"
oDesc("class").value = ".*likeButton"
odesc("class").RegularExpression = True
'Find all the Links
Set obj = Browser().Page().ChildObjects(oDesc)
Msgbox obj.Count 'will show how many buttons are found
For i = 0 to obj.Count - 1
Obj(i).Click
Wait 2 'waits for 2 sec
Next
You can use VRI (visual relations identifier) to link an ambiguous object (your like button) to a well defined object (the picture).
This way you can say "click the like button that's closes to picture X".
Another way is to create a simple web-extensibility project that exposes a new object for the pictures which support the functionality of Liking a picture.

Selenium webElement property change

I am automating an application with selenium.There are no normal labels like id,tag by which I will be able to find an element. So I was using the xpath[driver.findElement(By.xpath())]. But now i find that some of the xpaths of the WebElement changing dynamically while runtime and so my test cases are failing.Even the relative xpath option is not available for the HTML.I am pasting a part of the html of the AUT. Please let me know how to handle this scenario .
<div>
<button class="btn btn-primary ng-hide" ng-click="unlockOrder('/content/boss/en/dashboard');" ng-show="enableUnlockButton" type="button">Unlock Order</button>
<button class="btn btn-primary" ng-click="discardOrder('/content/boss/en/create-order/pre-order-options');" type="button">Discard Order</button>
<button class="btn btn-primary" ng-click="saveOrder(showSaveButton && ban_search.$valid);" ng-disabled="showSaveButton==false || ban_search.$invalid" type="button" disabled="disabled">Save Order</button>
</div>
I would rely on the button text:
driver.findElement(By.xpath('//button[. = "Unlock Order"]'))
where . refers to button's text.