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

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")

Related

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

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/

How to use the DOM locators in Selenium

I've the HTML source like this,
<input type="image" onclick="return logSub();" src="/images/Login_submit.gif" width="105" height="33" border="0" />
Here there is no ID or NAME. So I can only locate this using image index (which is hard) or using the src tag? But I dont know how use the src tag?
Is that possible?
See my answer to a previous question here: selenium: Is it possible to use the regexp in selenium locators
Basically the dom= protocol allows you to use javascript to locate elements for Selenium.
or with css:
css=input[type=image], [src="/images/Login_submit.gif"]
Have you tried
//input[#src='/images/Login_submit.gif']
Try this locator:
//input[contains(#src, 'Login_submit.gif')].

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.