How can i get the text or need path to get exact text inside the tag - selenium

I have html like the below
<label id ="someid">
some text in label <span> count(10) </span></label>
I need to get text inside the label.
If I use driver.find_element_by_id('someid').text();
I will get as some text in label count(10)
But I don't want to get count(10).
Is there any way to write the path to get exact text available in label, not elements inside label

This XPath will get first text node under label element :
//label[#id ='someid']/text()[1]
Given sample xml in this question, above XPath will return :
some text in label

Related

1)How to get the xpath to get a text without a tag on the same level of other tags

I am trying to get the XPath for "workbasket_598F".However when I giving it //div[contains(text(),'Workbasket Name')]//following::tr[1]/td[1]//div/script it's not detecting the text, it's detecting the
enter image description here
You can get that string with //div[#class="oflowDivM "]/text(), which basically will return the text - located inside <div class="oflowDivM "> - that is not wrapped in any tags.

Using Selenium keywords to find if any text is in the Element

I know that there is the following keyword to find if particular text is in the Element:
Element Should Contain <xpath> <text>
But I just want to find if the Element contains any text.
Do I have to use a different keyword or can I use some kind of wildcard for any text?
i.e. Will this be possible?
Element Should Contain <xpath> *
Define what should satisfy "any text" - will an empty string also qualify? If so, you can do Page Should Contain Element - it will pass if it's in the page, regardless what its text is.
You could also get the text, and validate it any way you like:
${txt} Get Text <xpath>
Should Not Be Empty ${txt} # etc

Selenium XPath find element where second text child element contains certain text (use contains on array item)

The page contains a multi-select dropdown (similar to the one below)
The html code looks like the below:
<div class="button-and-dropdown-div>
<button class="Multi-Select-Button">multi-select button</button>
<div class="dropdown-containing-options>
<label class="dropdown-item">
<input class="checkbox">
"
Name
"
</label>
<label class="dropdown-item">
<input class="checkbox">
"
Address
"
</label>
</div>
After testing in firefox developer tools, I was finally able to figure out the xPath needed in order to get the text for a certain label ...
The below XPath statement will return the the text "Phone"
$x("(//label[#class='dropdown-item'])[4]/text()[2]")
The label contains multiple text items (although it looks like there is just one text object when looking at the UI) in the label element. There are actually two text elements within each label element. The first is always empty, the second contains the actual text (as shown in the below image when observing the element through the Firefox developer tool's console window):
Question:
How do I modify the XPath shown above in order to use in Selenium's FindElement?
Driver.FindElement(By.XPath("?"));
I know how to use the contains tool, but apparently not with more complex XPath statements. I was pretty sure one of the below would work but they did not (develop tool complain of a syntax error):
$x("(//label[#class='dropdown-item' and text()[2][contains(., 'Name')]]")
$x("(//label[#class='dropdown-item' and contains(text()[2], 'Name')]")
I am using the 'contains' in order to avoid white-space conflicts.
Additional for learning purposes (good for XPath debugging):
just in case anyone comes across this who is new to XPath, I wanted to show what the data structure of these label objects looked like. You can explore the data structure of objects within your webpage by using the Firefox Console window within the developer tools (F12). As you can see, the label element contains three sub-items; text which is empty, then the inpput checkbox, then some more text which has the actual text in it (not ideal). In the picture below, you can see the part of the webpage that corresponds to the label data structure.
If you are looking to find the element that contains "Name" given the HTML above, you can use
//label[#class='dropdown-item'][contains(.,'Name')]
So finally got it to work. The Firefox developer environment was correct when it stated there was a syntax problem with the XPath strings.
The following XPath string finally returned the desired result:
$x("//label[#class='dropdown-item' and contains(text()[2], 'Name')]")

How to add xml data(containing tags) in extent-report.html

I'm trying to add xml string into my extent-report. It is instead treating it as an html tag and displaying in the dom instead of UI.
I tried adding chars before and after xml string and then it is printing the chars but not the xml data
Reporter.addStepLog("text is <"+"<response><abc value=10></abc></response>"+">"
I want the report to show o/p as-> text is <<response><abc value=10></abc></response>>
but i am getting-> text is <>
P.S.:If you see the console you will get what i am trying to explain !
you may try to use following html element :
Reporter.addStepLog("text is <textarea rows='20' cols='40' style='border:none;'>"+"<response><abc value=10></abc></response>"+"</textarea>");
For information, I found this trick on this site :
Display XML content in HTML page

inline image alt attribute and getText()

I got something like this:
<div id=soandso>
Having a
<img src="cat.gif" alt="Meow">
always helps
</div>
I have no problems finding the div element, when I invoke getText() I would wish to receive "Having a Meow always helps", but instead I get the text without the alt (Meow) description.
I agree this is the expected behaviour, but this is not what I need.
How can I preferably inline the alt text or at least get the text chunks in sequence with the inline image to verify the proper placement of the image inside the text?
selenium-webdriver works that way in both scenarios,
considers "alt" as attribute of image tag and not as element text for Div.
Considers "Having a always helps" as innerText of Div element.
i would suggest....to extract the Div text and img (alt) text separately. if text follows a pattern, try to identify sequence after or before which image attribute will be displayed.
WebElement Div_elem=Driver.findElement(By.id("soandso"));
String Div_text = Div_elem.getText();
String img_text=Div_elem.findElement(By.tagName("img")).getAttribute("alt");
would be glad if it helps
I found a solution here: How to get text of an element in Selenium WebDriver (via the Python api) without including child element text?
His suggested solution:
def get_text_excluding_children(driver, element):
return driver.execute_script("""
return jQuery(arguments[0]).contents().filter(function() {
return this.nodeType == Node.TEXT_NODE;
}).text();
""", element)
The element passed to the function can be something obtained from the find_element...() methods (i.e. it can be a WebElement object).
basically suggests that you use jQuery to get the text within the div.