How To Write Selenium Xpath - selenium

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/

Related

How to find webelement when there is no ID in Selenium test script

I have a button displayed on the browser with the following tag which doesn't have ID or a single CSS class name. How can I find the Webelement and add a click to this button.
<button aria-describedby="notes" aria-label="Notes" autofocus="" class="btn btn-primary" role="dialog" type="button">OK</button>
Web elements can be located by XPath or CSS Selectors based on any unique combination of any attribute values or / and tag names and also based on their parent or child elements.
Since you didn't share the entire HTML of that page we can only guess about the correct unique locator for that element.
With XPath it can be something like:
"//button[text()='OK']"
Or
"//button[#aria-label='Notes']"
In CSS Selector style it will be
"button[aria-label='Notes']"
Consider locating the Element by Tag Name. Go to the parent element which has a id or class name and get subelements by tag name (parentelement.find_element_by_tag_name('button')) which is your button
Besides that, the button in your example has two classes.
You click on a button via the .click() command.

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

Xpath for dynamic select menus

I'm trying to automate some web testing using Kantu and Selenium. The page is using pq-select ParamQuery to generate select menus, but neither Kantu or Selenium can see them in the page.
I'm guessing my best bet is to use xpath to locate them, but I'm not too sure on how to do that. The HTML for the select menu is:
<td style="white-space: nowrap " aria-describedby="df230254-d8a5-4ba1-9950-58d26145d5a9" role="gridcell" data-container-for="section1" id="sectiongrid_active_cell" class="">
If I could use the data-container-for that would be the best I think, but I'm not sure how to get that via xpath.
As per the HTML you have shared you can use either of the following solutions:
XPath - 1:
//td[#id='sectiongrid_active_cell' and #data-container-for='section1']
XPath - 2:
//td[#id='sectiongrid_active_cell' and #data-container-for='section1'][#role='gridcell']
Note: The element is a ParamQuery select element which is being converted to theme ready jQueryUI widget, you have to induce WebDriverWait for the element to be visible/interactable.

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

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.