Java - Selenium : <html> containing <html> problematic - java

I'm testing a web page with Java and Selenium API. The web page is done like this :
<html>
<head>
</head>
<frameset name="f1">
<html>
<frameset name="f2">
</frameset>
</html>
</frameset>
</html>
Using internetExplorerDriver.findElement(By.xpath("//frame[#name='f1']")) the good element is retrieved. But when I do internetExplorerDriver.findElement(By.xpath("//frame[#name='f2']")) it tells that element can't be found. When I look to children of f1 with f1.findElements(By.xpath("*")).size(); I obtain a 0, no children are found.
How this could be solved ? Does the web page has to be changed and remove the sub html markup ? Thanks in advance.
Regards,
Jean Ducrot

You need to switch to the frame before finding the element within the context OF the frame.
internetExplorerDriver.switchTo().frame("f1");
internetExplorerDriver.findElement(By.xpath("//frame[#name='f2']"))

Related

Replace custom tags in html with Java

is there a library on Java to help me to achieve custom tags replacement in html
like for example here is a simple template :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div>
<p>$welcome_title</p>
<p>$email_body</p>
<p>$footer_text</p>
</div>
</body>
</html>
Can i replace this custom tags ($welcome_title,$email_body,$footer_text) with values from java ?
The idea is to have template with tags which can be replaced at runtime with values from java objects :)
Also maybe (if there is a library) to generate straight away from html an PDF doc
Thanks :)
In Java world you can use https://www.thymeleaf.org/ or https://freemarker.apache.org/

Muliple body nodes present in HTML dom, so Selenium fails to find the element in dom

I'm trying to automate an application and that has multiple HTML head and body tags present. Below is the sample provided. I tried all possibility using xpath, id , class etc. It doesn't work for this application alone as it as embedded HTML page inside the DOM. I guess, JavaScript loads the a new HTML page inside the page.
Even-though the XPath works in Chrome browser, when I put it in script and run, it throws an exception:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: //*[text()='Continue'].
How to tackle this problem?
HTML DOM Sample:
<html class="UShellFullHeight">
<head>
<style id="antiClickjackStyle" type="text/css">
body {
display : none !important;
}
</style>
</head>
<body class="UiBody UShellFullHeight" role="application">
<div id="canvas" class="UShellFullHeight"></div>
#document
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html id="home" lang="EN">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
.
.
.
<span id="WD8A-cnt" class="urNoUserSelect lsButton--contentlsControl--centeraligned urBtnCnt" style="pointer-events:none;">
<span class="lsButton__text " id="WD8A-caption" style="white-space:nowrap;">Continue</span>
</span>
</body>
</html>
</body>
</html>
try with tag:
//span[text()='Continue']
or
the best solution for this example is to use id, this element has id:
driver.findElement(By.id("WD8A-caption"));
Or this xpath which is the same
//span[#id='WD8A-caption']
Try with xpath by creating manually
Create Xpath Manually or
the element has id and also class name driver.findElement(By.className("lsButton__text "))
Thanks a lot for your time guys. I got the answer myself.
Answer is i need to switch the frame and do the operation on elements.
to switch frame.
driver.switchTo().frame("id");

How to include new page on jsp page in every 10 sec?

Hi i am trying to include a new jsp page on my jsp page in every 10 sec how can i achieve this
i am able to include page but not including according to time
Here is my code
<%# page language="java" contentType="text/html;charset=UTF-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html>
<head>
<title>Untitled Document</title>
section
{height:70%; background-color:blue; display:block; overflow:auto;}
section .push {height:500px;}
</style>
</head>
<body>
<form action="F1.jsp" method="post">
<%# include file="F2.jsp" %>
<footer>footer</footer>
</form>
</body>
</html>
How can i achieve this
Thanks in advance
If you want to include more content or refresh a section of the page you need to use browser side JavaScript to pull new content from the server.
Once you display something using JSP, that is final. It cannot change unless you provide the mechanisms to change. You can set a refresh rate of your JSP to auto refresh and display each time a new content.
Or you can use AJAX.
How to do it using ajax (I prefer the JQuery version) can be found here at the answer with 5 upvotes
You can set interval in Javascript.
syntax:
setInterval(function(){
// code //
},10000);
In the code you can use Ajax calls which pool the content of JSP from the server.

How to parse a webpage that includes Javascript? [duplicate]

This question already has answers here:
Parse JavaScript with jsoup
(2 answers)
Closed 9 years ago.
I've got a webpage that creates a table using Javascript. Right now I'm using JSoup in my Java project to parse the webpage. By the way JSoup isn't able to run Javascript so the table isn't generated and the source of the webpage is incomplete.
How can I include the HTML code created by that script in order to parse its content using JSoup? Can you provide a simple example? Thank you!
Webpage example:
<!doctype html>
<html>
<head>
<title>A blank HTML5 page</title>
<meta charset="utf-8" />
</head>
<body>
<script>
var table = document.createElement("table");
var tr = document.createElement("tr");
table.appendChild(tr);
document.body.appendChild(table);
</script>
<p>First paragraph</p>
</body>
</html>
The output should be:
<!DOCTYPE html>
<html>
<head>
<title>
A blank HTML5 page
</title>
<meta charset="utf-8"></meta>
</head>
<body>
<script>
var table = document.createElement("table");
var tr = document.createElement("tr");
table.appendChild(tr);
document.body.appendChild(table);
</script>
<table>
<tr></tr>
</table>
<p>
First paragraph
</p>
</body>
</html>
By the way, JSoup doesn't include the table tag as it isn't able to execute Javascript. How can I achieve this?
First possibility
You have some options outside Jsoup, i.e. employing a "real" browser and interact with it. An excellent choice for this would be selenium webdriver. With selenium you can use different browsers as back end, and maybe in your case the very lightweight htmlUnit would do already. If more complicated JavaScript is called there is often no other choice then running a full browser. Luckily, phantomjs is out there and its footprint is not too bad (headless and all).
Second possibility
Another approach could be that you grab the javascript source with JSoup and start a JavaScript interpreter within Java. For that you could use Rhino. However, if you go that path you might as well use HtmlUnit directly, which is probably a bit less bulky.

Play! framework. template "include"

I'm planning my website structure as following:
header.scala.html
XXX
footer.scala.html
now, instead of "xxx" there should be a specific page (i.e. "UsersView.scala.html").
what I need is to include (like with well-known languages) the source of the footer and the
header into the the middle page's code.
so my questions are:
How do you include a page in another with scala templating?
Do you think it's a good paradigm for Play! framework based website?
Just call another template like a method. If you want to include footer.scala.html:
#footer()
A common pattern is to create a template that contains the boilerplate, and takes a parameter of type HTML. Let's say:
main.scala.html
#(content: HTML)
#header
// boilerplate
#content
// more boilerplate
#footer
In fact, you don't really need to separate out header and footer with this approach.
Your UsersView.scala.html then looks like this:
#main {
// all your users page html here.
}
You're wrapping the UsersView with main by passing it in as a parameter.
You can see examples of this in the samples
My usual main template is a little more involved and looks roughly like this:
#(title: String)(headInsert: Html = Html.empty)(content: Html)(implicit user: Option[User] = None)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>#title</title>
// bootstrap stuff here
#headInsert
</head>
<body>
#menu(user)
<div id="mainContainer" class="container">
#content
</div>
</body>
</html>
This way a template can pass in a head insert and title, and make a user available, as well as content of course.
Play provide a very convenient way to help implement that!
Layout part from official docs:
First we have a base.html (that's we call in django -_-)
// views/main.scala.html
#(title: String)(content: Html)
<!DOCTYPE html>
<html>
<head>
<title>#title</title>
</head>
<body>
<section class="content">#content</section>
</body>
</html>
How to use the base.html?
#main(title = "Home") {
<h1>Home page</h1>
}
More information here

Categories