I have tried every means, three browsers (Chrome, FireFox, Edge), Trying to use WebDriverIO to locate objects within an iframe has failed. Especially for testing it, I wrote the simplest html
outer.html
<html>
<body>
<iframe id="zzz" src="zzz.html" height="150" />
</body>
</html>
zzz.html
<input id="foo" name="ccc" value="ccc" />
my code
await browser.url(`http://localhost:8080/.../outer.html`)
const $iframe = await browser.$('iframe#zzz');
$iframe.waitForExist({ timeout: 10000 });
console.log('iframe exists');
const documentReady = await browser.executeScript('return document.readyState;', []);
console.log('Document ready state:', documentReady);
await browser.waitUntil(
async () => (await browser.execute(() => document.readyState)) === 'complete',
{ timeout: 10000, timeoutMsg: 'iframe load fail' }
);
await browser.pause(10000);
await browser.switchToFrame($iframe);
browser.setTimeout({ 'implicit': 10000 })
console.log('switch to iframe');
let fooExist = await browser.executeScript(
'return document.getElementById("foo")!=null;', []);
console.log('found foo by browser.executeScript:', fooExist);
fooExist = await browser.execute(() => {
return document.getElementById("foo") != null;
});
console.log('found foo by browser.execute:', fooExist);
fooExist = await (await browser.$('#foo')).isExisting();
console.log("found foo by $('#foo'):", fooExist);
await browser.switchToParentFrame();
result
[0-0] iframe exists
[0-0] Document ready state: complete
[0-0] switch to iframe
[0-0] found foo by browser.executeScript: true
[0-0] found foo by browser.execute: false
[0-0] found foo by $('#foo'): false
Currently only browser.executeScript(..) can find the foo object, I would like $('#foo') and browser.execute(..) to work as well.