span inside a button tag not clickable using selenium webdriver? - selenium

I'm trying to click and "Add Course" labelled button using selenium webdriver but it is not working out for me.
Here's the snippet I took from chrome developer tools:
<button type="button" class="btn btn-green" onclick="javascript:AddCourse();">
<span class="glyphicon glyphicon-plus-sign">
::before
</span>
<span translate="portallang_addCourse" class="open-sans ng-scope">
"Add Course"
</span>
</button>

You need one of the following xpaths:
First choice:
//span[contains(text(), 'Add Course')]
These two, only if there are always 2 spans and the second span is always Add Course:
(//button[#class='btn btn-green']/span)[2]
//button[#class='btn btn-green']/span[2]
Slowest option as the entire document is scanned. Add Course can only occur once on the page:
//*[contains(text(), 'Add Course')]

What error are you getting? What property did you use to click on the span element?
You got to use xpath for such elements.
Use xPath Finder add-on for Chrome and extract the unique xpath for this element:
https://chrome.google.com/webstore/detail/xpath-helper/hgimnogjllphhhkhlmebbmlgjoejdpjl?hl=en
You can always write an unique xpath manually but this just saves time and is accurate.
Hope this helps!

Related

Click on element in robot framework is not working in ie and work fine in chrome and fiorefox

Click Element is not working in Robot Framework, in the log.html it shows that it clicked on the element, but it does not actually happen in the browser.
The element which I want to click is:
<a href="http://www.twitter.com/intent/tweet?text=Orange%20named%20a%20Top%20Employer%20Global%202018%20for%203rd%20consecutive%20year&url=http%3A%2F%2Fintegration-ruby.proj.620nm.com%2Fen%2Fblogs%2Forange-named-top-employer-global-2018-3rd-consecutive-year&via=orangebusiness" class="share-button-twitter" target="_blank" title="Share on twitter">
<span class="fa-lg fa-stack text_black"> <i class="fa fa-circle-thin fa-stack-2x"></i> <i class="fa fa-twitter fa-stack-1x"></i> </span>
</a>
I run my test using:
Execute JavaScript document.querySelector(".share-button-twitter").click()
Execute JavaScript document.querySelector('a[title="Share on twitter"]').click()
Execute JavaScript document.querySelector(".fa-lg fa-stack text_black").click()
Click Link //a[#title="Share on twitter"]
Click Element //i[#class="fa fa-twitter fa-stack-1x"]
Click Element //span[#class="fa-lg fa-stack text_black"]
The element is visible in the page, and the script works fine in Chrome and Firefox I find this issue with the browser IE.
What can be the reason for this? Any idea to solve the problem?
Update your Internet Explorer to the latest version. And also download the appropriate supported version of IEDriver for that version of IE. The problem is only here, and no where else.

selenium click using span class or onclick

I am a newbie to java and selenium webdriver. I am having an issue clicking an image. Below is the page source.
<a href="javascript:void(0);">
<span class="HomeButton" onclick="javascript:onBtnHomeClick();"/>
</a>
I tried below codes but did not work and still getting the Unable to locate element error.
driver.findElement(By.xpath("//a[#onclick='onBtnHomeClick()']")).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[#id='js_AppContainer']/div[2]/div[1]/div[1]/span"))).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("HomeButton"))).click();
I have to click the homebutton. Any help would be much appreciated
I don't know why By.className("HomeButton") didn't work but you have errors in the other two.
In driver.findElement(By.xpath("//a[#onclick='onBtnHomeClick()']")).click(); the tag for onclick is <span> not <a>. It also not onBtnHomeClick() but javascript:onBtnHomeClick();
driver.findElement(By.xpath("//span[#onclick='javascript:onBtnHomeClick();']")).click();
If you want to use onBtnHomeClick() use contains
driver.findElement(By.xpath("//span[contains(#onclick, 'onBtnHomeClick')]")).click();
Or
driver.findElement(By.cssSelector("onclick*='onBtnHomeClick'")).click();
And in wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[#id='js_AppContainer']/div[2]/div[1]/div[1]/span"))).click(); the <span> parent tag is <a>, not <div>
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[#id='js_AppContainer']/div[2]/div[1]/div[1]/a/span"))).click();
You simply need the correct locator IF your element will be eventually visible.
Xpath = "//span[contains(#class,'HomeButton') and contains(#onclick,'onBtnHomeClick')]"
Add wait as needed above exanmple, that should work.

Robot Framework Test Data Editor - Click on SPAN/Div not working

I wanted to perform click on Panel element in Selenium Robot Framework
code below:
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<span href="#panel" data-parent="#accordion" data-toggle="collapse" class="accordion-toggle panelTitle collapsed" id="panel" aria-expanded="false">Text 1<span class="toggle-icon"><i class="fa fa-plus-circle"></i></span>
</span>
</h4>
</div>
I want perform a click on "span" tag the whenever I write in my selenium robot framework as
Click Link (in first column) id=panel (in second column)
It doesn't work.
In my previous projects it was working fine but I am not able to make it work in this.
This is pretty hard to answer without you providing your test code or more detail but I suspect the element doesn't exist on the page when you try to click it? What error message are you getting? Providing this can help get an answer quicker.
Reading your question closer, are you using the right keyword, are you actually clicking a link? i.e. something contained in link tags? e.g.
all we've done together
Here's a rudimentary example:
Wait Until Page Contains Element panel 10
Click Element panel
This link should help you find further information if you require it: http://robotframework-seleniumlibrary.googlecode.com/hg/doc/SeleniumLibrary.html?r=2.8#Page Should Contain Element
Other libraries contain similar keywords you could use like the Selenium2Library

Failed click using Selenium and XPath

For test automation on a web app I am currently using Robot framework and the Selenium library. There comes the moment I want to click on a button and then I wait for an element to appear. Alas, the click, although recorded successful in the log, does not lead to the apparition of the element I wish. In fact it is as if the click never happened.
To click on the button, I retrieve it using xpath. Using Firepath I successfully verified that the element is unique and does exist but what it shows leads me to think I have found why the button is not clicked correctly
You can see in light blue the button I want to click on and wrapped with dotted line the zone detected by firepath.
Is it possible that the click is done next to the button instead on it ?
Here is the incriminated line :
Click Element xpath=(.//button[contains(#id,'editContract')])[1
Edit :
HTML of the page :
<button id="listContractTableForm:listContractTable:0:editContract" name="listContractTableForm:listContractTable:0:editContract" type="button" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-icon-only" onclick="window.location.href='destinationPage?faces-redirect=true&contractEntityContractId=678'">
<span class="ui-button-icon-left ui-icon ui-c ui-icon-search"></span>
<span class="ui-button-text ui-c">ui-button</span>
</button>
Assuming you want the span with class containing ui-button-icon-left ui-icon ui-c ui-icon-search
//button[contains(#id,'listContractTableForm')]/span[contains(#class,'ui-icon-search')]
if you want the span with class="ui-button-text ui-c" simply change the class in the xpath

How to handle dynamic ID in HTML

I have to click on the button which has id. But this id is generated dynamically. And find By.className() is not doing anything.
The HTML code for the button:
<td class="x-btn-mc">
<em class="" unselectable="on">
<button id="cq-gen372" class=" x-btn-text" type="button">OK</button>
</em>
</td>
How to select the button and click on it in Java?
By.className() really was bugged in IE and some older Selenium versions. I didn't know it is still the case. Anyway! You can search by a lot of things, not just id:
You can try By.xpath("//button[text()='OK']"); if it is the only (or the first) OK button on page.
For more xpaths, see XPath v1.0 on w3.org and XPath v2.0 on w3.org - only for some new browsers!.
Or you can go with css selectors - The w3 again or wikipedia.
You can go with below options
//button[text()='OK']
xpath=//button[contains(., 'OK')]
//button[contains(#class, 'x-btn-text')]