简体   繁体   中英

retrive value of read only field using Selenium Webdriver

Update -- I have added the entire HTML code below for easier understanding of the issue. I have tried using the xpath findElement(By.xpath(".//div[@id='ContainerCell']/span/[@id='StartDate']/input[@id='StartDate_txtInput']")).getText();

I still get the error:

The given selector .//div[@id='ContainerCell']/span/[@id='StartDate']/input[@id='StartDate_txtInput'] is either invalid or does not result in a WebElement. The following error occurred:
InvalidSelectorError: Unable to locate an element with the xpath expression .//div[@id='ContainerCell']/span/[@id='StartDate']/input[@id='StartDate_txtInput'] because of the following error:
SyntaxError: The expression is not a legal expression.

Using Selenium Webdriver I need to retrieve the value of a read only field. This field contains a date which I need to compare to another date. I have tried all methods (find element by id / class name / xpath etc) it always returns element not found . I suspect this is because the input filed resides inside a span which resides inside a div but being new to java I am unable to get to this field.

Value I am looking for is <input id="StartDate_txtInput">

HTML code below.

#document
    <!--

    [if lte IE 8]>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD …

    -->
    <!--

    [if gte IE 9]>
    <!DOCTYPE HTML>
    <![endif]

    -->
    <!--

    [if !IE]> 

    -->
    <!DOCTYPE html>
    <!--

     <![endif]

    -->
    <html webdriver="true">

<head id="RuntimeForm"></head>
<body id="RuntimeFormBody" onload="InitialiseRuntimeForm();setInitialSize();" onbeforeunload="return PromptWhenClosing(event)" style="overflow: hidden; margin: 0px;" onresize="resizeContainerCell()" tabindex="-1">
    <form id="Form1" style="height: 100%;" onkeydown="return FormKeyDown(event)" onkeypress="return FormKeyPress(event)" onsubmit="javascript:return WebForm_OnSubmit();" action="RunTimeForm.aspx?ScreenID=8142758e-269a-41f0-b551-433e56dd1225" method="post" name="Form1">

<div></div>
<script type="text/javascript"></script>
<script type="text/javascript" src="/V14_ST1/WebResource.axd?d=5sNVsMkS2BZs-uoFEc4iQlsiyiLjXyZMN…nDsYTHfbaZu1hg-eY3e5am9_qL4BG_2h5iaVRw1&t=635662391674952392"></script>
<script></script>
<script></script>
<script></script>
<script type="text/javascript" src="/V14_ST1/WebResource.axd?d=s_BonnG3MwQl_dl8IfLCFCykZO1rmMJx-…odgFonyPZXsgvo4Ui0T7XvpKyRoUhQlSwxeG541&t=635662391674952392"></script>
<script src="/V14_ST1/UltimateSpellInclude/UltimateSpell.core.js" language="javascript" type="text/javascript"></script>
<script language="javascript" type="text/javascript"></script>
<script type="text/javascript"></script>
<input id="hdAccessKeyArea" type="hidden" value="8" name="hdAccessKeyArea"></input>
<a id="AccessKeyLink" accesskey="0" onactivate="ShowAccessKeyHelp('false',$('#hdAccessKeyArea')[0].value);this.blur();" title="Access Key Help" tabindex="-1" href="javascript:ShowAccessKeyHelp(false,$('#hdAccessKeyArea')[0].value);"></a>
<div id="ValidationSummary" style="color:Red;height:22px;width:197px;z-index:102;left:13px;position:absolute;top:420px;display:none;"></div>
<div id="tblContainer" style="display: block; width: 100%;">

                            <!--

                             Header ToolBar 

                            -->
                            <div id="trToolbarHeaderContainer" style="height: auto; width: 100%;"></div>
                            <!--

                             Runtime form content 

                            -->
                            <div id="Content" style="position:relative; background-color: #F9FAF5; width: 100%; height: 100%;">
                                <!--

                                 Runtime form cotrols 

                                -->
                                <div id="ContainerCell" style="overflow: auto; width: 459px; height: 305px; position: relative; background-color: rgb(250, 250, 250); cursor: default;"></div>
                                <!--

                                 Refresh panel 

                                -->
                                <div id="RefreshPanel" style="position: absolute; left: 0px; top: 0px; width: 100%; height: 100%; display: none"></div>
                                <!--

                                 Footer ToolBar 

                                -->
                                <div id="trToolbarFooterContainer" style="width: 100%;"></div>
                                <div id="trToolbarMobileContainer" style="width: 100%;"></div>
                                <div id="dialog" title=""></div>
                                ::after
                            </div>
                            <input id="hdSubRecordDelete" type="hidden" name="hdSubRecordDelete"></input>
                            <input id="hdRecordID" type="hidden" value="00000000-0000-0000-0000-000000000000" name="hdRecordID"></input>
                            <input id="hdTableName" type="hidden" name="hdTableName"></input>
                            <input id="hdNoDisplayScreen" type="hidden" value=" No display screen assigned for the current Grid." name="hdNoDisplayScreen"></input>
                            <input id="hdParentRecordID" type="hidden" value="00000000-0000-0000-0000-000000000000" name="hdParentRecordID"></input>
                            <input id="hdHelpURL" type="hidden" name="hdHelpURL"></input>
                            <input id="hdHelpExists" type="hidden" value="html" name="hdHelpExists"></input>
                            <input id="hdSaveRecordFirstMessage" type="hidden" value="You must save the current record before carrying out this action." name="hdSaveRecordFirstMessage"></input>
                            <input id="hdDBSearchAddInID" type="hidden" name="hdDBSearchAddInID"></input>
                            <input id="ReadOnly" type="hidden" value="1" name="ReadOnly"></input>
                            <input id="hdHelpTitle" type="hidden" value=" Help " name="hdHelpTitle"></input>
                            <input id="hdDateLastModified" type="hidden" name="hdDateLastModified"></input>
                            <input id="ChangesAction" type="hidden" value="0" name="ChangesAction"></input>
                            <input id="hdFormLastModified" type="hidden" value="07/01/2015 08:20:11" name="hdFormLastModified"></input>
                            <input id="hdIsChangesMade" type="hidden" name="hdIsChangesMade"></input>
                            <input id="ImpersonationToken" type="hidden" name="ImpersonationToken"></input>
                            <input id="HRImpersonationToken" type="hidden" value="EcwWpmHtjrWmn9UTEYVeW0QB4wsmk2a+IdSkkHq9ENs=" name="HRImpersonationToken"></input>
                            <input id="ImpersonationId" type="hidden" name="ImpersonationId"></input>
                            <input id="InstanceID" type="hidden" value="0538a35f-03d7-e511-968c-8e001800001f" name="InstanceID"></input>
                            <input id="hdScreenId" type="hidden" value="8142758e-269a-41f0-b551-433e56dd1225" name="hdScreenId"></input>
                            <input id="hdScreenIdentifiers" type="hidden" name="hdScreenIdentifiers"></input>
                            <input id="hdUniqueID" type="hidden" value="d0b819a3-47b6-4cf1-a08b-983e98d0a673" name="hdUniqueID"></input>
                            <input id="hdFormDataset" type="hidden" value="AAEAAAD/////AQAAAAAAAAAMAgAAAE5TeXN0ZW0uRGF0YSwgVmVyc2lvbj00…cgZfQnVpbGQJX1JldmlzaW9uAAAAAAgICAgCAAAAAAAAAP//////////Cw==" name="hdFormDataset"></input>
                            <input id="hdRecordIDList" type="hidden" name="hdRecordIDList"></input>
                            <input id="hdVersion" type="hidden" name="hdVersion"></input>
                            <input id="hdAutoCompleteMobileOnly" type="hidden" value="True" name="hdAutoCompleteMobileOnly"></input>
                            <input id="hdWorkflowSubmitted" type="hidden" value="false" name="hdWorkflowSubmitted"></input>
                            <input id="hEventSource" type="hidden" name="hEventSource"></input>
                            <script type="text/javascript"></script>
                            <script type="text/javascript"></script>
                            <script></script>
                            <script type="text/javascript"></script>
                            <script language="javascript" type="text/javascript"></script>
                            <form target="DOCCONTAINER" action="" name="DocName" method="post"></form>
                            <iframe name="DOCCONTAINER" src="javascript:void(0)" style="visibility: hidden; display: none"></iframe>
                            <script type="text/javascript"></script>
                            <script type="text/javascript" language="javascript"></script>
                            <script></script>
                        </div>
                    </form>
                </body>
            </html>
        </html>
    </iframe>
    <script defer="" src="FormContainer.js" type="text/javascript"></script>
    <script type="text/javascript"></script>
</body>

So being inside a span shouldn't be a reason for Element Not Found. For now Please try following two things:

Use Wait : Generally it takes time for element to load and mostly Element not found issue is fixed with this.

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("StartDate_txtInput")));

Use getAttribute() : You might be using element.getText() for retrieving the text. Sometimes there is no text within the element, rather it's just set as an attribute say, value for input tag in this case. Try:

driver.findElement(By.id("StartDate_txtInput")).getAttribute("value");

Through ID

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

or use CSS selector

input[id="StartDate_txtInput"]

or xpath

//input[@id='StartDate_txtInput']

or relative xpath

//div[@id='ContainerSizeGuide']/span/input[@id='StartDate_txtInput']

or

//div[@id='ContainerSizeGuide']/span/input

Before using these path check iframe, if this div in the iframe first you have go to iframe then use above nay one path.

driver.switchTo().frame((WebElement) By.id("iframeID"));

else you have use webdriver explicitly wait

WebDriverWait wait=new WebDriverWait(driver, 5);
 wait.until(ExpectedConditions.visibilityOfElementLocated(null));

You have to use the attribute value instead of text.

The code goes as follows:

String date=driver.findElement(By.id("StartDate_txtInput")).getAttribute("value");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM