Selenium: finding the right element identifier
I'm relatively new to selenium automation, and am trying to test a form that has been created for me. I'd like to put test values into a textbox whose HTML code looks like:
<input type="text" id="txtEndCustId" onkeypress="return isNumberKey(event);" class="Text" required="" onfocus="txtEndCustIdFocus('Your customer END CUSTOMER DEP ID');" onblur="txtEndCustIdblur();">
I thought I'd use the "id" of the box, but the code:
Dim options As New Chrome.ChromeOptions
Dim service As ChromeDriverService = ChromeDriverService.CreateDefaultService
Dim wd As New Chrome.ChromeDriver(service, options)
wd.Navigate.GoToUrl("x")
wd.FindElementById("txtEndCustId").SendKeys("1")
Gives an error:
Unable to locate element: {"method":"id","selector":"txtEndCustId"}
To be perfectly honest I've no idea where I went wrong here - thanks in advance for your help.
Is there any "List All Element ids" function, so that I can marry them up with what's shown in the chrome inspector view?
vb.net selenium selenium-chromedriver
|
show 1 more comment
I'm relatively new to selenium automation, and am trying to test a form that has been created for me. I'd like to put test values into a textbox whose HTML code looks like:
<input type="text" id="txtEndCustId" onkeypress="return isNumberKey(event);" class="Text" required="" onfocus="txtEndCustIdFocus('Your customer END CUSTOMER DEP ID');" onblur="txtEndCustIdblur();">
I thought I'd use the "id" of the box, but the code:
Dim options As New Chrome.ChromeOptions
Dim service As ChromeDriverService = ChromeDriverService.CreateDefaultService
Dim wd As New Chrome.ChromeDriver(service, options)
wd.Navigate.GoToUrl("x")
wd.FindElementById("txtEndCustId").SendKeys("1")
Gives an error:
Unable to locate element: {"method":"id","selector":"txtEndCustId"}
To be perfectly honest I've no idea where I went wrong here - thanks in advance for your help.
Is there any "List All Element ids" function, so that I can marry them up with what's shown in the chrome inspector view?
vb.net selenium selenium-chromedriver
Please provide the entire line of code where you are trying to get the element by it's ID, (...) does not give enough information.
– Ryan Wilson
Nov 21 '18 at 15:52
Sorry, it's just the chromedriver object - I've expanded the sample code now
– Martin KS
Nov 21 '18 at 16:01
IS it possible you are trying to get the element before the page has fully loaded?? You may want to read this SO post on how to do a wait with webdriver (stackoverflow.com/questions/13244225/…)
– Ryan Wilson
Nov 21 '18 at 16:07
no, I'm stepping through the code in the debugger, and it doesn't even allow me to step to that line before the page has finished the 'navigate' line and fully loaded - or at least it appears visually to be fully loaded, and the chromium window can see the element if I hit F12 there.
– Martin KS
Nov 21 '18 at 16:14
@MartingKS is the element you want wrapped inside a frame, iframe?
– Ryan Wilson
Nov 21 '18 at 16:18
|
show 1 more comment
I'm relatively new to selenium automation, and am trying to test a form that has been created for me. I'd like to put test values into a textbox whose HTML code looks like:
<input type="text" id="txtEndCustId" onkeypress="return isNumberKey(event);" class="Text" required="" onfocus="txtEndCustIdFocus('Your customer END CUSTOMER DEP ID');" onblur="txtEndCustIdblur();">
I thought I'd use the "id" of the box, but the code:
Dim options As New Chrome.ChromeOptions
Dim service As ChromeDriverService = ChromeDriverService.CreateDefaultService
Dim wd As New Chrome.ChromeDriver(service, options)
wd.Navigate.GoToUrl("x")
wd.FindElementById("txtEndCustId").SendKeys("1")
Gives an error:
Unable to locate element: {"method":"id","selector":"txtEndCustId"}
To be perfectly honest I've no idea where I went wrong here - thanks in advance for your help.
Is there any "List All Element ids" function, so that I can marry them up with what's shown in the chrome inspector view?
vb.net selenium selenium-chromedriver
I'm relatively new to selenium automation, and am trying to test a form that has been created for me. I'd like to put test values into a textbox whose HTML code looks like:
<input type="text" id="txtEndCustId" onkeypress="return isNumberKey(event);" class="Text" required="" onfocus="txtEndCustIdFocus('Your customer END CUSTOMER DEP ID');" onblur="txtEndCustIdblur();">
I thought I'd use the "id" of the box, but the code:
Dim options As New Chrome.ChromeOptions
Dim service As ChromeDriverService = ChromeDriverService.CreateDefaultService
Dim wd As New Chrome.ChromeDriver(service, options)
wd.Navigate.GoToUrl("x")
wd.FindElementById("txtEndCustId").SendKeys("1")
Gives an error:
Unable to locate element: {"method":"id","selector":"txtEndCustId"}
To be perfectly honest I've no idea where I went wrong here - thanks in advance for your help.
Is there any "List All Element ids" function, so that I can marry them up with what's shown in the chrome inspector view?
vb.net selenium selenium-chromedriver
vb.net selenium selenium-chromedriver
edited Nov 21 '18 at 16:00
Martin KS
asked Nov 21 '18 at 15:51
Martin KSMartin KS
145319
145319
Please provide the entire line of code where you are trying to get the element by it's ID, (...) does not give enough information.
– Ryan Wilson
Nov 21 '18 at 15:52
Sorry, it's just the chromedriver object - I've expanded the sample code now
– Martin KS
Nov 21 '18 at 16:01
IS it possible you are trying to get the element before the page has fully loaded?? You may want to read this SO post on how to do a wait with webdriver (stackoverflow.com/questions/13244225/…)
– Ryan Wilson
Nov 21 '18 at 16:07
no, I'm stepping through the code in the debugger, and it doesn't even allow me to step to that line before the page has finished the 'navigate' line and fully loaded - or at least it appears visually to be fully loaded, and the chromium window can see the element if I hit F12 there.
– Martin KS
Nov 21 '18 at 16:14
@MartingKS is the element you want wrapped inside a frame, iframe?
– Ryan Wilson
Nov 21 '18 at 16:18
|
show 1 more comment
Please provide the entire line of code where you are trying to get the element by it's ID, (...) does not give enough information.
– Ryan Wilson
Nov 21 '18 at 15:52
Sorry, it's just the chromedriver object - I've expanded the sample code now
– Martin KS
Nov 21 '18 at 16:01
IS it possible you are trying to get the element before the page has fully loaded?? You may want to read this SO post on how to do a wait with webdriver (stackoverflow.com/questions/13244225/…)
– Ryan Wilson
Nov 21 '18 at 16:07
no, I'm stepping through the code in the debugger, and it doesn't even allow me to step to that line before the page has finished the 'navigate' line and fully loaded - or at least it appears visually to be fully loaded, and the chromium window can see the element if I hit F12 there.
– Martin KS
Nov 21 '18 at 16:14
@MartingKS is the element you want wrapped inside a frame, iframe?
– Ryan Wilson
Nov 21 '18 at 16:18
Please provide the entire line of code where you are trying to get the element by it's ID, (...) does not give enough information.
– Ryan Wilson
Nov 21 '18 at 15:52
Please provide the entire line of code where you are trying to get the element by it's ID, (...) does not give enough information.
– Ryan Wilson
Nov 21 '18 at 15:52
Sorry, it's just the chromedriver object - I've expanded the sample code now
– Martin KS
Nov 21 '18 at 16:01
Sorry, it's just the chromedriver object - I've expanded the sample code now
– Martin KS
Nov 21 '18 at 16:01
IS it possible you are trying to get the element before the page has fully loaded?? You may want to read this SO post on how to do a wait with webdriver (stackoverflow.com/questions/13244225/…)
– Ryan Wilson
Nov 21 '18 at 16:07
IS it possible you are trying to get the element before the page has fully loaded?? You may want to read this SO post on how to do a wait with webdriver (stackoverflow.com/questions/13244225/…)
– Ryan Wilson
Nov 21 '18 at 16:07
no, I'm stepping through the code in the debugger, and it doesn't even allow me to step to that line before the page has finished the 'navigate' line and fully loaded - or at least it appears visually to be fully loaded, and the chromium window can see the element if I hit F12 there.
– Martin KS
Nov 21 '18 at 16:14
no, I'm stepping through the code in the debugger, and it doesn't even allow me to step to that line before the page has finished the 'navigate' line and fully loaded - or at least it appears visually to be fully loaded, and the chromium window can see the element if I hit F12 there.
– Martin KS
Nov 21 '18 at 16:14
@MartingKS is the element you want wrapped inside a frame, iframe?
– Ryan Wilson
Nov 21 '18 at 16:18
@MartingKS is the element you want wrapped inside a frame, iframe?
– Ryan Wilson
Nov 21 '18 at 16:18
|
show 1 more comment
1 Answer
1
active
oldest
votes
Since your element is inside a frame, you need to navigate the driver to the frame first, a frame has it's own Document containing x number of DOM elements.
First switch to the frame:
//You need to use method switchTo() to set the driver to the frame document
//In this case we are passing in the id of the iframe
wd.switchTo().frame("ctl00_CPH_iframeCat");
//Now that the driver is working on the frame document, you should be able to manipulate the
//input you want
wd.FindElementById("txtEndCustId").SendKeys("1")
//Finally, switch the driver back to the main page document (original document which contains the frames)
driver.switchTo().defaultContent();
Here is a good piece of documentation which should explain this in more detail:
(http://www.assertselenium.com/webdriver/handling-iframes-using-webdriver/)
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%2f53415788%2fselenium-finding-the-right-element-identifier%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
Since your element is inside a frame, you need to navigate the driver to the frame first, a frame has it's own Document containing x number of DOM elements.
First switch to the frame:
//You need to use method switchTo() to set the driver to the frame document
//In this case we are passing in the id of the iframe
wd.switchTo().frame("ctl00_CPH_iframeCat");
//Now that the driver is working on the frame document, you should be able to manipulate the
//input you want
wd.FindElementById("txtEndCustId").SendKeys("1")
//Finally, switch the driver back to the main page document (original document which contains the frames)
driver.switchTo().defaultContent();
Here is a good piece of documentation which should explain this in more detail:
(http://www.assertselenium.com/webdriver/handling-iframes-using-webdriver/)
add a comment |
Since your element is inside a frame, you need to navigate the driver to the frame first, a frame has it's own Document containing x number of DOM elements.
First switch to the frame:
//You need to use method switchTo() to set the driver to the frame document
//In this case we are passing in the id of the iframe
wd.switchTo().frame("ctl00_CPH_iframeCat");
//Now that the driver is working on the frame document, you should be able to manipulate the
//input you want
wd.FindElementById("txtEndCustId").SendKeys("1")
//Finally, switch the driver back to the main page document (original document which contains the frames)
driver.switchTo().defaultContent();
Here is a good piece of documentation which should explain this in more detail:
(http://www.assertselenium.com/webdriver/handling-iframes-using-webdriver/)
add a comment |
Since your element is inside a frame, you need to navigate the driver to the frame first, a frame has it's own Document containing x number of DOM elements.
First switch to the frame:
//You need to use method switchTo() to set the driver to the frame document
//In this case we are passing in the id of the iframe
wd.switchTo().frame("ctl00_CPH_iframeCat");
//Now that the driver is working on the frame document, you should be able to manipulate the
//input you want
wd.FindElementById("txtEndCustId").SendKeys("1")
//Finally, switch the driver back to the main page document (original document which contains the frames)
driver.switchTo().defaultContent();
Here is a good piece of documentation which should explain this in more detail:
(http://www.assertselenium.com/webdriver/handling-iframes-using-webdriver/)
Since your element is inside a frame, you need to navigate the driver to the frame first, a frame has it's own Document containing x number of DOM elements.
First switch to the frame:
//You need to use method switchTo() to set the driver to the frame document
//In this case we are passing in the id of the iframe
wd.switchTo().frame("ctl00_CPH_iframeCat");
//Now that the driver is working on the frame document, you should be able to manipulate the
//input you want
wd.FindElementById("txtEndCustId").SendKeys("1")
//Finally, switch the driver back to the main page document (original document which contains the frames)
driver.switchTo().defaultContent();
Here is a good piece of documentation which should explain this in more detail:
(http://www.assertselenium.com/webdriver/handling-iframes-using-webdriver/)
answered Nov 21 '18 at 16:52
Ryan WilsonRyan Wilson
3,7941518
3,7941518
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%2f53415788%2fselenium-finding-the-right-element-identifier%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
Please provide the entire line of code where you are trying to get the element by it's ID, (...) does not give enough information.
– Ryan Wilson
Nov 21 '18 at 15:52
Sorry, it's just the chromedriver object - I've expanded the sample code now
– Martin KS
Nov 21 '18 at 16:01
IS it possible you are trying to get the element before the page has fully loaded?? You may want to read this SO post on how to do a wait with webdriver (stackoverflow.com/questions/13244225/…)
– Ryan Wilson
Nov 21 '18 at 16:07
no, I'm stepping through the code in the debugger, and it doesn't even allow me to step to that line before the page has finished the 'navigate' line and fully loaded - or at least it appears visually to be fully loaded, and the chromium window can see the element if I hit F12 there.
– Martin KS
Nov 21 '18 at 16:14
@MartingKS is the element you want wrapped inside a frame, iframe?
– Ryan Wilson
Nov 21 '18 at 16:18