Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can select web element selenium web driver with html source inside another html tag

<html>
<head></head>
<body>
    <div id="title"></div>
    <div id="credit"></div>
    <div id="btnFS"></div>
    <div id="cont">
        <div id="fscont">
        <div style="font-size: 0px; position: absolute; left: 0; right: 0; top: …w: auto; overflow-y: auto; -webkit-overflow-scrolling:touch;"></div>
        <div style="font-size: 0px; position: absolute; left: 0; right: 0; height: 75px; overflow: hidden; bottom: 0;">
            <iframe width="100%" height="100%" frameborder="0" name="cboxform" scrolling="no" marginwidth="0" marginheight="0" src="//www4.cbox.ws/box/?boxid=4255329&boxtag=ev9nj4&sec=form" allowtransparency="yes">
                #document
                    <!DOCTYPE html>
                    <html style="position: absolute; height: 100%; width: 100%; overflow: hidden; margin: 0px; padding: 0px;">
                        <head></head>
                        <body class="fmbdy" style="padding: 0px; margin: 0px;">
                            <form class="cfrm" onsubmit="return do_post();" method="post" action="./?boxid=4255329&boxtag=ev9nj4&sec=submit" target="cboxmain" name="cbox">
                                <table width="100%" height="100%" cellspacing="0" cellpadding="0" border="0" style="width: auto;">
                                <tbody>
                                <tr></tr>
                                <tr>
                                <td id="tblmid" valign="top" style="vertical-align: top; white-space: nowrap; font-size: 0;" colspan="2">
                                    <input type="hidden" value="" name="key"></input>
                                    <input class="frmtb" type="text" onblur="frmblur(this, 'name');" onfocus="frmfocus(this, 'name');" value="name" size="9" autocomplete="off" spellcheck="false" name="nme" maxlength="25" style="box-sizing: content-box; width: 301px;"></input>

I have a chatbox page, which was created by cbox.ws and I want to select elements on this page.
I tried many ways to select the id tblmid in this web but I can't.
Many ways was used such as get by name, find xpath then get by xpath, find and get them by css... but it didn't work.

How can I get that element? I am using java.
I tried get step by step get by id cont -> get by id fscont -> get by name cboxform but I can't go anymore, stuck here.

WebElement inputName = driver.findElement(By.id("cont"));
inputName = inputName.findElement(By.id("fscont"));
inputName = inputName.findElement(By.name("cboxform"));

Thanks a lot.

like image 482
thienkhoi tran Avatar asked Sep 14 '25 20:09

thienkhoi tran


1 Answers

The desired element is inside an iframe, you need to switch to it before making a search:

driver.switchTo().frame("cboxform");

driver.findElement(By.id("tblmid"));

See also:

  • How to switch between frames in Selenium WebDriver using Java
like image 154
alecxe Avatar answered Sep 16 '25 12:09

alecxe