How do i fetch the text from div tag and print in one line - selenium

//div[#id='Container']/div/div[2] -This line gives me the following code:
<div style="margin-left: auto;margin-right: auto;width: 25%;" xpath="1">
<span class="dxeBase_Office2010Silver field-fnt" id="Version" style="color:#000000 !important;">
Version
</span>
9.1.2.170
</div>
How do i print "Version is 9.1.2.170" in one line.
Any help will be appreciated.

To print the text Version is 9.1.2.170 you can use:
Using Java and xpath:
System.out.println(driver.findElement(By.xpath("//div/span[#class='dxeBase_Office2010Silver field-fnt' and #id='Version']/..")).getText());
Using Python and xpath:
print(driver.find_element_by_xpath("//div/span[#class='dxeBase_Office2010Silver field-fnt' and #id='Version']/..").text)

Related

Unable to get text() node before a specific span using xpath

I'm trying to find the immediate preceding text of an element.
Below is the sample HTML code that I'm working on.
<span class= "platform">
::before
<br>
name
<br>
age
<span class = "highlight">
::before
"Negine"
</span>
</span>
Came up with an XPath:
//span[#class='platform']/text()[following-sibling::span[position() = 1]]
But the XPath is returning both text nodes where as it should return only 'age'.
Have written this text using your provided HTML in the comment section.
Try using this xpath - //span[#class="platform"]/text()[3]

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

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

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