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.
Related
I recently work on the project that I have to get the element from a specific website.
I want to get the text elements that are something below.
<div class="block-content">
<div class="block-heading">
<a href="https://www~~~~~~">
<i class="fa fa-map">
::before
</i>
"Text I want to get"
</a>
</div>
</div>
I have been trying to solve this for a while, but I could not find anything working fine.
I would love you if you could help me.
Thank you.
According to the information you provided the text you are looking for is inside a element so the xpath for this element is something like:
//a[contains(#href,'https://www')]
But since there is also i element inside it, getting the text from a element will give you both text contained in a itself and the text inside the i.
So you should get the text from i that is looking like just a (space) here and reduce it from the text you are receiving from the a.
In case you want to perform this action on all the a elements containing href and i element inside it you can use the following xpath:
//a[#href and ./i]
If there are more specific definitions about the elements you are looking for - the xpath I mentioned should be updated accordingly
From your comment, I understood that you would like to extract that text. So here is the code for you which would extract the text you want.
Selenium::WebDriver::Wait
.new(timeout: 60)
.until { !driver.find_element(xpath: "//i[#class='fa fa-map-marker']/..").text.empty? }
p driver.find_element(xpath: "//i[#class='fa fa-map-marker']/..").text[/(?<=before \")\w+ \w+ \w+ \w+ \w+/]
output
"Text I want to get"
I couldn't get the elements that I wanted directly, so here's what I did.
It is just that I did modify the elements with some methods though.
def seller_name
shop_info_elements = #driver.find_elements(:class_name, "block-content")
shop_info_text= shop_info_elements.first.text
shop_info_text_array = shop_info_text.lines
seller_name = shop_info_text_array.first.chomp
seller_name
end
It is not beautiful, but it can work for any other pages on the same site.
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");
I'm using the Crawler library that helps you to make some XPath expressions to get the content of the HTML tags. I'm currently reading a HTML5 content from a page and I want to retrieve a text that is not inserted in a tag in this way.
<div class="country">
<strong> USA </strong>
Some text here
</div>
So I'm trying to get this text Some text here but the crawler library allows to get just what's in a tag and not outside it.
So any alternative please.
These's the Crawler part :
$crawler = new Crawler();
$crawler->xpathSingle($xml, '//div[#class="country"]/strong/#text');
Either of these XPaths will return "Some text here" as requested:
normalize-space(substring-after(//div[#class="country"], 'USA'))
normalize-space(//div[#class="country"]/strong/following-sibling::text())
Choose based on the sort of variations you wish to accommodate.
Credit: Second example is derived from suggestion first made in comment by #Keith Hall.
Update:
As I mentioned you'll need to choose your XPath based on the variations you wish to accomodate. No sooner did I post than you encountered a variation:
<div class="country">
<strong> USA </strong>
Some text here
<i>Do not want this text</i>
</div>
You can exclude "Do not want this text" and return "Some text here" as requested using the second XPath above but just grab the first following text node:
normalize-space(//div[#class="country"]/strong/following-sibling::text()[1])
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.