Xpath: not supported by FireFox, but Chrome OK - selenium

WebDriver Xpath: not supported by FireFox:
//div[#class='foo']//*[name()='div' or name()='span' or name()='input' and #type='text']
But this is working for Chrome. Is this Xpath 1.0?
how to fix it for firefox? Is there a way to verify xpath in firefox browser?

It's legal XPath 1.0. You might like to add parentheses in the condition to make quite sure that you, the reader, and the XPath processor all share the same understanding of the operator precedence of "or" and "and".
Can't help you diagnose a problem if you don't tell us the symptoms.

Here are the options.
<html><head></head><body>
<div class="foo">
<div><input type="text"></div>
<span type="text"></span>
<label> ... </label>
<div type="text"></div>
<input type="text">
</div>
</body></html>
Option 1:
//div[#class='foo']//*[#type='text'][local-name()='div' or local-name()='span' or local-name()='input']
Option 2:
//div[#class='foo']//div[#type='text']|//div[#class='foo']//span[#type='text']|//div[#class='foo']//input[#type='text']

Related

Not able to find xpath. Chropath message - This element might be inside iframe from different src. Currently ChroPath doesn't support for them

Below is the HTML snippet I am trying to get xpath. I have tried so many combination in chrome dev tools but no luck. Chropath message is that element might be from different src.
I am not sure what it means. Any help appreciated.
<input type="text" class="sn-global-typeahead-input -global-active-input" name="sncwsgs-
typeahead-input" id="sncwsgs-typeahead-input" placeholder="Search Global" autocomplete="off"
aria-label="Search" aria-hidden="false" aria-expanded="true" aria-activedescendant="sncwsgs-
typeahead-record-1-2" aria-autocomplete="both" aria-owns="sncwsgs-typeahead-sections" aria-
controls="sncwsgs-typeahead-sections" aria-describedby="sncwsgs-typeahead-instructions" aria-
haspopup="listbox" value="" role="combobox">
There's an id tag, so you can use the following for the XPath:
"//input[#id='sncwsgs-typeahead-input']"
That also translates to the following CSS Selector:
"input#sncwsgs-typeahead-input"
But if that's inside an iframe, you'll need to switch to the iframe first before you can interact with elements inside of it.
Learn about switching into iframes here: https://www.selenium.dev/documentation/webdriver/interactions/frames/
Example of switching into an iframe:
driver.switch_to.frame("iframe")

Selenium WebDriver, works fine on laptop but not on desktop.WHY?

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

Can't Click About Element IE With Robot Framework

In my testcase i have the next locater:
Double Click Element //input[#value='user1']
The locater is for the next snippet:
<tr onselectstart="listview_onselectstart(this, event)" tabindex="1" class="row altrow selected"><input class="text" type="text" tabindex="-1" readonly="readonly" style="width:;" value="user1">
This works perfect with Chrome and Firefox. When i try this with ie i see the page refresing and nothing happends.
Doesnt ie works with javascript ? Or what can be the problem ?
IE has a long-standing history of extremely poor performance when it comes to XPath selectors.
Please consider using CSS selectors if you intend to work with IE.
In your case, Double Click Element css=input[value=user1] should do the trick.

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')]

Input button image - not work in Chrome & Safari?

I have the following code in my index.html
<li>
<select name="cmbtype" style="display:none" id="cmbtype" onChange="Changetype()">
<option value="0">
<input type="image" src="images/ocean.png" value="Play" onclick="previewplay(); " />
</option>
</select>
</li>
In firefox, opera and IE 7 I see the ocean.png and am able to click it. When I do, it accordingly plays the mp3 it is supposed to!
When I load this same code in Safari (Windows and Mac), as well as in Chrome the ocean.png is not seen and there is no button to click?
Is there something I can add or do to get this code/design to work in Safari and Chrome?
Thanks
Why do you have the input in your select?
It probably inherits the display: none therefor not getting rendered, which I would assume would be the correct behaviour.
Maybe IE, firefox and opera finds it illegal syntax and rewrite the input outside of the select, but webkit does not.
It is illegal HTML markup and not supposed to work.
<li> stands for list item and must be inside an <ol> (ordered list) or <ul> (unordered list)
<select> can have <optgroup> and <option>
<option> can have characters but no element
In this case Chrome and Safaris way of doing it is the correct way to do it.