I want to get text "Entered code is already exists" using selenium webdriver , I tried using id="code_error" but no use
HTML code is as follows :
<div class="leftsection">
<div class="form-element">
<fieldset>
<label><span class="required">*</span>Code:</label>
<input type="text" maxlength="6" value="" id="code" name="code" style="border: 1px solid rgb(178, 178, 178);">
</fieldset>
<span role="alert" class="errormsg" id="code_error">Entered Code already exists</span>
</div>
i used xpath , id, cssselecor but it returns NULL.
Have a look at the below code..works perfectly
options=driver.find_elements_by_class_name("errormsg")
for i in options:
print i.text
when selenium can parse JavaScript, you can use this:
var text = document.getElementById('sbucode_error').innerHTML;
Please use Selenium Ide to check whether id=code_error selector pointing to your element then use the below mentioned code to get text from that.
driver.findElement(By.id("code_error")).getText();
String text = driver.findElementById("code_error").getText(); should do the trick for you :)
Related
Query 1:
I would request you to please help me on getting xpath for submit button. Absolute path starts with Select file and *.
<div id="bg">
<label id="label" style=" font-family: Segoe UI;color:#2e2e2e; font-size:12px; float:left; padding-top:8px;">
Select File <span id="spanhide" class="red">*</span></label>
<div style="margin-left:105px;"><input type="file" name="filUploadIcon" id="filUploadIcon" class="txt-box" onchange="FileUpload_OnChange(this,event);" style="width:180px;">
<input type="submit" name="btnUploadcancel" value="" onclick="return check();" id="btnUploadcancel" title="Upload" class="upload_pop"></div>
<input name="textFileName" type="text" id="textFileName" style="display:none;">
<input type="hidden" name="hdnframeID" id="hdnframeID">
<input type="hidden" name="hdnlbl" id="hdnlbl">
</div>
Query 2:
How to write xpath to skip few nodes in between. Please help. Also let me know adding // or * in between to skip nodes.
Ex: Above HTML
//*div[#id="bg"]/skip elements before input type submit node/input [#type="submit"]
Use the below xpath to target the precedence nodes that include the label with the string "Select File" and the embedded span that contains '*'.
//div[contains(#id, 'bg')]/label[contains(text(), 'Select File')]/span[contains(text(), '*')]
Then add on the below line to return to the parent node label to the span tag.
/parent::label
Then add on the below to get to the sibling div tag of the label tag, which contains the input tag with a type of submit.
/following-sibling::div/input[#type='submit']
So the xpath in its entirety should look like this:
//div[contains(#id, 'bg')]/label[contains(text(), 'Select File')]/span[contains(text(), '*')]/parent::label/following-sibling::div/input[#type='submit']
You can use below xpath.
//div[#id='bg']//input[#name='btnUploadcancel']
Strongly suggest to go through this to learn more on xpath 1.0 which will answer all your questions in OP.
Looking for a generic way to find text before an input field to know what to fill in the field. Using xpath, css selector or any other way possible.
<div>
<span>Full Name</span>
<input name="xddadN">
</div>
<div>
<span>Email</span>
<input name="xedadN">
</div>
Or
<div>
<div><label>Full Name</label></div>
<div><input name="xddadN"></div>
<div><label>Email</label></div>
<div><input name="xedadN"></
</div>
Or
<div>
<label>Full Name<br>
<span><input name="xddadN"></span>
</label>
</div>
<div>
<label>Full Name<br>
<span><input name="xddadN"></span>
</label>
</div>
You can try below XPath expression to get preceding text node:
//input/preceding::*[1]
or more specific for Full Name
//input[#name="xddadN"]/preceding::*[1]
and Email:
//input[#name="xedadN"]/preceding::*[1]
For full name use this Xpath : //input[#name='xddadN']/preceding-sibling::span
code :
String fullName = driver.findElement(By.Xpath(//input[#name='xddadN']/preceding-sibling::span)).getText();
String Email = driver.findElement(By.Xpath(//input[#name='xedadN']/preceding-sibling::span)).getText();
You haven't mentioned any Selenium Language Binding Art so I will be using Java for the example.
First the Answer
Yes, you can use a generic way to find text before an input field as follows :
As per the HTML :
<div>
<span>Full Name</span>
<input name="xddadN">
</div>
<div>
<span>Email</span>
<input name="xedadN">
</div>
To retrieve the text Full Name from the <span> tag with respect to the <input> tag you can use :
String myText = driver.findElement(By.xpath("//input[#name='xddadN']//preceding::*[1]")).getAttribute("innerHTML");
Now the Pitfall
Without any visibility to your usecase in my opinion the generic way would be a pitfall which will induce much chaos and uncertanity for the following reasons :
As per the xpath we are straightway jumping into the previous element, a small change in the HTML DOM (e.g. inclusion of a <span> tag) will make your Testcases to Fail.
In general, while constructing a Locator Strategy through css-selectors or xpath it will be benificial to include the <tagName> to optimize the element search process. If <tagName> are not included your Tests will require more time to locate the elements and perform action on them. In this process you are compromising some of the advantages of Test Automation.
Conclusion
Hence as a conclusion as per the Best Practices always include the <tagName> while constructing a Locator Strategy through css-selectors or xpath.
I wrote xpath for below HTML code i.e. displayed below
1. //a[#text()='Life Insurance']
2. //span[#text()='Apply now']
But I got element not found exception. If I used Absolute xpath processor then It's working and I wrote own xpath then it thrown exception.
Please tell me how to write it.
Below are the HTML code for which I need xpath.
1.<a class="mainlink" href="https://leads.hdfcbank.com/applications/webforms/apply/HDFC_Life_Click2Protect/index.aspx?promocode=P4_hp_AppNow_LI" target="" rel="nofollow width=375 height=213">Life Insurance</a>
2." <div class="menutext"> <span class="mainlink">Apply now</span> <img class="pointer" alt="Pointer" src="/assets/images/nav_pointer.png" style="display: none;"> </div> "
Try these
For 1
//a[text()='Life Insurance']
For 2
//span[text()='Apply now']
You have to remove '#' in your code.
(or)
You can also use:
//a[contains(text(),'Life Insurance')]
For 2
//span[contains(text(),'Apply now')]
I would like to type in the date as following: 2015/05/05 in to that field. the idea is to use the ends-with command. So far the following didn't work
//input[ends-with(#id,'_ExecutionDate')and contains(.,'ExecutionDate')][#class='emphasis']
//div[ends-with(#id,'_ExecutionDate')and contains(.,'ExecutionDate')][#class='emphasis']
Here is a XPATH from Selenium's Select option:
xpath=(//input[#id='44d02654-39b3-447e-904d-8d3c7ca016b6.ExecutionDate'])[3]
Her is an xpath from Firebug:
.//*[#id='44d02654-39b3-447e-904d-8d3c7ca016b6.ExecutionDate']
Here is a code:
<div class="pull-right" data-bind="visible: view.isIndividual"><!-- ko foreach: signingParties -->
<a id="44d02654-39b3-447e-904d-8d3c7ca016b6.ExecutionDate" class="emphasis" style="margin-right:5px" data-bind="visible: !$root.locked(), click: $root.transferOfLand.view.editExecutionDates, valName: 'ExecutionDate', text: view.executionDateDisplay()" href="#" name="44d02654-39b3-447e-904d-8d3c7ca016b6.ExecutionDate" data-val="LandTransferDocument.Transferor.0.SigningParty.ExecutionDate">Add Execution Date...</a>
<span id="44d02654-39b3-447e-904d-8d3c7ca016b6.ExecutionDate" style="margin-right: 5px; display: none;" data-bind="visible: $root.locked(), valName: 'ExecutionDate', text: view.executionDateDisplayReadOnly()" name="44d02654-39b3-447e-904d-8d3c7ca016b6.ExecutionDate" data-val="LandTransferDocument.Transferor.0.SigningParty.ExecutionDate">No Execution Date</span>
ends-with() is a part of XPath 2.0 and, hence, cannot be used here, see details at:
Xpath error with not() and ends-with()
Instead, use contains():
//input[contains(#id,'ExecutionDate')]
Alternatively, there is an ends-with CSS selector:
input[id$=ExecutionDate]
I am attempting to capture a text (title for different panels on a webpage) and verify the text content. I tried using the xpath to capture the text, but surprisingly on WebDriver, it fails. The same xpath seems to work perfectly fine with the Selenium RC. I wonder why WebDriver does not seem to recognize the xpath while RC does!! The code I have tried is given below:
assertEquals("Identification requests", driver.findElement(By.xpath("//span[8]")).getText());
I also tried the complete xpath:
assertEquals("Identification requests", driver.findElement(By.xpath("html/body/div[2]/div[2]/div/div/div[1]/div/div[2]/div[2]/div/div/kibana-panel/div/div[1]/div[2]/div/span[8]")).getText());
I attempted using the class variable too, but none of these seem to be returning any results. All the attempts fail with an error that the element was not found.
The html code for that portion is as shown below:
<span class="panel-text panel-title ng-binding">Identification requests</span>
The HTML code surrounding the span portion mentioned above is as shown below:
<div class="row-fluid panel-extra">
<div class="panel-extra-container">
<span class="extra row-button" ng-show="panel.editable != false && panel.removable != false">
<span class="extra row-button" ng-hide="panel.draggable == false">
<span class="row-button extra" ng-show="panel.editable != false">
<span class="row-button extra ng-scope" ng-show="task.show" ng-repeat="task in panelMeta.modals">
<span class="row-button extra ng-scope" ng-show="task.show" ng-repeat="task in panelMeta.modals">
<span class="row-button extra ng-scope" ng-show="task.show" ng-repeat="task in panelMeta.modals">
<span class="row-button extra" ng-show="panelMeta.loading == true" style="display: none;">
<span class="panel-text panel-title ng-binding">Identification requests</span>
</div>
</div>
</div>
I have doubts about why the xpath that works for RC wouldn't work for WebDriver. And how I would be able to capture the text?
I would recommend the following (use css selector):
String spanCssSelector ="span.panel-text.panel-title.ng-binding";
WebElement spanItem=driver.findElement(By.cssSelector(spanCssSelector));
String textObtained= spanItem.getText().trim();
assertEquals(textObtained,"Identification requests");
Hope this works for you.
You can use assert
assertEquals("Identification requests", driver.findElement(By.xpath("//span[starts-with(#class, 'panel-text')]")).getText());
Or
assertEquals("Identification requests", driver.findElement(By.xpath("//span[#class='panel-text panel-title ng-binding']")).getText());
Or just findElement via text() function of Xpath:
driver.findElement(By.xpath("//span[starts-with(#class, 'panel-text')][text()='Identification requests']));
Or via full class attribute value:
driver.findElement(By.xpath("//span[#class='panel-text panel-title ng-binding'][text()='Identification requests']));