click method is not working properly in selenium webdriver - selenium-webdriver

i am trying to Automate flipkart website in which i am trying to change address but "add new adress" is not getting clicked i have attached the snapshot
my code is like driver.findElement(By.xpath("//*[#id='ng-app']/div/div[2]/ul/li[2]/div/div[2]/div[2]/div[2]/a/span")).click();
please give the appropriate help

I doesn't look that you are clicking active element, the xpath is //*[#id='ng-app']/div/div[2]/ul/li[2]/div/div[2]/div[2]/div[2]/a/span not correct it clicks on some span.
Use Firepath https://addons.mozilla.org/en-US/firefox/addon/firepath/ to get the xpath.

To ensure that button is clickable Use isDisplayed() and isEnabled() method before clicking on "Add New Address" button, this method return boolean value.
driver.findElement(By.xpath("//*[#id='ng-app']/div/div[2]/ul/li[2]/div/div[2]/div[2]/div[2]/a/span")).isDisplayed();
driver.findElement(By.xpath("//*[#id='ng-app']/div/div[2]/ul/li[2]/div/div[2]/div[2]/div[2]/a/span")).isEnabled();
Also you can verify that element is exist on page or not using below code
if(driver.findElements(byVal).size()!=0){
// Element is present.
}
hope it may helpful to identify cause of issue, why its not clickable.

First and foremost, Use a customized Xpath instead of the one you are using which is extracted directly from the browser. If no customized Xpath can be constructed then try a customized Css or any other locator if possible.
Try to go through the following attempts in order (I hope you can grasp why this is):
1- If .click() still doesn’t work, then keep on changing the values of the attributes you are using to construct your customized xpath, cssSelector, or locator.
2- Use the Actions class.
3- Use JavaScript executioner
4- instead of click(), try using any of: .sendKeys(“\n”). Or .sendKeys(keys.ENTER) or .sendKeys(keys.RETURN)

Related

"How to fix 'at org.openqa.selenium.support.ui.Select.<init>' error in selenium"

I have created the object of Select in selenium to handle dropdown . Also have included the associated packages. Yet the dropdown is not getting selected. Kindly help!
Select select = new Select(driver.findElement(By.xpath("/html[1]/body[1]/div[1]/div[1]/header[1]/div[3]/div[1]/div[1]/div[6]/ul[1]/li[1]/a[1]")));
select.selectByValue("Blouses");
I am recieving the following error "at org.openqa.selenium.support.ui.Select.(Select.java:48)";
Alongwith a note when i hover over Select -
org.openqa.selenium.support.ui.Select
Note: This element neither has attached source nor attached Javadoc and hence no Javadoc could be found.
As far as I can see your XPath expression ends with a which indicates <a> HTML tag which in its turn stands for a hyperlink
In order to be able to use Select class you need to pass to it's constructor a WebElement instance which will point to a <select> HTML tag.
If there is no <select> elements in your page source code - it means that the dropdown is being generated by means of CSS and JavaScript therefore you just need to click the link with the Blouses text which in its turn can be as simple as:
driver.findElementByLinkText("Blouses").click()
If you still want to use XPath - be aware that you can make it a lot shorter, readable and reliable: limit your search scope to hyperlinks only like //a and utilise text() XPath function to match only "interesting" links, the expression which will click the link with Blouses text would be something like:
driver.findElementByXPath("//a[text()='Blouses']").click();

Google tag manager button reference

I'm trying to get google tag manager to track a couple of different buttons on a site. We're currently unable to change the site to aid with this, so we have to find a solution solely with tag manager.
There are several buttons on the site all with the same format as to the two below.. they all have "submit" as the type and a unique term for value so I'm trying to use the tag manager Form Listener which picks up on type="submit". Is there any variable I can use to pull the value field into my event so I can create individual goals in analytics?
etc etc
Any help is greatly appreciated.
You can use built-in variable "Click Element", then create custom JS-variable:
function(){
try{
return {{element}}.getAttribute("value"); //I am not sure now if it is {{element}} or {{Click Element}}
}catch(err){}
}
This will give you a value attribute of clicked button.
Maybe a useful link by Simo Ahava:
http://www.simoahava.com/analytics/track-form-engagement-with-google-tag-manager/#3
You can use built-in auto-event variable Element Attribute to get value. And be sure to use click tracking and not form tracking, because you want to track button clicks and not form submissions.

Not able to click and element using Selenium

I am trying to automate 'Tableau' login screen and download reports flow. Everything works fine till I reach the 'Download' button. The first problem is I am NOT able to right click on the Download button and do an 'Inspect' in Chrome which is weird. I somehow am able to find the xpath by going through the whole page. When i try to click it I get "Caused by: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[2]/div[2]/div/div[3]/div[4]/span[1]"}".
The code that I am using is mentioned below:
String xPath = "/html/body/div[2]/div[2]/div/div[3]/div[4]/span[1]";
(new WebDriverWait(driver, 20))
.until(ExpectedConditions.elementToBeClickable(By.xpath(xPath)));
driver.findElement(By.xpath(xPath)).click();
The page layout is like this from where I am picking the xpath
<div class="tabToolbarButton tab-widget" style="-webkit-user-select: none; -webkit-tap-highlight-color: transparent; width: 77.4px;">
<span class="tabToolbarButtonImg tab-icon-download"></span>
<span class="tabToolbarButtonText">Download</span></div>
<span class="tabToolbarButtonImg tab-icon-download"></span>
<span class="tabToolbarButtonText">Download</span>
</div>
Any help would be appreciated. What could be the reason for not able to 'Inspect'??
If you are not able to right click on the element, you can still right click anywhere on the page and select Inspect this element. You can then grab little binocular icon(tool tip message-select an element in the page to inspect it) and point it to the desired web element to check the xpath.
1st thing you need to check if you are writing xpath correctly. On a chrome you can do that without any plugin. On the developer window(same window which gets open when you select Inspect Element option), look for Console window and write your xpath as mentioned below and hit Enter.
$x("Write x path here")
for e.g. xpath for google search box
$x("//input[#id='lst-ib']")
I'm not sure why you writing absolute xpath. Its a bad practice. You can write x path for Download button in better way.
e.g.
(//span[#class='tabToolbarButtonText'])
I got the problem. So it seems the web page I was trying to automate has used frames. That is why even after providing the xpath the element was not getting clicked. I had to use 'switchTo.frame' in order to get the frame in focus and then I was able to click the element. Thanks for all the responses.
What I have still not understood is that why I was not able to inspect the element directly but that is a different question.

Cannot find button element on form using Webdriver

I'm having trouble trying to click on on a button within a form.
I've tried xpath, cssselector, className, id, but still cannot find it.
Here's the HTML snippet for the button:
<input type="button" value="Continue" id="ni-reg-btn-register" class="btnNext ni-reg-btn-register">
I'm using WebDriver in Java
Getting this trace:
org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with
Command duration or timeout: 30.09 seconds
Frequency: 100%
Browser: Firefox
URL: https://subscription.thetimes.co.uk/webjourney/webj_capturecustomerdetails
I've tried each of the following lines of code one by one (but no luck):
driver.findElement(By.className("btnNext ni-reg-btn-register")).click();
driver.findElement(By.cssSelector("buttons#ni-reg-btn-register.btnNext ni-reg-btn-register")).click();
List<WebElement> buttonlist= driver.findElements(By.className("btnNext ni-reg-btn-register"));
driver.findElement(By.id("ni-reg-btn-register")).click();
driver.findElement(By.xpath("//*[#id="ni-reg-btn-register"]")).click();
I was able to click the Continue Button using the below xpath.
//input[#id='ni-reg-btn-register']
I would suggest trying a WebDriverWait call to wait for the element in question to exist on the page prior to interacting with it. The Java documentation can be found here. It appears that while you are using the correct locator and your page is working, there is a timing issue where Selenium is trying to access the element prior to it being loaded.
EDIT: I was unable to locate an element on the provided page with the supplied id. I assume I am missing a step, but I did a search on the HTML for that page and found nothing.
I dont find the above button on the page link you provided ..
better chk the page link and try to give some wait statement after page load ...
implicit wait would be better or simpli Thread.sleep(2000);

Wrong button is clicked on with a page having two buttons

I'm using Selenium Webdriver to test a web page.
The web page http://www.leaseplan.nl/contact/index.asp has two buttons, one button with button text 'Zoeken' and one with button text 'Verstuur'. I want to click on the button with button text 'Verstuur' either by using XPath or CssSelector with the following code:
driver.FindElement(By.XPath("/html/body/div[3]/div[2]/div[2]/form/fieldset/a/span")).Click();
driver.FindElement(By.CssSelector("fieldset.contact_form > a.button > span.button_center")).Click();
But using the either one of the above lines of code, on the button with text 'Zoeken' is clicked on instead.
This button has quite a similar CssSelector and XPath:
fieldset.header_search a.button span.button_center
/html/body/div[3]/div/form/fieldset/a/span[2]
Does anybody know how to solve this?
Try this out with the cssSelector and tell me if it's work.
For "verstuur" :
By.cssSelector("div.content form a.button")
Solution :
Ok, i've found your problem. Your xpath is good but now, your action on the click submit the first form, so the form with "Zoeken".
onclick="document.forms[0].submit();" // submit the 1st form, the bad one !
Try this :
onclick="document.forms["form"].submit();" // submit the 2nd form, the good one !
//or
onclick="document.forms[1].submit();"
proof : Jsfiddle
xpath for ZOEKEN /html/body/div[3]/div/form/fieldset/a/span[2]
xpath for VERSTUUR /html/body/div[3]/div[2]/div[2]/form/fieldset/a/span[2]
Try this:
driver.FindElement(By.XPath("//span[text()='Verstuur']")).click();
Edit:
I think you want to learn Selenium that's why you are using 3rd party websites. If you really want to learn Selenium find some opensource applications to automate. Here is a good application to automate -
http://sourceforge.net/projects/sugarcrm/files/1%20-%20SugarCRM%206.5.X/FastStack/
Download the latest version and install.
Find more info about the website and selenium stuff here. It's so good..
http://selftechy.com/2011/02/05/introduction-to-selenium-web-application-test-automation-tool
Zoeken: driver.findElement(By.xpath("//div[1]/form/fieldset/a/span[2]")).click();
Verstuur : driver.findElement(By.xpath("//div[2]/form/fieldset/a/span[2]")).click();
Using xpath is easy but slow.

Resources