How can I get the element of a-tag in the div class with selenium? - html

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.

Related

Is a wrapping span element necessary for a Schema.org telephone link?

The examples I often see for using Schema.org Microdata use a lot of extra span elements, such as the following:
<span itemprop="telephone">
<a href="tel:+18506484200">
850-648-4200
</a>
</span>
Is the extra span element really necessary or is it acceptable to place the itemprop in the a tag? Like so:
<a href="tel:+18506484200" itemprop="telephone">
850-648-4200
</a>
The latter example seems so much cleaner, but the examples I see always seem to use separate span elements.
The span element would generate a Text value, the a element would generate a URL value (details).
Schema.org’s telephone property expects a Text value:
Values expected to be one of these types
Text
Note that you don’t have to follow this advice. It is possible to use values that are not expected. There is an issue that asks to expect URL values, too: Make the telephone property more structured (not just Text).

Xpath - Retrieve text from within a span with mailto href

I have this piece of HTML code. I've already tried several xpath selectors but don't seem to be able to get the "Ask us" text from within the span with class "someClass".
<span class="someClass">Ask us</span>
Thanks in advance.
You can reach the content from the link with "/text()"
For me works this XPath snippet on your example.
/span[#class="someClass"]/a/text()
string(//span[#class="someClass"])
If you want the string() function to concatenate all child text, you
must then pass a single node instead of a node-set.

Protractor: Finding Element by Div Text

Hey I have this code in one of my div elements:
<div class="col-sm-8">Account Information: </div>
Can someone tell me how I would go about finding this element in my protractor code? Is it possible to do something like this:
expect(element(by.divText('Account Information: ')).isDisplayed()).toBe(true);
I have multiple elements with the class "col-sm-8" so I am not able to find the element by class. I was just wondering if there is any way to possibly find the element using the text in the div element? Thanks for the help!
I would recommend you to use by.cssContainingText
element(by.cssContainingText('.col-sm-8', 'Account Information'))
There is no webdriver method which would allow locating an element by its text. You could try using xpath in the following way (not tested):
element(by.xpath('//div[contains(text(), "Account Information: ")]')
keep in mind by.cssContainingText matches the element by PARTIAL text
so element(by.cssContainingText('div', 'male')) will actually match both male and female text
To solve this, use xpath with exact text match
element(by.xpath('//div[text()="male"]'))

Xpath query to grab text between different html tags

I am using R to screen scrape. I've grabbed a page and I've managed to find all the links on the page that found in a certain place on the page (anchor tags within anchor tags with a name attribute) using:
links <- xpathSApply(doc, "//a[#name]//a/#href")
Now I have grabbed got the documents from the links with Curl and I want to scrape a certain amount of text. The text seems to always be between an <p> tag (although there are other <p> tags in the text and end before the following text
</pre><hr>Back to: <a href="#TOP">
I decided to grab all the text between <p> and <a href="#TOP"> and I cant seem to nail the xpath query. So far I have got:
text <- xpathSApply(doc, '"/ //text()[preceding:://a/#href="#TOP"] and following::*//p')
Could anyone point me in the right direction? There are quite a few xpath answers on stackoverflow but they don't always explain the answer which makes it hard to edit them for my own use.
Sample HTML:
<span ID="MSGHDR-CONTENT-TYPE-H-PRE">Content-type:</b></span> <span ID="MSGHDR-CONTENT- TYPE-PRE">text/plain; charset=us-ascii</span>
</span><p>
lots and lots of text here that I want
</pre><hr>Back to: Top of message | Previous page | Main CYBCOM page<p>
The HTML is badly formed, so it was difficult for me to figure out what a well-formed instance would look like when parsed into a tree of nodes.
Something like the following might work. It assumes that all of the <p> elements declared inside of the <pre> are children of it (even though not closed in the HTML).
It looks for the text() that is a child of the <p> that does not have a child <p> and is a descendant of the <pre> that has a following-sibling who's first <a> has an href with the value "#TOP".
//body/pre[following-sibling::a[position()=1 and #href='#TOP']]//p[not(p)]/text()

Finding XPath for text in div following input

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.