Selenium can't select context of an iframe (intermittently)
I have Selenium tests (written in Python) that find an iframe using its ID, and then looks for a button inside the iframe. The iframe is injected by a browser extension. This is the Python code:
id_ = "my-special-iframe"
driver.switch_to.frame(driver.find_element_by_id(id_))
polling.poll(
target=lambda:driver.find_elements_by_id("button")),
timeout=10,
step=1)
Where an element with the id "button" exists within the iframe but not outside it.
What I observe is the call to switch_to
succeeds, but half of the time the context remains the default context at the top of the DOM. I know this because the result of driver.page_source
prints the whole DOM (as if the default context was selected), and not that of the iframe. Moreover, I can call switch_to
as many times as I want (because, being within the main DOM rather than within the iframe, I can select the iframe). So it follows that Selenium can't find the button element.
Here's the logs from Geckodriver in an instance where Selenium fails to get into the iframe's context:
1542386220903 Marionette TRACE 0 -> [0,25,"WebDriver:FindElement",{"using":"xpath","value":"//iframe[@id='my-special-frame']"}]
1542386220906 Marionette TRACE 0 <- [1,25,null,{"value":{"element-6066-11e4-a52e-4f735466cecf":"47898438-5185-4e1e-ac19-c5220a6f8fcf"}}]
1542386220908 Marionette TRACE 0 -> [0,26,"WebDriver:SwitchToFrame",{"element":"47898438-5185-4e1e-ac19-c5220a6f8fcf"}]
1542386220910 Marionette TRACE 0 <- [1,26,null,{"value":null}]
1542386220912 Marionette TRACE 0 -> [0,27,"WebDriver:FindElement",{"using":"css selector","value":"#button"}]
1542386220914 Marionette TRACE 0 <- [1,27,{"error":"no such element","message":"Unable to locate element: #button","stacktrace":"WebDri ... entError@chrome://marionette/content/error.js:388:5nelement.find/</<@chrome://marionette/content/element.js:339:16n"},null]
With the exception of the failure to find the element at the end, the logs look the same in the case where Selenium can select the iframe context.
I see this using both Chromedriver and Geckodriver. Why can't Selenium switch to the correct context sometimes? When I open the Chrome (or Firefox) console, I can see the contents of the iframe there so Selenium should be able to find the element. Does Selenium have a problem with switching to injected iframes?
Previous answers deal with finding elements within the iframe without switching to the iframe first. In this case, I am switching to the iframe but even then I don't get the correct context. No idea why this was marked as duplicate.
python selenium testing selenium-webdriver
|
show 1 more comment
I have Selenium tests (written in Python) that find an iframe using its ID, and then looks for a button inside the iframe. The iframe is injected by a browser extension. This is the Python code:
id_ = "my-special-iframe"
driver.switch_to.frame(driver.find_element_by_id(id_))
polling.poll(
target=lambda:driver.find_elements_by_id("button")),
timeout=10,
step=1)
Where an element with the id "button" exists within the iframe but not outside it.
What I observe is the call to switch_to
succeeds, but half of the time the context remains the default context at the top of the DOM. I know this because the result of driver.page_source
prints the whole DOM (as if the default context was selected), and not that of the iframe. Moreover, I can call switch_to
as many times as I want (because, being within the main DOM rather than within the iframe, I can select the iframe). So it follows that Selenium can't find the button element.
Here's the logs from Geckodriver in an instance where Selenium fails to get into the iframe's context:
1542386220903 Marionette TRACE 0 -> [0,25,"WebDriver:FindElement",{"using":"xpath","value":"//iframe[@id='my-special-frame']"}]
1542386220906 Marionette TRACE 0 <- [1,25,null,{"value":{"element-6066-11e4-a52e-4f735466cecf":"47898438-5185-4e1e-ac19-c5220a6f8fcf"}}]
1542386220908 Marionette TRACE 0 -> [0,26,"WebDriver:SwitchToFrame",{"element":"47898438-5185-4e1e-ac19-c5220a6f8fcf"}]
1542386220910 Marionette TRACE 0 <- [1,26,null,{"value":null}]
1542386220912 Marionette TRACE 0 -> [0,27,"WebDriver:FindElement",{"using":"css selector","value":"#button"}]
1542386220914 Marionette TRACE 0 <- [1,27,{"error":"no such element","message":"Unable to locate element: #button","stacktrace":"WebDri ... entError@chrome://marionette/content/error.js:388:5nelement.find/</<@chrome://marionette/content/element.js:339:16n"},null]
With the exception of the failure to find the element at the end, the logs look the same in the case where Selenium can select the iframe context.
I see this using both Chromedriver and Geckodriver. Why can't Selenium switch to the correct context sometimes? When I open the Chrome (or Firefox) console, I can see the contents of the iframe there so Selenium should be able to find the element. Does Selenium have a problem with switching to injected iframes?
Previous answers deal with finding elements within the iframe without switching to the iframe first. In this case, I am switching to the iframe but even then I don't get the correct context. No idea why this was marked as duplicate.
python selenium testing selenium-webdriver
Have you tried running JavaScript executor for Selenium to interact with the button?. I had a similar problem trying to interact with a form inside an iframe, but switching between frames worked fine for me.
– Alejandro Haro
Nov 16 '18 at 18:40
Are you sure that driver.page_source changes when you switch browsing contexts?
– Metareven
Nov 16 '18 at 18:41
@Metareven It doesn't. That's the problem: if browsing context switched correctly, I'd expect to get the iframe's page source, not the whole page's.
– Robert Hartley
Nov 16 '18 at 20:14
@AlejandroHaro Interesting idea - do you have any sample code for selecting a button within an iframe and clicking it?
– Robert Hartley
Nov 16 '18 at 20:48
@RobertHartley, I will add my C# code. Hope it helps.
– Alejandro Haro
Nov 20 '18 at 14:51
|
show 1 more comment
I have Selenium tests (written in Python) that find an iframe using its ID, and then looks for a button inside the iframe. The iframe is injected by a browser extension. This is the Python code:
id_ = "my-special-iframe"
driver.switch_to.frame(driver.find_element_by_id(id_))
polling.poll(
target=lambda:driver.find_elements_by_id("button")),
timeout=10,
step=1)
Where an element with the id "button" exists within the iframe but not outside it.
What I observe is the call to switch_to
succeeds, but half of the time the context remains the default context at the top of the DOM. I know this because the result of driver.page_source
prints the whole DOM (as if the default context was selected), and not that of the iframe. Moreover, I can call switch_to
as many times as I want (because, being within the main DOM rather than within the iframe, I can select the iframe). So it follows that Selenium can't find the button element.
Here's the logs from Geckodriver in an instance where Selenium fails to get into the iframe's context:
1542386220903 Marionette TRACE 0 -> [0,25,"WebDriver:FindElement",{"using":"xpath","value":"//iframe[@id='my-special-frame']"}]
1542386220906 Marionette TRACE 0 <- [1,25,null,{"value":{"element-6066-11e4-a52e-4f735466cecf":"47898438-5185-4e1e-ac19-c5220a6f8fcf"}}]
1542386220908 Marionette TRACE 0 -> [0,26,"WebDriver:SwitchToFrame",{"element":"47898438-5185-4e1e-ac19-c5220a6f8fcf"}]
1542386220910 Marionette TRACE 0 <- [1,26,null,{"value":null}]
1542386220912 Marionette TRACE 0 -> [0,27,"WebDriver:FindElement",{"using":"css selector","value":"#button"}]
1542386220914 Marionette TRACE 0 <- [1,27,{"error":"no such element","message":"Unable to locate element: #button","stacktrace":"WebDri ... entError@chrome://marionette/content/error.js:388:5nelement.find/</<@chrome://marionette/content/element.js:339:16n"},null]
With the exception of the failure to find the element at the end, the logs look the same in the case where Selenium can select the iframe context.
I see this using both Chromedriver and Geckodriver. Why can't Selenium switch to the correct context sometimes? When I open the Chrome (or Firefox) console, I can see the contents of the iframe there so Selenium should be able to find the element. Does Selenium have a problem with switching to injected iframes?
Previous answers deal with finding elements within the iframe without switching to the iframe first. In this case, I am switching to the iframe but even then I don't get the correct context. No idea why this was marked as duplicate.
python selenium testing selenium-webdriver
I have Selenium tests (written in Python) that find an iframe using its ID, and then looks for a button inside the iframe. The iframe is injected by a browser extension. This is the Python code:
id_ = "my-special-iframe"
driver.switch_to.frame(driver.find_element_by_id(id_))
polling.poll(
target=lambda:driver.find_elements_by_id("button")),
timeout=10,
step=1)
Where an element with the id "button" exists within the iframe but not outside it.
What I observe is the call to switch_to
succeeds, but half of the time the context remains the default context at the top of the DOM. I know this because the result of driver.page_source
prints the whole DOM (as if the default context was selected), and not that of the iframe. Moreover, I can call switch_to
as many times as I want (because, being within the main DOM rather than within the iframe, I can select the iframe). So it follows that Selenium can't find the button element.
Here's the logs from Geckodriver in an instance where Selenium fails to get into the iframe's context:
1542386220903 Marionette TRACE 0 -> [0,25,"WebDriver:FindElement",{"using":"xpath","value":"//iframe[@id='my-special-frame']"}]
1542386220906 Marionette TRACE 0 <- [1,25,null,{"value":{"element-6066-11e4-a52e-4f735466cecf":"47898438-5185-4e1e-ac19-c5220a6f8fcf"}}]
1542386220908 Marionette TRACE 0 -> [0,26,"WebDriver:SwitchToFrame",{"element":"47898438-5185-4e1e-ac19-c5220a6f8fcf"}]
1542386220910 Marionette TRACE 0 <- [1,26,null,{"value":null}]
1542386220912 Marionette TRACE 0 -> [0,27,"WebDriver:FindElement",{"using":"css selector","value":"#button"}]
1542386220914 Marionette TRACE 0 <- [1,27,{"error":"no such element","message":"Unable to locate element: #button","stacktrace":"WebDri ... entError@chrome://marionette/content/error.js:388:5nelement.find/</<@chrome://marionette/content/element.js:339:16n"},null]
With the exception of the failure to find the element at the end, the logs look the same in the case where Selenium can select the iframe context.
I see this using both Chromedriver and Geckodriver. Why can't Selenium switch to the correct context sometimes? When I open the Chrome (or Firefox) console, I can see the contents of the iframe there so Selenium should be able to find the element. Does Selenium have a problem with switching to injected iframes?
Previous answers deal with finding elements within the iframe without switching to the iframe first. In this case, I am switching to the iframe but even then I don't get the correct context. No idea why this was marked as duplicate.
python selenium testing selenium-webdriver
python selenium testing selenium-webdriver
edited Nov 16 '18 at 20:16
Robert Hartley
asked Nov 16 '18 at 18:23
Robert HartleyRobert Hartley
113
113
Have you tried running JavaScript executor for Selenium to interact with the button?. I had a similar problem trying to interact with a form inside an iframe, but switching between frames worked fine for me.
– Alejandro Haro
Nov 16 '18 at 18:40
Are you sure that driver.page_source changes when you switch browsing contexts?
– Metareven
Nov 16 '18 at 18:41
@Metareven It doesn't. That's the problem: if browsing context switched correctly, I'd expect to get the iframe's page source, not the whole page's.
– Robert Hartley
Nov 16 '18 at 20:14
@AlejandroHaro Interesting idea - do you have any sample code for selecting a button within an iframe and clicking it?
– Robert Hartley
Nov 16 '18 at 20:48
@RobertHartley, I will add my C# code. Hope it helps.
– Alejandro Haro
Nov 20 '18 at 14:51
|
show 1 more comment
Have you tried running JavaScript executor for Selenium to interact with the button?. I had a similar problem trying to interact with a form inside an iframe, but switching between frames worked fine for me.
– Alejandro Haro
Nov 16 '18 at 18:40
Are you sure that driver.page_source changes when you switch browsing contexts?
– Metareven
Nov 16 '18 at 18:41
@Metareven It doesn't. That's the problem: if browsing context switched correctly, I'd expect to get the iframe's page source, not the whole page's.
– Robert Hartley
Nov 16 '18 at 20:14
@AlejandroHaro Interesting idea - do you have any sample code for selecting a button within an iframe and clicking it?
– Robert Hartley
Nov 16 '18 at 20:48
@RobertHartley, I will add my C# code. Hope it helps.
– Alejandro Haro
Nov 20 '18 at 14:51
Have you tried running JavaScript executor for Selenium to interact with the button?. I had a similar problem trying to interact with a form inside an iframe, but switching between frames worked fine for me.
– Alejandro Haro
Nov 16 '18 at 18:40
Have you tried running JavaScript executor for Selenium to interact with the button?. I had a similar problem trying to interact with a form inside an iframe, but switching between frames worked fine for me.
– Alejandro Haro
Nov 16 '18 at 18:40
Are you sure that driver.page_source changes when you switch browsing contexts?
– Metareven
Nov 16 '18 at 18:41
Are you sure that driver.page_source changes when you switch browsing contexts?
– Metareven
Nov 16 '18 at 18:41
@Metareven It doesn't. That's the problem: if browsing context switched correctly, I'd expect to get the iframe's page source, not the whole page's.
– Robert Hartley
Nov 16 '18 at 20:14
@Metareven It doesn't. That's the problem: if browsing context switched correctly, I'd expect to get the iframe's page source, not the whole page's.
– Robert Hartley
Nov 16 '18 at 20:14
@AlejandroHaro Interesting idea - do you have any sample code for selecting a button within an iframe and clicking it?
– Robert Hartley
Nov 16 '18 at 20:48
@AlejandroHaro Interesting idea - do you have any sample code for selecting a button within an iframe and clicking it?
– Robert Hartley
Nov 16 '18 at 20:48
@RobertHartley, I will add my C# code. Hope it helps.
– Alejandro Haro
Nov 20 '18 at 14:51
@RobertHartley, I will add my C# code. Hope it helps.
– Alejandro Haro
Nov 20 '18 at 14:51
|
show 1 more comment
1 Answer
1
active
oldest
votes
wait.Until(ExpectedConditions.ElementToBeClickable(By.Id("formstack")));
driver.SwitchTo().Frame("formstack");
var FirstName = driver.FindElement(By.CssSelector("#field58374948"));
FirstName.Click();
FirstName.SendKeys("Alejandro");
driver.SwitchTo().defaultContent();
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53343417%2fselenium-cant-select-context-of-an-iframe-intermittently%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
wait.Until(ExpectedConditions.ElementToBeClickable(By.Id("formstack")));
driver.SwitchTo().Frame("formstack");
var FirstName = driver.FindElement(By.CssSelector("#field58374948"));
FirstName.Click();
FirstName.SendKeys("Alejandro");
driver.SwitchTo().defaultContent();
add a comment |
wait.Until(ExpectedConditions.ElementToBeClickable(By.Id("formstack")));
driver.SwitchTo().Frame("formstack");
var FirstName = driver.FindElement(By.CssSelector("#field58374948"));
FirstName.Click();
FirstName.SendKeys("Alejandro");
driver.SwitchTo().defaultContent();
add a comment |
wait.Until(ExpectedConditions.ElementToBeClickable(By.Id("formstack")));
driver.SwitchTo().Frame("formstack");
var FirstName = driver.FindElement(By.CssSelector("#field58374948"));
FirstName.Click();
FirstName.SendKeys("Alejandro");
driver.SwitchTo().defaultContent();
wait.Until(ExpectedConditions.ElementToBeClickable(By.Id("formstack")));
driver.SwitchTo().Frame("formstack");
var FirstName = driver.FindElement(By.CssSelector("#field58374948"));
FirstName.Click();
FirstName.SendKeys("Alejandro");
driver.SwitchTo().defaultContent();
answered Nov 20 '18 at 14:54
Alejandro HaroAlejandro Haro
686
686
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53343417%2fselenium-cant-select-context-of-an-iframe-intermittently%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Have you tried running JavaScript executor for Selenium to interact with the button?. I had a similar problem trying to interact with a form inside an iframe, but switching between frames worked fine for me.
– Alejandro Haro
Nov 16 '18 at 18:40
Are you sure that driver.page_source changes when you switch browsing contexts?
– Metareven
Nov 16 '18 at 18:41
@Metareven It doesn't. That's the problem: if browsing context switched correctly, I'd expect to get the iframe's page source, not the whole page's.
– Robert Hartley
Nov 16 '18 at 20:14
@AlejandroHaro Interesting idea - do you have any sample code for selecting a button within an iframe and clicking it?
– Robert Hartley
Nov 16 '18 at 20:48
@RobertHartley, I will add my C# code. Hope it helps.
– Alejandro Haro
Nov 20 '18 at 14:51