I have one HTML form where I need to click on checkbox but somehow it is not able to click on checkbox and instead it click on link
HTML Element - When checkbox is not clicked
<div class="form-group">
<input id="chkTermAndCondition" type="checkbox">
<label for="chkTermAndCondition">
I have read and agree to the
<a id="lnkTermsAndCondition" href="javascrip:void(0);">terms and conditions.</a>
</label>
</div>
HTML Element - After checkbox is clicked
XPAth Used but not working and clicking on "link"
//*[#for='chkTermAndCondition'] - click on link
//*[#id='chkTermAndCondition'] - This doesn't work
//div[#id='sellerInformationDiv']/div[3]/div/label - click on link
Both the above xpath is not clicking on checkbox
Try below code:
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
js.ExecuteScript("arguments[0].click();", driver.FindElement(By.xpath("//input[#id='chkTermAndCondition']"));
Related
I want to achieve clicking a HTML button via reading its Value or innerText in C# Selenium, none of the methods for click is seem to work, no idea why?
I was wondering if this is possible if so please advise my code is below.
Html below
<div class="a-button-stack">
<span class="a-declarative" data-action="dp-pre-atc-declarative" data-dp-pre-atc-declarative="{}" id="atc-declarative">
<span id="submit.add-to-cart-ubb" class="a-button a-spacing-small a-button-primary a-button-icon">
<span class="a-button-inner">
<i class="a-icon a-icon-cart"></i><input id="add-to-cart-button-ubb" name="submit.add-to-cart-ubb" title="Add to Shopping Basket" data-hover="Select <b>__dims__</b> from the left<br> to add to Basket" class="a-button-input" type="submit"
value="Add to Basket" aria-labelledby="submit.add-to-cart-ubb-announce">
<span id="submit.add-to-cart-ubb-announce" class="a-button-text" aria-hidden="true">Add to Basket</span></span></span>
</span>
</div>
My code in C# is the following:
driver.FindElement(By.XPath("//button[contains(text(), 'Add to Basket')]")).Click();
driver.FindElement(By.XPath("//button[contains(text(), 'Add to Basket')]")).Click();
driver.FindElement(By.XPath("//*[contains(text(),'Add to Basket')]")).Click();
driver.FindElement(By.XPath("//button[contains(text(), 'Add to Basket')]")).Click();
//input[#value='Add to Basket']
There is no button tag but you can grab the input tag by it's data attribute value like so.
You are trying to use an element of a button but a button element does not exist.
The add to cart button in the html is:
//input[#id='add-to-cart-button-ubb']
How can i write test scripts for login button without button name and id?
Here my code,
<button _ngcontent-lcu-6="" class="md-primary pull-right" md-raised-button="">
<span class="md-button-wrapper">Login</span>
<div class="md-button-ripple" md-ripple="" mdripplebackgroundcolor="rgba(0, 0, 0, 0)">
<div class="md-ripple-background"></div>
</div>
<div class="md-button-focus-overlay"></div>
</button>
You can use XPath or CSS to locate an element when there is no ID or name associated with a target element.
XPATH
//button/span[text()='Login']
CSS
button>span.md-button-wrapper
Sample code
driver.FindElement(By.XPath("//button/span[text()='Login']")).Click();
driver.FindElement(By.CssSelector("button>span.md-button-wrapper")).Click();
I got a form with a radio button (button with value 3) that I need to select and a submit button that need to be clicked. The problem is I don't know how to select the radio buttons or click the button since it doesn't have a name or id.
Here is the HTML code:
<input type="radio" name="chosen" value="3" id="a3">
<input type="radio" name="chosen" value="2" id="a2">
<input type="radio" name="chosen" value="1" id="a1">
<input value="Next" type="submit">
Here is the code I've tried for the button (I'm trying to select the button with value 3:
webBrowser1.Document.GetElementsByTagName("input").GetElementsByName("chosen")[2].InvokeMember("click");
but that code won't work and I don't know how to click the button to submit and go on. I need help selecting the radio button and click the submit button. I don't have access to change any of the HTML code.
I'm not quite sure what you meant by "use C# to click". Because basically, the element is displayed and interact in browsers (Chrome, FireFox). And C# is for processing http requests in server (IIS).
So to select/click some html element, you can use jQuery such as
$('input[value="3"]').click();
<div class="pill-wrap double">
<a ng-click="modalCancel()" class="button pill">No</a>
<a ng-click="modalSuccess()" class="button pill">Yes</a>
</div>
I want to get the button with class button pill and where the innertext is 'No' and click it
To click on the button with innertext as No you can use the following line of code :
driver.FindElement(By.XPath("//div[#class='pill-wrap double']//a[#class='button pill' and #ng-click='modalCancel()']")).Click();
My webbrowser connects to a page, then I want it to click a div.
<div class="acceptButton state1">
<a class="buttonGreen">
<span>${$item.getAddressButtonText( $data.messageType )}</span>
</a>
</div>
The page uses jquery or something to do it all. :( and most help I found required an id, which these only have a class
<script id='messageListItem' type='text/x-jquery-template'>
<li data-messagetype="${messageType}" class="${messageType}" data-messageid="${messageId}">
<div class="messageItem">
<div class="closeButton">
<a><span>X</span></a>
</div>
<img class="friendImage" src="${senderImgUrl}" alt="${senderName}" />
<div class="messageBubble">
<div class="messageBubbleTop"></div>
<div class="messageBubbleBody">
<div class="messageBubbleContent">
{{if $data.messageImgUrl != null}}
<img class="giftImage messageImage" alt="${messageImgAltText}" src="${messageImgUrl}">
{{/if}}
<h5 class="friendName">${senderName}:</h5>
<p class="requestMessage">${message}</p>
<span class="requestDate">${timestampStr}</span>
<div class="clearFloat"></div>
</div>
</div>
<div class="messageBubbleBottom"></div>
</div>
<div class="acceptButton state1">
<a class="buttonGreen"><span>${$item.getAddressButtonText( $data.messageType )}</span></a>
</div>
<div class="clearFloat"></div>
</div>
</li>
To click the div you can run JavaScript code in the WebBrowser's DocumentCompleted event handler using Navigate():
webBrowser1.Navigate("javascript: /* your javascript code */ void(0);");
So if you have a div:
<div class="myDivClass">
...
</div>
You can trigger a click on it using:
webBrowser1.Navigate("javascript: document.getElementsByClassName('myDivClass')[0].click();void(0);"); //assuming it's first and/or only div with that class
As far as I remember, it is not possible to click a DIV. In that, if you try, the event in the DIV will not trigger. Say, if the DIVhas an onclick event, it will not trigger.
So, what you have to do, in order to get the onclick event in the DIV to trigger, is to click anything (any of the other elements) in the DIV. Let's say the DIV has an IMG element/tag: Perform a .click on that and the DIV's onclick event will be triggered. Does this make sense? So any DIV's onclick triggering is only possible through onclick event bubbling - by using the method I described above.
I am just telling you this in case you we're expecting an onclick event to run that is attached directly to the DIV. Just in case you add it in the future or you run into it later, it is important for you to understand this (even though I didn't see an onclick in your DIVtag at the moment, it's an important information to have).
Hope this helps and if you have any further questions, let me know.
I only found this through testing and it isn't really written anywhere, the same thing applies to the span tag if I remember correctly.