Selenium WebDriver - how to verify multiple lines text - webdriver

I have an html page with following contents:
<center>
This is Login page.
<br>
Please click below link to Login.
<br>
Login
</center>
How can I verify all the static text above using one webdriver command?
I tried these but none of it works :(
driver.findElement(By.xpath("//*[contains(.,'This is Login page.<br>Please click below link to Login')]"))
driver.findElement(By.xpath("//*[contains(.,'This is Login page.\nPlease click below link to Login')]"))
Anyone know how to do it?

You can do:
webDriver.findElement(By.id("<ID of center tag>")).getText().contains("This is Login page.Please click below link to Login.Login");
Feel free to use a locator of your choice instead of By.id.
Basically getText() of any WebElement returns the textual content of the node, stripped of all the HTML tags and comments.

This is more a limitation of XPath than Selenium.
I do not see why you are using the global XPath selector there, a better XPath selector would be:
By.XPath("//center");
If it's the only "center" tag on the page, or even:
By.XPath("//center[contains(text(), 'This is a Login page'")
The /r and /n characters will be retained if you do it through code instead of blindly searching for it in XPath.

How to find the texts if the texts has space inbetween them.
<br>
<br/>
Please click below link to Login.
<br>
Login
</center>

You can do with "\n"
return getText(supportText).contains("Information:" + "\n" +
"This page is for use by the TECHNICAL TEAM only. Following links will be active, only if you have access (subscription) to those modules.");

Related

I am not getting to click on href link

I am trying to click on Href link but with no success.
This is the HTML of the page I am trying to click:
And here is the code that I tryed to use.
Welcome Page Should Be Open
Location Should Be ${WELCOME URL}
Title Should Be OrangeHRM
Click Link id=welcome
Wait Until Element Is Visible id=welcome-menu 8
#Click Link href=https://opensource-demo.orangehrmlive.com/index.php/auth/logout
Click Link xpath://a{test()='logout'}
I tried with "href" and "xpath", but both give the error:
No keyword with name XXXXXX found.
Can you please tell me what I am doing wrong?
The error is because you only have a single space between Click Link and the xpath. You need two or more spaces to separate a keyword from its arguments.
Also, xpath://a{test()='logout'} is not a valid xpath expression. You need to change the curly braces to square brackets, and change test to text. You also need to change logout to Logout.
xpath://a[text()='Logout']
For Click Link, you can also just specify the link text or href, for example:
Click Link Logout

Chrome autofills username into random text input

This is happening on an asp.net webforms application, using Chrome Version 72.0.3626.109 (Official Build) (64-bit).
The site is password-protected. The user logs in with a username and password. After a successful login the user is redirected to the "Loan Search" page. The Loan Search page contains a handful of text inputs.
The problem is Chrome will autofill my username into one of the text inputs (see image). "tregan" is the username I entered into the login page.
Chrome always selects this particular text input to autofill the username ("Contact Mailing Address"). This is happening to myself and several dozen other users of our web site.
Any idea why Chrome is doing this autofill, and is there anything I can do to prevent it? I cleared my Chrome autofill cache, but that did not fix the problem.
The answer is to add an invisible text input to the asp.net form called "username".
Several years ago we were having the same problem with a different input. The answer was to add an invisible input of type "password", as explained in this SO answer, scroll down to the phrase "It is so simple and tricky...":
Disabling Chrome Autofill
Below is the complete fix, I added these two elements inside the form element in our site's master page. Per #Jeff_Mergler's comment below, put these inputs at the top of your form tag:
<input type="text" id="username" style="width:0;height:0;visibility:hidden;position:absolute;left:0;top:0" />
<input type="password" style="width:0;height:0;visibility:hidden;position:absolute;left:0;top:0" />
Some more ways to try to workaround this:
Add autocomplete="off" to the <form> and/or to the <input>
Change the field's name/id
to something that does not have "name" or "user" in it
If it is not already inside <form> wrap the element with empty <form> tag
Randomize the name attribute of the input, or use data-name instead of name. You'll have to change the code that process the data accordingly.
Also I think it'll help to report this issue to Google via ⋮→Help→Report an issue (or Alt+Shift+I) to encourage them to fix these issues.
I was facing the same issue, i found a fix by wrapping my div inside a form tag and added a property autocomplete="off" in the form tag.
......
.....
Same here. This needs to be resolved by chrome. This is just dumb to have to add tags around textboxes. Also, quick tip to anyone needing to add form tag without having to re-do your CSS. Add "display:contents;" to the form. It will act as if its not even there.

How do i find the css selector for a Text message

I need to find the css selector for the text message : Congratulations! displayed after the registration form is filled successfully by a new user. I am using Selenium Web driver to automate the registration process. I have to assert "Congratulations,Your email has been verified" text. Below is the HTML code for the same :
<div class="d-title">
<h1>Congratulations!</h1>
<strong>Your e-mail address has been verified.</strong>
Thanks,
Java Beginner
You can't match on text in CSS. You can do it in XPath, but CSS does not have a selector that matches a text node. In XPath, it would be simple: //div/h1[. = 'Congratulations!'].
The below code will wait for 10 seconds and check if the selector contains the provided text, if it doesnt contain, an exception will be thrown...If you want to handle the exception, just surround it with try and catch
new WebDriverWait(driver,10).until(ExpectedConditions.textToBePresentInElement(By.cssSelector("div.d-title>h1"),"Congratulations,Your email has been verified"));
Since, Congratulations! and Your e-mail address has been verified. are two different texts, you will have to user two different css selectors for them:
For Congratulations!, use div.d-title > h1
For Your e-mail address has been verified., use div.d-title > strong

why are the first characters in my asp login labels in italics?

I can't work out why the first characters of all the labels on my asp login forms are showing in italics:
The code looks like this:
<asp:login DestinationPageUrl="blah.aspx" runat="server" usernamelabeltext="Email Address" ></asp:login>
When I inspect it using firebug it shows that the first characters are being enclosed in tags, like so:
<label for="ctl00_ctl00_ctl00_ContentPlaceHolderDefault_wwContentArea_ctl03_UserName">
<em>E</em>
mail Address
</label>
...
<label for="ctl00_ctl00_ctl00_ContentPlaceHolderDefault_wwContentArea_ctl03_Password">
<em>P</em>
assword:
</label>
Does anyone know what may be causing this? I thought it might have been something to do with access keys (if I press alt+e then it focuses to the email text box) but I cant work out how to stop this.
For me, it was because I was using the CSS Friendly Control Adapters. Since my primary motivation for installing them was for the menu control adapter, I've disabled the login control adapter.
Open the CSSFriendAdapters.browser in the App_Browsers folder.
Comment out the LoginAdapter, like so:
<!--<adapter controlType="System.Web.UI.WebControls.Login" adapterType="CSSFriendly.LoginAdapter" />-->
There's probably a way to get the advantages of the CSS adapter without the italics.
I can't answer why it was doing it, but I've managed to work around it.
With the line of code still as follows:
<asp:login DestinationPageUrl="/schools/what-works/whatworkssearch.aspx" runat="server" usernamelabeltext="Email Address"></asp:login>
This is what the control looked like in designer view:
Although there are no italics shown, it was being rendered with the first characters in italics as I described in my question. To get around this I converted the control to a template like so:
This created a template that shows the labels for the login fields and therefore I could remove the italics tags.
Although this doesn't answer why it was behaving like this for the single line of code, it shows how to get around it.
You may want to check the AccessKey property of the login control and the individual controls pasted within the login section such as Label, TextBox and buttons. By removing the AccessKey value should fix the issue.

Input Validation When Using a Rich Text Editor

I have an ASP.NET MVC application and I'm using CKEditor for text entry. I have turned off input validation so the HTML created from CKEditor can be passed into the controller action. I am then showing the entered HTML on a web page.
I only have certain buttons on CKEditor enabled, but obviously someone could send whatever text they want down. I want to be able to show the HTML on the page after the user has entered it. How can I validate the input, but still be able to show the few things that are enabled in the editor?
So basically I want to sanitize everything except for a few key things like bold, italics, lists and links. This needs to be done server side.
How about AntiXSS?
See my full answer here from similar question:
I have found that replacing the angel
brackets with encoded angel brackets
solves most problems
You could create a "whitelist" of sorts for the html tags you'd like to allow. You could start by HTML encoding the whole thing. Then, replace a series of "allowed" sequences, such as:
"<strong>" and "</strong>" back to "<strong>" and "</strong>"
"<em>" and "</em>" back to "<em>" and "</em>"
"<li>" and "</li>" back to ... etc. etc.
For things like the A tag, you could resort to a regular expression (since you'd want the href attribute to be allowed too). You would still want to be careful about XSS; someone else already recommended AntiXSS.
Sample Regexp to replace the A tags:
<a href="([^"]+)">
Then replace as
<a href="$1">
Good luck!

Resources