selenium click using span class or onclick - selenium

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.

Related

Click hidden element using selenium webdriver

I need to click on the fields bug, epic, feature, issue and task which appear on click the "+" icon.
I inspect each of the elements and the see the same common xpath highlighted in the image in developer tool.
How do i find the xpath of each of the elements highlighted and click on them using selenium webdriver?
<body class="lwp">
<noscript><div class="absolute-fill flex-column flex-center"><h2>JavaScript is Disabled</h2><p>Please enable javascript and refresh the page</p></div></noscript>
<input type="hidden" name="__RequestVerificationToken" value="t6WnlqRwnH3QtDIOt2b3JiHWYI1V5vB3MheOCOTH7Fx6QaBNoXWz4k8P4luP9i7TYw70KVq4O2vuwa8DQRuLcdUxo_KKVjcmxorJTHAE3c42sdjYqojLZkOSNt1gad70mD0BBA2" />
<div data-componentregion="page" class="full-size"></div>
<div class="bolt-portal-host absolute-fill no-events scroll-hidden"></div>
</body>
You need to handle windows here because when you click "+" new window appearing with elements "bug","epic","feature",,,etc
should be parent window and "epic",,,, are child window
I could not identify unique xpath for the elements as all the elements had same xpath. using sikulix library solved my problem

span inside a button tag not clickable using selenium webdriver?

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!

locate the tapestry component pagelink in selenium

I am beginner in selenium.
How to locate the element pagelink in selenium?
<li>
<t:pagelink page="NewCustomer" >New Customer</t:pagelink>
</li>
I used this :
driver.findElement(By.partialLinkText("NewCustomer")).click();
but it didnt work for me.
Any suggestions?
Thanks;
Look at the HTML source in your browser. You'll see that the Tapestry pagelink component renders itself as an ordinary HTML hyperlink, perhaps <a href="/NewCustomer">. That's what Selenium must look for. Something like this:
//*[local-name()='a' and #href='/NewCustomer']
But a better strategy might be to add a CSS class name or id other attribute specifically to make it easier for Selenium to find the element:
<li>
<t:pagelink page="NewCustomer" class="myNewCustomerLink">New Customer</t:pagelink>
</li>
//*[local-name()='a' and #class='myNewCustomerLink']
You can use driver.findElement(By.linkText("New Customer").click(), this will work.

various ways to find elements by using Webdriver #FindBy annotations

I am trying to find the solution for my Webdriver script to work. I need your help to find the solution in my scenario.
I have a web application that creates form by using tabbed screen. There is a Next and Back buttons that have similar attributes and I cant find the solution.
xpath doesn't work, CSS has the same name and its getting confused.
here is the "Next" button that needs to be clicked:
<div class="button" style="margin-right: 5px;">
<input type="button" onclick="javascript:submitForm('metaStudyAction!moveToData.action')" value="Next">
I would like to use #FindBy annotation to find the element. Can I use HOW with multiple attributes?
You can use css for multiple attributes
#FindBy(css = "[type='button'][value='Next']")
private WebElement button;

How To Write Selenium Xpath

I am stuck here. I am trying a write Xpath for the following. I am trying to click "Browse". The ID is changing every time.
I have tried this as my Xpath:
//*[#id='ext-gen43']/em/span/span
<a id="ext-gen43" class="x-tab-right" href="#">
<em class="x-tab-left">
<span class="x-tab-strip-inner">
<span class="x-tab-strip-text ">Browse</span>
</span>
</em>
</a>
XPATH WRITING PLUGINS :
Inorder to write xpath's by yourself you must first install firebug
and firepath plugins, they are the plugins available for Firefox
browser.
You can also install xpath checker which is another plugin available for firefox. It's a awesome plugin because you can actually
see the UI elements in the xpath checker as you are writing the
xpath.
EXAMPLE :
Inorder to write xpath's by yourself you must following the child and parent hierarchy available in the HTML tab in firebug.
STEPS :
Hover on the element you want to write xpath for using the small blue arrow available in the firebug toolbar in the top left corner.
Now click on the element, you would observe that the tag for the respective element in the firebug is highlighted.
Say your tag looks like :-
<label> class="control-label col-sm-3" for="name"> Your Name <label>
So the xpath for respective tag would be :-
//label[#class="control-label col-sm-3"]
So the above xpath specifies //parent tag which is label and into the bracket we should specify the locator, locator can be id, class, name anything.
And in your case the xpath would be :-
//span[#class="x-tab-strip-text"]
Have You tried copy xpath for given element in your browser(check element -> copy xpath)?
Then delete id and check once again.
It should be easy if you know how to select element by xpath with given class.
Try to use firepath to get xpath. It`s addon for FireFox https://addons.mozilla.org/uk/firefox/addon/firepath/