Unable to find and click the button - c#

trying to find and click the image with C#. Getting error as below.
new_eog.myclass.Logintest:
OpenQA.Selenium.NoSuchElementException : Unable to locate element:
"method":"xpath","selector":"//img[contains(#src,'https://www.sandbox.paypal.com/en_US/i/b
tn/btn_donate_LG.gif')]"}
Below is the html code for the image.
<input type="image" border="0" alt="Make payments with PayPal - it's fast, free and secure!" src="https://www.sandbox.paypal.com/en_US/i/btn/btn_donate_LG.gif" name="submit">
Below is the code i am using to locate and click the image link.
IWebElement temp = wd.FindElement(By.XPath("//img[contains(#src,'https://www.sandbox.paypal.com/en_US/i/btn/btn_donate_LG.gif')]"));
temp.Click();

It's an input tag, so it should be
By.XPath("//input[contains(#src,'www.sandbox.paypal.com')]")

Find the below statement:
wd.findElement(By.xpath("//input[#src='https://www.sandbox.paypal.com/en_US/i/btn/btn_donate_LG.gif']")).click();

You can use By.Name instead of XPath, which i suspect would be slow in performance wise. So you can try out below code
IWebElement temp = wd.FindElement(By.Name("submit"));
temp.Click();

Related

Unable to select checkbox using Selenium webdrive (c#)

I've been puzzling at this for a few hours now. Checked through similar issues on Stackoverflow but been unable to find a solution.
I have a checkbox I'm trying to use Selenium webdriver to check, but when I run the script, I don't get any error messages but the checkbox remains unchecked.
I'm using the line below to select and check the box
IWebElement checkBox = m_driver.FindElement(By.XPath("//div[2]/label/span"));
checkBox.Click();
I've copied the HTML from it below.
<div class="input-group single-option label-empty" >
<label class="" >
<input type="checkbox" name="privacy" value="true" required />
<span>I have read and understood the <a data-toggle="#privacy-terms"
data-group="privacy-terms">Privacy Policy</a> and <a data-toggle=
"#terms" data-group="privacy-terms">Terms and Conditions</a>. </span>
</label>
I'd really be grateful for some help. I'm pretty new to automation...and C#
Unfortunately, I am unable to post the url as it's a passworded client site. If I can post the HTML, that may help...
I've posted the html from the page in question but removed the client name :)
Link to HTML
If I select the checkbox through Chrome developer tools and copy the Xpath, I get this.
IWebElement checkBox = m_driver.FindElement(By.XPath("/html/body/div[1]/main/section/form/div[2]/label/input"));
I re-ran it and Selenium generated an error.
Message: OpenQA.Selenium.WebDriverException : unknown error: Element
is not
clickable at point (491, 593). Other element would receive the click:
... (Session info: chrome=71.0.3578.98) (Driver info:
chromedriver=2.45.615291
(ec3682e3c9061c10f26ea9e5cdcf3c53f3f74387),platform=Windows NT
10.0.17134 x86_64)
developer tools
Here is a screenshot of developer tools with the checkbox ticked. Could it be something to do with the 'span'?
You are clicking the SPAN not the INPUT that is the checkbox. This should work.
m_driver.FindElement(By.CssSelector("input[name='privacy']")).Click();
You may need a wait, depending on what is going on before the click.
Your error about element is not clickable could be any number of things. It could be popup blocking it, a floating DIV panel, a loading spinner, ... etc. You will need to deal with the popup (by closing it, etc.), floating DIV may require the page to be scrolled, or for the spinner a wait for the spinner to become invisible. It's hard to say without more information.
I figured it out. Instead of using "checkBox.Click();", I used "checkBox.SendKeys(Keys.Space);
and it works. Now I'm trying to do the same with the recaptcha which by definition should be difficult.

*Problems finding specific element on page

I know there are existing questions on this topic, but none of them seems to help me with this:
I've got a lightbox with several elements.
I can find and access all of these elements, except ONE, using the XPath.
These are the items:
Text header: No problem
Text: No problem
Input field: No problem
Text: No problem
Text: No problem
Button (upload file): THIS IS SEEMINGLY IMPOSSIBLE FOR Selenium TO FIND
Button (cancel): No problem
Button (send): No problem
The XPaths for all the elements:
.//*[#id='overlays']/overlay--master/div/div/overlay-lightbox/div/div[1] /content-placeholder/h1
.//*[#id='overlays']/overlay--master/div/div/overlay-lightbox/div/div[3]/content-placeholder/ul[1]/li[1]/label/span
.//*[#id='overlays']/overlay--master/div/div/overlay-lightbox/div/div[3]/content-placeholder/ul[1]/li[1]/div/div/input
.//*[#id='overlays']/overlay--master/div/div/overlay-lightbox/div/div[3]/content-placeholder/label
.//*[#id='overlays']/overlay--master/div/div/overlay-lightbox/div/div[3]/content-placeholder/span
.//*[#id='overlays']/overlay--master/div/div/overlay-lightbox/div/div[3]/content-placeholder/a/input
.//*[#id='overlays']/overlay--master/div/div/overlay-lightbox/div/div[5]/content-placeholder/button[1]
.//*[#id='overlays']/overlay--master/div/div/overlay-lightbox/div/div[5]/content-placeholder/button[2]
The problematic element is this:
.//*[#id='overlays']/overlay--master/div/div/overlay-lightbox/div/div[3]/content-placeholder/a/input
As far as I can see, there's no reason why it should be different from the other elements (textfield, button, text)?
I'm accessing all these elements with an implicit wait, to check that they've all loaded before continuing.
GCDriver.WaitForVisible("//*[#id='overlays']/overlay--master/div/div/overlay-lightbox/div/div[3]/content-placeholder/a/input");
From the GCDriver (Selenium Driver) class:
public static void WaitForVisible (string xpath) {
var wait = new WebDriverWait(GCDriver.Instance,
TimeSpan.FromSeconds(10));
wait.Until(driver =>
driver.FindElement(By.XPath(xpath)).Displayed);
}
Now, as mentioned, this works for all the other elements, as well as accessing them directly. For this, the wait times out with WebDriverTimeoutEsception:
Result Message:
Test method Tests.Regression_tests.VerifyOverlays.Verify_Update_Ticket_OverlayContent threw exception:
OpenQA.Selenium.WebDriverTimeoutException: Timed out after 10 seconds
Also, of course, trying to ACCESS the button with .Click() also fails:
GCDriver.Instance.FindElement(By.XPath(".//*[#id='overlays']/overlay--master/div/div/overlay-lightbox/div/div[3]/content-placeholder/a/input")).Click();
Result Message:
Test method Tests.Regression_tests.VerifyOverlays.Verify_Update_Ticket_OverlayContent threw exception:
System.InvalidOperationException: unknown error: Element is not clickable at point (-208, 307)
Here's the html code for the element:
<a class="btn btn-grey file-input-container btn-small" data-bind="enable: !uploading() "
style="margin-top: 10px; padding: 7px 12px; "data-tooltipped=""
aria-describedby="tippy-tooltip-32"
data-original-title="Add Attachment">
<i class="fa fa-cloud-upload"/>
<span class="mq-for-small-hide">
<span localize-me="">Add Attachment</span>
</span>
<input data-bind="upload: addAttachments,
enable: !uploading()" type="file"/>
</a>
I've tried some other ways of getting the element, but since this is quite (imo) "messy" html, with no unique ID's or good class names, I've been unable to figure out how.
And it REALLY bugs me that I cannot find it by the XPath. There are 8 elements on the page, all visible and accessible, but this ONE element is impossible to find with Selenium.
The element is there; I can manually click the button on the page while Selenium runs it.
UPDATE:
I also tried using .Enabled instead of .Displayed. Same result.
UPDATE 2:
There are two answers below, and I have to select one as the "winner".
Shubham Jain gives an answer that, while not the exact thing I was trying to to, is a very good work-around. By using JavaScriptExecutor to try clicking the button, it also checks if the button is visible. However, the answer given doesn't do what it tries to do; Clicking doesn't work quite that way. See Solution below to see the correct/working code to click a button using JavaScriptExectutor.
smit9234's answer is exactly what I'm trying to do, although clicking doesn't work that way. To click the button, JS is necessary in this case. However, the question was how to check .Displayed, and that works with the modified XPath he gave me from the code excerpt.
Solution
The XPath of the element (button) is, according to FirePath:
.//*[#id='overlays']/overlay--master/div/div/overlay-lightbox/div/div[3]/content-placeholder/a/input
This, however, doesn't work. Selenium simply cannot find it, even though it's clearly there.
THIS XPath, however, does work:
.//*[#id='overlays']/overlay--master/div/div/overlay-lightbox/div/div[3]/content-placeholder/a/span/span
However, it works with reagards to the .Displayed check. It does NOT work with Click(). To be able to click the button, I began with Shubham Jain's code example and created this method in the Driver class, to be able to use JavaScript (with Selenium's JavaScriptExecutor) to click the button:
using OpenQA.Selenium.Interactions;
public static void JSClick (string xpath) {
IWebElement icon = Instance.FindElement(By.XPath(xpath));
Actions ob = new Actions(Instance);
ob.Click(icon);
IAction action = ob.Build();
action.Perform();
}
Looking at the html snippet you posted, it seems like this is a file attachment function. Based on the html structure of the snippet, try using the following xpath:
.//*[#id='overlays']/overlay--master/div/div/overlay-lightbox/div/div[3]/content-placeholder/a/span/span
You should then be able to use the click(); method to click the "Add Attachments"
I assume that clicking on the input doesn't do anything, however you should be able to use the sendKeys(); method for sending the "file path" to the input element.
Use below XPath :-
//input[#type='file' and contains(#data-bind,'upload: addAttachments')]
You can use javascriptexecutorof selenium to click on button. It operated directly on JS of page.
In java :-
WebElement element = driver.findElement(By.id("gbqfd"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
I don't know more about c# but I believe it something like
IWebElement clicks = driver.FindElement(By.Id("gbqfq"));
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
js.ExecuteScript("arguments[0].click();", clicks);
Change the locator in above elements as per your convenience.
Below you will find more details of javascriptexecutor
https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/JavascriptExecutor.html
Hope it will help you :)
May be input element is not visible on the page. you may not use displayed function for that element and try with enabled as given below.
public static void WaitForEnabled (string xpath) {
var wait = new WebDriverWait(GCDriver.Instance,
TimeSpan.FromSeconds(10));
wait.Until(driver =>
driver.FindElement(By.XPath(xpath)).Enabled);
}
if the above is not working, you try to click on anchor tag instead of input.
It seems like it's not clickable. It looks like there's some javascript on the page with a function called "uploading()".
since you're button has this on it
enable: !uploading()
just a test to verify if this is actually the cause, put a breakpoint before your click. on the browser dev tools stick a breakpoint in the uploading() function on the javascript file and see what it's returning.
If this is the case you'll have to use the javascript executor to bypass this.

C# Selenium Button in a Span Class XPath (Amazon.com)

I am trying to create a proper XPATH syntax in C# to click on a download button from the Amazon business website. Everything I have tried is unable to find the button. Here are some of the things I've tried:
driver.FindElement(By.XPath("//button[#type='submit']")).Submit();
driver.FindElement(By.XPath("//span[contains(#class,'a-button-inner')][contains(text(),'downloadCSV_button-announce')]")).Submit();
driver.FindElement(By.XPath("//span[contains(#class,'a-button-inner')][contains(text(),'Download CSV')]")).Submit();
Below is the source code from the Amazon page. Can anyone help me to design the proper XPATH query to click this download button? Thank you.
<h1>Amazon Business Analytics</h1>
<div class="a-row a-spacing-medium a-grid-vertical-align a-grid-center">
<div class="a-column a-span12">
<span class="a-declarative" data-action="aba:download-csv" data-aba:download-csv="{}">
<span id="downloadCSV_button" class="a-button aok-float-right"><span class="a-button-inner"><input class="a-button-input" type="submit" aria-labelledby="downloadCSV_button-announce"><span id="downloadCSV_button-announce" class="a-button-text" aria-hidden="true">Download CSV</span></span></span>
</span>
You should try using WebElement#click() to perform click on element instead as below :-
driver.FindElement(By.CssSelector("input.a-button-input[aria-labelledby = 'downloadCSV_button-announce']")).Click();
Or if span element is clickable try as :-
driver.FindElement(By.Id("downloadCSV_button-announce")).Click();
Or
driver.FindElement(By.Id("downloadCSV_button")).Click();

Retrieve the dynamical link text using Selenium

Goal:
Select the second link, from top, by using By.LinkText
<a class="ng-binding" href="#/test/id_var1">324 fff</a>
<a class="ng-binding" href="#/test/id_var2">44 gggg</a>
Problem:
In this context the link text 44 gggg is not static because the link text changes every time the page is refreshed.
My idea is to retrieve all class="ng-binding" and then use the second link as a linkText, but I don't know how do it?
The answer is simply that it is impossible. You cannot get an element by link text if this text is dynamic and you don't know the value.
You could select it using other methods though.
css-selector
a[class='ng-binding']
tag-name (not recommended as it is probably not unique)
a
class-name
ng-binding
xpath
//a[#class='ng-binding']
Find the elements in a list and pull out the one you need.
Another option is selecting the 2nd element immediately using xpath.
(//a[#class='ng-binding'])[2]
use the below as cssSelector:
a.ng-binding[href$='/test/id_var2']// this will return the second link.
if u want to get all the elements using the
ng-binding
class, use the below as cssSelector
a.ng-binding//return all elemtns using the ng-binding class
You can also use below xpath:
//a[#href="#/test/id_var2"]

WatiN finding link to click issue

I am attempting do some automated testing using WatiN and C#. There is a link on the page as an image with the html code
<TD height=27 vAlign=bottom align=right>
<A href="paymentdetailsupdate.do">
<IMG border=0 src="/images/update.gif" width=64 height=21></A></TD>
I just want to click the link but this is proving to be very challenging.
I have tried using following code
cloaspage.Element(Find.By("href", "paymentdetailsupdate.do"));
but it does not seem to work. It times out looking for the element. I have absolutely no idea why. I also can't edit the HTML to add an id or anything like that. Any help is greatly appreciated.
EDIT: I forgot to add in that I can find the link by going through the tables,tablerows, tablecells etc but this is very time consuming and not practical in case of html changes.
Have you tried :
Link link = cloaspage.Link(Find.ByUrl("paymentdetailsupdate.do"));
link.Click();
Probably the same result but you should give it a try...

Categories

Resources