Is there a way to handle "nested iFrames" in protractor - selenium

I am using protractor to test non-angular application and in a scenario i had nested "iFrames", these iFrames are in div tag. Using protractor i was able to navigate to second iFrame, but could not read any elements on page. Let me know if you have any suggestions on this.
<div class="modal-content" modal-transclude="">
<iframe id="frame1" name="newContentItemframe" onload="onLoadDone();"
<iframe name="frame2" id="form_iframe"
<input type="text" class="iw-formspub-textbox-active" name="Content/Title" id="field_0">
</iframe>
</iframe>
</div>

Use switchTo(), which takes a name or index, to switch to the frame/window containing your elements. Also, don't forget to switch back!
browser.switchTo().frame('frame2');
// access elements in the frame...
// then switch back to main window
browser.switchTo().defaultContent();

Maybe not very elegant solution, but picking up the second iframe wasn't working for me, so I had to go with:
browser.switchTo().frame(0);
and then, once in the first frame, again:browser.switchTo().frame(0);.

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

How to get parent of parent's element in webdriverIO?

I am running tests for iPhone safari on browser stack. But click command is not working for iPhone safari on browser stack Selector seems to find an element correctly, but click does nothing and no error,no action ,just silently not executing click.
Same test running perfectly with android device. This is issue with dom structure and I wants to click on parent of parent's element.
I have tried below code snippet and worked for me.
const element:WebdriverIO.Element = $('selctor');
let parentEle = element.$('..').$('..');
Should be possible with xpaths using the xpath axes, specially ancestor
<div class="one">
<div class="two">
<input>
</div>
</div>
To find the parent elements of input you could use an xpath like
browser.$('//input//ancestor::div[1]') //finds div with class=two
browser.$('//input//ancestor::div[2]') //finds div with class=one
Sounds like you need
browser.$('//input//ancestor::div[2]') //finds div with class=one

What could be a reason for not finding a web element by its id?

I have been using Robot Framework for a couple of days now to automate some basic flows tests in my working web app and found a problem:
Can't find some web elements using the id locator
I've been using the SeleniumLibrary for this. Tried with different locators such as xPath and CSS selector, setting an implicit wait before looking for the element and the keywords Wait Until Page Contains Element and Wait Until Element Is Visible, but still it gives the same result.
First of all I check if a certain element is present:
Page Should Contain Element id=some-button
then I try to send some keys to an input:
Input Text id=some-input Some characters
and then I get the error Element with locator 'id=some-button' not found
The page has the following structure:
...
<body>
<div>
...
<div>
<form>
<div>
<button id=some-button />
</div>
<div>
<!--- 8 opening div tags -->
<input id=some-input />
<!--- 8 closing div tags -->
</div>
</form>
</div>
</div>
</body>
...
Does any one have any idea why it happens?
UPDATE:
Tried
/html/body/div/div[1]/form/div[2]/div[1]/div[2]/div[2]/div[2]/div/div[1]/div[1]/div/input
instead of
//*[#id="some-button"]
and that worked perfectly.
What could be a reason for not finding a web element by its id?
These are the reasons I can think of off the top of my head:
the element truly isn't on the page at the time you're looking for it. This could be because you aren't waiting for the page to finish rendering, or it could be that it's genuinely not there.
you could have a typo in the id
the element is in a frame
the element was there, but then got deleted by some executing javascript
Whenever you're using any Xpath or a CSS selector, you should always check in it in Chrome Developer tools.
Steps:
1. Right click the page and click 'Inspect'
2. Go to Console tab
3. If it is a XPath evaluate using: $x and if it is a CSS selector, evaluate using $$
e.g.For
Xpath:
$x("//input[#id='Username']")
CSS:
$$("#Username")

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.

jquery jqmodal .load() issue

I have a modal window using jqmodal. I need a button within that modal to load a different page in the modal. I have this functioning in all browsers except safari.
JS
<script language="javascript">
function shipCalc() {
$('.jqmWindow').load("/ash/shop/shipping.php");
}
</script>
HTML
<form name="form9" id="form9" method="post">
Zip: <input type="text" size="5" name="zip">
<a href="#" id="submitbtn" onclick= "shipCalc();" >zip</a>
</form>
KEEP IN MIND! The class .jqmWindow is a modal window using the jqmodal jquery plugin. It is NOT just a div on a page. I am using the method .load() to change what has been loaded in the modal window after it has popped up. The html shown above is inside the page that is originally loaded in the modal. I am having trouble understanding why this works in all browsers besides safari. I posted earlier and got some responses that weren't quite addressing the real problem here, which is that I can not use the .load() method to load anything into my modal window in safari. (Ive stripped some non-important information regarding future modifications I will make to further make this suite the needs of the site, just to keep this nice and simple to understand...)
function shipCalc() needed to be on both the initial page AND the page that gets loaded into the modal for safari to load everything properly. All other browsers did not require that. All is working now, so I figured I would post my own answer in case anyone was ever searching for something similar!