How to write xpath in selenium webdriver for below HTML expressions? - selenium

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')]

Related

Not able to identify custom checkbox element/Need relative xpath

I need relative xpath of the span element
<span class="icon"></span>
in selenium web driver.
<span class="custom-checkbox">
<input id="personalization1" name="personalization-terms-check" class="personalization-terms-check required" value="accept" type="checkbox">
<label for="personalization1">
<span class="icon"></span>
<span class="order-ready-text">yes! I double-checked all personalization entered above and i'm ready to order</span>
</label>
</span>
need relative xpath of the span element
<span class="icon">
in above html and I am getting relative path:
//*[#id="main"]/div[3]/div[4]/div[2]/div/span/label/span[1].
Please help me to get an relative xpath or another way to make an click on the span
NOTE:
<span class="icon">
It can be multiple so I need unique relative xpath.
You can wait for the element visibility before performing any action like below.
WebElement element=driver.findElement(By.xpath("//*[#name='personalization-terms-check']/following-sibling::label/span"));
WebDriverWait wait = new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.visibilityOf(element));
element.click();
and you can use different xpath or id to locate the element. the above xpath map be unique if only one personalization term checkbox on the page.
Can you try with this xpath?
"//label[#for='personalization1']/span[1]"
Hope this helps. Thanks.
Try this:
Try this XPath -
//input[#id='personalization1']/label/span
Try this xpath to click on checkbox:
.//input[#id='personalization1']
or you can use this xpath
.//span[contains(text(),'yes! I dou')]

Need help Identifying fields with dynamic names using Selenium IDE

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]

Any suggesstions to locate the below elements in Selenium

Example HTML below. I want to locate the elements which contains the text Person Attributes and Age.
<div id="ext-gen210" class="x-tool x-tool-toggle"></div>
<span id="ext-gen214" class="x-panel-header-text">Person Attributes</span>
</div>
<div id="ext-comp-1071" class=" DDView ListView" style="height: auto;">
<p class="dragItem "><i class="icon-Gen"></i>Age</p>
</div>
Note: I am looking for a solution without using xpath or id or className as they might change with every new release of my website.
I tried to locate them using
'name' --> By.name("Person Attributes") and By.name("Age") but both failed.
By.name would check for the name attribute. What you need is to check the text of an element using By.xpath:
By.xpath('//div[span/text() = "Person Attributes"]')
Or, you can also check that an id element starts with ext-gen:
By.xpath('//div[starts-with(#id, "ext-gen")]')

Capturing a text/title on the webpage using Selenium WebDriver

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']));

want to gettext from span

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 :)