Selenium : Find nested div with specific plain text using xpath - html

I need to find a certain text in a nested div that has no class or id.
This is a structure of the html.
<div class="active_row">
<div class="outcomes">
<div class="event_outcome" onclick="doSomething">
<div>Target Text</div>
</div>
</div>
</div>
I tried accessing the text directly using the example I got from here.
driver.find_elements_by_xpath("//div[contains(., 'Target Text')]")
This returns a list of elements that contain the target text but nothing happens when I run the click method on them.
What's the best way to set this query to find the text and then click on the div with event_outcome class?

To select the div with event_outcome class, you can add a predicate in your XPath to check class attribute value :
//div[contains(., 'Target Text') and #class='event_outcome']
or add a predicate to check existence of onclick attribute :
//div[contains(., 'Target Text') and #onclick]

What's the best way to set this query to find the text and then click on the div with event_outcome class?
You should try using below xpath which would returns <div class="event_outcome" onclick="doSomething"> with text Target Text which would fullfil all your needs as below :-
element = driver.find_element_by_xpath(".//div[contains(., 'Target Text') and #class='event_outcome']")
print(element.text)
element.click()
Or you can also get the same with exact match of the inner text using normalize-space() function of the xpath as below :-
element = driver.find_element_by_xpath(".//div[normalize-space()='Target Text' and #class='event_outcome']")
print(element.text)
element.click()

Related

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

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.

Getting text in <div> using Selenium

I have a question related to Selenium in Python:
I want to obtain the text content "D. New Jersey" on a webpage. In addition, the text that I want to get can be different on different pages, but it is always under "COURT:".
The HTML code is:
<div class="span4">
<strong>COURT:</strong>
D. New Jersey
</div>
The code I use now is as follows. And it doesn't work.
self.driver.get(address)
element=driver.findElement("//a[contains(#class,'span4') and contains(div/div/text(),'COURT:')]").gettext()
I have also tried the following solutions with no luck, and no Selenium exception is being thrown either:
text = self.driver.find_element_by_xpath("//div[strong[text()='COURT:']]").text
and
text = self.driver.find_element_by_xpath("//a[contains(#class,'span4') and contains(div/div/text(),'COURT:')]").text
Is there anyone who knows how to get the text from this code using Selenium?
Thanks
For Python, you can get the text as such:
text = self.driver.find_element_by_xpath("//div[strong[text()='COURT:']]").text
This uses an XPath to query on the div element, using its inner strong element to ensure we have selected the correct div. Then, we call Python's webelement.text method to get the div's text.

Watir get INNER html of <span>

I'm trying to find a navigation link by iterating through a handful of spans with class 'menu-item-text'. My goal is to compare what is inside the span tags to see if it is the right navigation control to click (there are no hard ids to go by.) My code is like this:
navlinks = #browser.spans(:class, 'menu-item')
navlinks.each do |this|
puts "'#{this.text}'"
if this.text == link_name
this.click
break
end
I know for sure I'm getting the correct elements. However, text is always an empty string. My second idea was to use .html instead of .text, but that returns something like this:
<span class="menu-item">Insights</span>
What I want is the "Insights" text inside the span, not the full html that includes the tag markup. I have also tried using this.span.text, but that did not work either.
How can I target exclusively the inner html of an element through watir's content grabbing methods?
Thanks!
Assuming you are using Watir-Webdriver v0.6.9 or later, a inner_html method has been added for getting the inner HTML.
For the span:
<span class="menu-item">Insights</span>
You could do:
#browser.span(:class => 'menu-item').inner_html
#=> "Insights"
Similarly, you could try using this method in your loop instead of .text.
Note that depending on the uniqueness of your text, you might be able to simply check if the text appears in element's (outer) html:
#browser.spans(:class => 'menu-item', :html => /#{link_name}/).click

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.

Select radio button by text

I have a radio button:
<input type="radio">test</input>
and I would like to retrieve the element by XPath using through its type and the text inside. I have tried the following:
//input[#type='radio' and text() = 'test']
but it hasn't worked out. I believe the problem is in the text() part since //input[#type='radio'] does select the element.
What is it I'm doing wrong?
In HTML/XHTML, input is an empty element; it cannot contain text. In this case, the text test actually exists as a sibling text node that directly follows the input element node, rather than as a text node within the input element node. Consequently, the closing </input> tag you have there doesn't mean anything.
Try this instead:
//input[#type='radio' and following-sibling::text() = 'test']
Or this:
//input[#type='radio' and contains(following-sibling::text(), 'test')]