Locating webelement by link in Selenium java - selenium

How is it possible to locale Selenium WebElement that is next to certain link?
I have this kind of source code
<a href="/url.com" >
Link text
</a>
<ul>
<li class="dir">THIS LIST ELEMENT I WANT TO LOCATE
<ul>
<li>
If the url is not nessesary in locating, feel free to tell me better way! I have had no luck with counting elements, because amount of them varies based on mouse moves. I use Selenium Java.

Using xpath You can locate.
"//a[text()='Link text']/following-sibling::ul/li[#class='dir']"
I assume that a and ul tags are siblings. If those are not siblings then also you can achieve that by modifying above xpath slightly.
"//a[text()='Link text']/../following-sibling::ul/li[#class='dir']"

Related

Ember dropdown selenium xpath

How to write xpath for the ember dropdown.
<ul id="ember-power-select-options-ember2473" class="ember-power-select-options ember-view" aria-controls="ember-power-select-trigger-ember2473" role="listbox"> <li class="ember-power-select-option" aria-selected="false" aria-current="false" data-option-index="0" role="option">Option A
Since the ember id changes, how can i write xpath??
With xpath:
//ul[contains(#id, 'ember-power-select-options-ember')]
With css:
ul[id*='ember-power-select-options-ember']
Other css:
ul.ember-power-select-options[role=listbox]
The value of the value of the id attribute will keep changing dynamically, everytime you access the AUT(Application Under Test). Hence to interact / click the dropdown you need to construct dynamic Locator Strategies as follows:
cssSelector:
ul.ember-power-select-options.ember-view[id^='ember-power-select-options-ember']
xpath:
//ul[starts-with(#id, 'ember-power-select-options-ember') and #class='ember-power-select-options ember-view']
References
You can find a couple of relevant detailed discussions in:
How to click on the ember.js enabled button using Selenium and Python
Selenium - Finding element based on ember

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;

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.

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/