Selenium: test if element contains some text - html

With Selenium IDE, how can I test if an element's inner text contains a specific string? For example:
<p id="fred">abcde</p>
'id=fred' contains "bcd" = true)

The Selenium-IDE documentation is helpful in this situation.
The command you are looking for is assertText, the locator would be id=fred and the text for example *bcd*.

It can be done with a simple wildcard:
verifyText
id="fred"
*bcd*
See selenium IDE Doc

You can also use:
assertElementPresent
css=p#fred:contains('bcd')

A solution with XPath:
Command: verify element present
Target: xpath=//div[#id='fred' and contains(.,'bcd')]

Are you able to use jQuery if so try something like
$("p#fred:contains('bcd')").css("text-decoration", "underline");

It seems regular expressions might work:
"The simplest character set is a character. The regular expression "the" contains three
character sets: "t," "h" and "e". It will match any line with the string "the" inside it.
This would also match the word "other". "
(From site: http://www.grymoire.com/Unix/Regular.html)
If you are using visual studio there is functionality for evaluating strings with regular expressions of ALL kinds (not just contains):
using System.Text.RegularExpressions;
Regex.IsMatch("YourInnerText", #"^[a-zA-Z]+$");
The expression I posted will check if the string contains ONLY letters.
Your regular expression would then according to my link be "bcd" or some string you construct at runtime. Or:
Regex.IsMatch("YourInnerText", #"bcd");
(Something like that anyway)
Hope it helped.

You can use the command assertTextPresent or verifyText

Related

Xpath: Get Text After Element With Containing Text

I am looking for a way to get text which is not inside an HTML element:
<div class="col-sm-4">
<strong>Handelnde Personen:</strong><br><br>
<strong>Geschäftsführer</strong><br>
Mr John Doe<br>
Privatperson<br>
.....<br>
<br>
I want to get "Mr John Doe".
The only way I see is looking for a strong element which contains "Geschäftsführer" and then look for the following text.
My idea so far:
//strong[contains(text(), 'Gesch')]/br/../text()
... I simply can't make it work.
Also, is there a "wildcard" for strings? That I could use
*esch*ftsf*hr*
for "Geschäftsführer"?
I highly appreciate your help, thanks!
Try
//strong[starts-with(., 'Gesch')]/following-sibling::text()[1]
As for wildcard matching, with XPath 2.0 you use regular expressions:
//strong[matches(., '.*esch.*ftsf.*hr.*')]
With XPath 3.0 you could also use the Unicode collation algorithm
//strong[compare(., 'Geschäftsführer',
'http://www.w3.org/2013/collation/UCA?strength=primary') = 0]
(strength=primary ignores case and accents)
But to get anything more advanced than XPath 1.0 in the browser, you would need to deploy Saxon-JS.
Another option with 1.0 is to use translate() to remove case and umlauts:
//strong[translate(., 'ABCD..XYZÄÖÜäöüß', 'abcd..xyzaouaous') = 'geschaftsfuhrer']
Note, in all these examples I have used "." rather than "text()" to get the string value of an element - this is recommended practice.

How do you replace part of a string of an apache NIFI attribute?

Can you update attribute using replace text of an attribute's value?
I have an attribute - ${a} which has a place holder called "PLACEHOLDER". How do I replace each occurrence of the word "PLACEHOLDER" in attribute ${a} with the value of attribute ${b}?
I have tried using replace text processor - using both Regex Replace and Literal Replace replacement strategies without any luck.
See below my example.
1. Generate flow file.
Replace text attempt 1 - literal replace strategy
Replace text attempt 2 - regex replace strategy
None works as attribute value is unchanged.
How can I achieve this? If not this processor, which one please? How? Thanks in advance!
Haha...Such a bummer. Found the answer. Should have kept digging through the documentation i guess.
Use update attribute processor then set attribute to ${a:replace('PLACEHOLDER', ${b})}
Hope this helps someone who's not searched documentation properly like me in future :-)

How to replace a specific line of HTML code with Regular Expression In Dreamweaver?

I want to replace <whatever>Some Title</whatever> with <something>Some Title</something> using the Find and Replace tool inside of Dreamweaver. How do I perform?
Not a Dreamweaver user, but this simple approach works in my editor (Emacs):
Replace:
<whatever>\(.*\)</whatever>
With:
<something>\1</something>
This is a pretty straightforward approach but it may fall short of your needs. Do some or all of your <whatever> element pairs occupy more than one line of text? Or do you have more than one <whatever> pair on a single line?
i guess what you want is to change all your <whatever> tag with an <something> tag whitout changing your text, right?
If it is so, you want to use find and replace with regular expression. Find (in source code) <whatever>(.*)</whatever> and replace it with <something>$1</something>. The $1 is used as a variable for anything fits the (.*) part DW finds for each instance.
For example, you you want to comment all instances of an
document.NAMEOFANYFORMONTHEPAGE.WHATEVERNAME.focus();
in a JavaScript file, you would use find:
document\.(.*)\.focus\(\);
and replace it with:
// document.$1.focus();
Don't forget to escape special characters and, please, try a few instances before using Replace All

Reg Exp To Remove 'onclick' from HTML elements (Notepad++)

I have a big html file (87000+ lines) and want to delete all occurrences of onclick from all elements. Two examples of what I want to catch are:
1. onclick="fnSelectNode('name2',2, true);"
2. onclick="fnExpandNode('img3','node3','/include/format_DB.asp?Index2=3');"
The problem is that both function names and parameters passed to them are not the same. So I need a Regular expression that matches onclick= + anything + );"
And I need one that works in Notepad++
Thanks for helping ;-)
Not familiar with notepad++, but what I use in vim is:
onclick="[^"]+"
Of course this depends on there being double quotes around the onclick in every case...
This regular expression will fail if you have a " or ' character included within quotes escaped by a \. Other than that, this should do it.
(onclick="[^"]+")|(onclick='[^"]+')
onclick="[^"]+" works for me, for that 2 strings.
If you want to go with a regex:
/onclick=".*?"/
You could also use something which is DOM-aware, such as a HTML/XML parser, or even just load up jQuery:
$("[onclick]").removeAttr("onclick");
... and then copy the body HTML into a new file.
Could
onclick=\".+;\"
Work?
onclick=\".*\);\"
This regex should do the trick.
(\s+onclick=(?:"[^"]+")|(?:'[^']+'))
Open your file on dreamweaver, choose edit from the toolbar, select find and replace,
put onclick="[^"]+" in find field and keep replace blank
this will do the whole thing.
Enjoy

Regex to Parse Hyperlinks and Descriptions

C#: What is a good Regex to parse hyperlinks and their description?
Please consider case insensitivity, white-space and use of single quotes (instead of double quotes) around the HREF tag.
Please also consider obtaining hyperlinks which have other tags within the <a> tags such as <b> and <i>.
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
As long as there are no nested tags (and no line breaks), the following variant works well:
<a\s+href=(?:"([^"]+)"|'([^']+)').*?>(.*?)</a>
As soon as nested tags come into play, regular expressions are unfit for parsing. However, you can still use them by applying more advanced features of modern interpreters (depending on your regex machine). E.g. .NET regular expressions use a stack; I found this:
(?:<a.*?href=[""'](?<url>.*?)[""'].*?>)(?<name>(?><a[^<]*>(?<DEPTH>)|</a>(?<-DEPTH>)|.)+)(?(DEPTH)(?!))(?:</a>)
Source: http://weblogs.asp.net/scottcate/archive/2004/12/13/281955.aspx
See this example from StackOverflow: Regular expression for parsing links from a webpage?
Using The HTML Agility Pack you can parse the html, and extract details using the semantics of the HTML, instead of a broken regex.
I found this but apparently these guys had some problems with it.
Edit: (It works!)
I have now done my own testing and found that it works, I don't know C# so I can't give you a C# answer but I do know PHP and here's the matches array I got back from running it on this:
Text
array(3) { [0]=> string(52) "Text" [1]=> string(15) "pages/index.php" [2]=> string(4) "Text" }
I have a regex that handles most cases, though I believe it does match HTML within a multiline comment.
It's written using the .NET syntax, but should be easily translatable.
Just going to throw this snippet out there now that I have it working..this is a less greedy version of one suggested earlier. The original wouldnt work if the input had multiple hyperlinks. This code below will allow you to loop through all the hyperlinks:
static Regex rHref = new Regex(#"<a.*?href=[""'](?<url>[^""^']+[.]*?)[""'].*?>(?<keywords>[^<]+[.]*?)</a>", RegexOptions.IgnoreCase | RegexOptions.Compiled);
public void ParseHyperlinks(string html)
{
MatchCollection mcHref = rHref.Matches(html);
foreach (Match m in mcHref)
AddKeywordLink(m.Groups["keywords"].Value, m.Groups["url"].Value);
}
Here is a regular expression that will match the balanced tags.
(?:""'[""'].*?>)(?(?>(?)|(?<-DEPTH>)|.)+)(?(DEPTH)(?!))(?:)