Any idea how to select an element using Selenium and this output i Firebug:
<tr class="ui-widget-content ui-datatable-even ui-datatable-selectable" aria-selected="false" role="row" data-rk="0101" data-ri="0">
Use Chrome, lots easier. Locate the element, then open context menu with right click, Copy -> XPath. Tutorial with images: How to verify an XPath expression in Chrome Developers tool or Firefox's Firebug?
Use Firepath plugin for Firefox, which is add-on with dependency of Firebug
You can evaluate both XPATH & cssSelectors.
Related
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.
I'm using Selenium Webdriver latest version and run the test on Chrome v69.0.3497.100.
When I execute script in my laptop, It working fine and click on the element but on desktop, it find element but can't click.
This is HTML of element:
<div class='col-sm-1'>
<div style="position: relative;margin-top: 19px;left:14px;" class="material-switch">
<input id="checkHiddenDevice" name="checkHiddenDeviceOption" type="checkbox">
<input id="checkHiddenDevice" name="checkHiddenDeviceOption" type="checkbox">
<label for="checkHiddenDevice" class="label-primary">
::before
::after
</label>
</div>
</div>
This is the Xpath:
//div[#class='col-sm-1']
I've also tried some xpaths like //label[#for='checkHiddenDevice'] or find element by CSS but it doesn't work. only xpath //div[#class='col-sm-1'] is working but in laptop only.
Then I try on KatalonRecorder Tool. When I put my Xpath and run, test case passed because it finds element but didn't click? Then I try to find Xpath by Katalon tool then Katalon generate this Xpath:
xpath=(.//*[normalize-space(text()) and normalize-space(.)='#of devices'])[1]/following::label[1]
But with this xpath it also can't find element (or I don't know how to change it to right xpath in my code)
Does anyone was faced with such problem?
Yes.. Even i have faced this issue, this issue might occur when the browser zooming is more than 100%. just execute the same scripts with 90% of browser zoom
How can I find XPath and CSS in Mozilla Firefox 58.0.1?
I can't find Firebug.
I tried adding the add-ons but it does not allow me to do so.
I'm using Selenium 3.8.1.
Right-click the element you want to inspect
Select Inspect element
Right-click in the now open Developer Tools on the element you want the XPath of
Select Copy -> XPath
On the right hand side you can also inspect all CSS properties of the element.
I am writing selenium tests, and I need to switch to an iframe with no id or name and which parent element contains variable id's (so not helpful. Also, the src attribute has variable data in it as well, so I can't target it directly like By.cssSelector("iframe[src='example']"). I need an xpath selector that targets the src, but also that uses contains. I am trying to learn how to build xpaths outside of Chrome's Copy XPath but I can't figure this one out. Thanks for your help! Here is the iframe html:
<iframe scrolling="auto"
src="/admin/catalog/manage_variants_in_product.jsp?productId=160502"
width="100%" height="100%" frameborder="no"
style="position:absolute; top:0px; left:0px;">
</iframe>
The "contains" CSS selector might help here:
iframe[src*=manage_variants_in_product]
FYI, there are also ^= and $= that mean "starts with" and "ends with" respectively.
The better way I would recommend to learn building xpath or csspath is Firepath add-on of Firefox
First install Firebug in your Firefox browse and then install Firepath.
There you will get the efficient way to get the xpath or evaluate the xpath build by yourself
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/