I am trying to get value of title attribute for following html code :-
<span class='overlay' title id='ab12'></span>
Actually this code is written for a tooltip. When i view source code for this HTML page , I see following
<span class='overlay' title="Test Tooltip"></span>
So basically id='ab12' in HTML code denotes Test Tooltip.
Could you tell me how can I get this text value (Test Tooltip) using Selenium-Webdriver ?
Actually your question creates some confusion, I don't think what you are saying about id='ab12', but as I'm seeing in your provided HTML class='overlay' is fixed.
(Assuming you're using Java) you should try using By.className() to locate <span> element, then use getAttribute("title") to get tooltip text as below :-
WebElement el = driver.findElement(By.className("overlay"));
String tooltip = el.getAttribute("title");
Related
I am trying to get a series of text from a web element, but unable to.
The HTML code is as follows:
<span class="versionInfo">
<span class="menu-highight">SoftFEPVis (GUI): </span> == $0
"1.6.4"
</span>
Where SoftFEPVis (GUI): and 1.6.4 are the texts which I would like to be able extract.
I am able to locate the element, and print out its class (menu-highlight), but un-able to extract SoftFEPVis (GUI): and 1.6.4.
I tried :
Version_Number = Browser.find_element(By.XPATH,'//[#id="versionDropDown"]/div/span[3]/span').getText()
and got an error:
'WebElement' object has no attribute getText.
Please help.
Instead of using .getText() you could use:
.get_attribute('innerText')
or
.get_attribute('innerHtml')
or
.text
If it helps, here is a more in-depth discussion of the topic:
Given a (python) selenium WebElement can I get the innerText?
getText() is a Selenium Java client method, where as from your code trials and the error message presumably you are using Selenium Python client.
Solution
To print the text SoftFEPVis (GUI): and 1.6.4 you can use the text attribute and you can use either of the following locator strategies:
Using css_selector and get_attribute("innerHTML"):
print(Browser.find_element(By.CSS_SELECTOR, "span.versionInfo").text)
Using xpath and text attribute:
print(Browser.find_element(By.XPATH, "//span[#class='versionInfo']").text)
Note : You have to add the following imports :
from selenium.webdriver.common.by import By
I am trying to get 'RGF Administrator' text from the html example below. The difficulty is that title can take two values: 'Application Settings' or 'Параметры приложения'. At the same time, html may contain other elements with title = 'Application options'.
Which can i use xPath for it?
<div>
<button
title="Application options">
<span>
<span>
<bdi>
RGF Administrator
</bdi>
</span>
</span>
</button>
</div>
I use selenium, but I can't determine the xPath for 'RGF Administrator'.
xpath = "//*[#title='Параметры приложения' or #title='Application options']"
won't work since at the same time, html may contain other elements with title = 'Application options'.
Try this xpath expression on your actual xml and see if it works:
//div/button[#title = ('Параметры приложения', 'Application options')]//bdi/text()
If you are using selenium, then you need to use the get_attribute(string) method. So for example, when you find the elements xpath:
element = driver.find_element_by_xpath("xpath")
and then do
element.get_attribute("title")
to get Application options
How many bdi containers do you have on the webpage? If there is only 1, you can literally just use
//bdi
And you don't need any pathing.
I am storing this string within a ResourceFile:
Please select a #Html.ActionLink("Personal", "PersonalAccount", "Help"),
or a #Html.ActionLink("Corporate", "BusinessAccount", "Help") account.
Now I want to put this content on a Span element inside the Title attribute.
I tried this without success :
<span class="tooltip-icon tooltip"
title="#Html.Raw(HttpUtility.HtmlDecode(ToolTips.AccountType))">
</span>
The issue is that I am seeing the regular text, not the content encoded.
Razor is not going to double parse your page, It parses your page and renders the content of your resource which is a text having # in it(Please select a #Html.ActionLink("Personal", "PersonalAccount", "Help")). It doesn't care what is in the resource.
Any how, you can't do it this way and you shouldn't, the only culture specific part of your code is the link's text which you should just put that part in your resource file not whole of that line.
Using VahidND suggestion I was able to do it without Razor inside the Resource file.
So I actually ended with this :
<span class="tooltip-icon tooltip"
title="#String.Format(ToolTips.PTAccountTypeRadio1,
Html.ActionLink(ToolTips.PTAccountTypeRadio2, "PersonalAccount", "Help"),
Html.ActionLink(ToolTips.PTAccountTypeRadio3, "BusinessAccount", "Help"))"
</span>
I got an issue reading XPath. Need some help/advise from experts.
Part of my HTML is below:
<div class = "input required_field">
<div class="rounded_corner_error">
<input id="FnameInput" class="ideField" type="text" value="" name="first_name>
<div class ="help-tooltip">LOGIN BACK TO MAIN</div>
<div class="error-tooltip">
I need to find the XPath of the text message (LOGIN BACK TO MAIN)
Using Firebug I find the XPath
("//html/body/div/div[5]/div/div/form/fieldset/div/div[2]/div[2]/div/div");
But using above XPath I can read only class = help-tooltip but I need to read LOGIN BACK TO MAIN.
Try adding /text() on the end of the xpath you have.
It does not really look like your XPath matches your XHTML element.
You should try something simpler and more generic, such as:
//div[#class="help-tooltip"]/text()
See Selecting a css class with xpath.
I would use:
# Selecting the div element
//input[#id="FnameInput"]/following-sibling::div[#class="help-tooltip"]
# Selecting the text content of the div
//input[#id="FnameInput"]/following-sibling::div[#class="help-tooltip"]/text()
…since a syntactically-valid HTML document will have a unique id attribute, and as such that's a pretty strong anchor point.
Note that the latter expression will select the text node, not the text string content of that node; you need to extract the value of the text node if you want the string. How you do that depends on what tools you are using:
In JavaScript/DOM that would be the .nodeValue property of the text node.
For Nokogiri that would be the .content method.
…but I have no idea what technology you are using your XPath with.
I have some HTML like this:
<h4 class="box_header clearfix">
<span>
<a rel="dialog" href="http://www.google.com/?q=word">Search</a>
</span>
<small>
<span>
<a rel="dialog" href="http://www.google.com/?q=word">Search</a>
</span>
</h4>
I am trying to get the href here in Java using Selenium. I have tried the following:
selenium.getText("xpath=/descendant::h4[#class='box_header clearfix']/");
selenium.getAttribute("xpath=/descendant::h4[#class='box_header clearfix']/");
But none of these work. It keeps complaining that my xpath is invalid. Can someone tell me what mistake I am doing?
You should use getAttribute to get the href of the link. Your XPath needs a reference to the final node, plus the required attribute. The following should work:
selenium.getAttribute("xpath=/descendant::h4[#class='box_header clearfix']/a#href");
You could also modify your XPath so that it's a bit more flexible to change, or even use CSS to locate the element:
//modified xpath
selenium.getAttribute("//h4[contains(#class,'box_header')]/a#href");
//css locator
selenium.getAttribute("css=.box_header a#href");
I had similar problems with Selenium and xpath in the past and couldn't really resolve it (other than changing the expression). Just to be sure I suggest trying your xpath expressions with the XPath Checker addon for firefox.