Selenium Java | Trouble finding/clicking button - java

I'm trying to use Selenium in order to open a HTML page and click a button.
The HTML I get back is:
<html>
<head>
<link rel="shortcut icon" type="image/x-icon">
<link rel="stylesheet" type="text/css" href="http://localhost:5050/style.css">
<title>Test</title>
</head>
<body>
<div class="btn-container">
<button class="btn-orange" id="successButton" name="Success" value="Success"> Success </button>
<button class="btn-orange" id="failButton" name="Fail" value="Fail"></button> Fail
</div>
</div>
<footer class="footer" align="center">
<div class="container-fluid">
<div class="clearfix">
<div class="cards pull-left">
</div>
</div>
</div>
</footer>
</body>
</html>
I'm trying to click the successButton but it does not seem to work, I try to access via its id.
Here is the line I use to click:
driver.findElement(By.id("successButton")).click();
And here's my entire function:
public void openTheHtmlPageAndClickButton(
String pageUrl,
String SiteUrl,
String buttonValue) {
String lastUrl = null;
boolean timeout = true;
for (int tryNumber = 1; tryNumber <= 5 && timeout; tryNumber++) {
WebDriver driver = null;
try {
driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), DesiredCapabilities.firefox());
System.out.println("Selenium open: " + pageUrl);
driver.get(pageUrl);
int i = 0;
Alert alert;
while (i++ < 30) {
try {
alert = driver.switchTo().alert();
if (alert != null) {
alert.accept();
}
} catch (NoAlertPresentException e) {
}
String currentUrl = driver.getCurrentUrl();
driver.findElement(By.id("successButton")).click();
if (!currentUrl.equals(lastUrl)) {
System.out.println("currentUrl: " + currentUrl);
lastUrl = currentUrl;
if (currentUrl.startsWith(SiteUrl)) {
timeout = false;
break;
}
} else {
try {
Thread.sleep(5000);
} catch (InterruptedException e1) {
Assert.fail();
}
}
}
} catch (Exception e) {
System.out.println("Selenium exception: " + e.toString());
} finally {
if (driver == null) {
Assert.fail("Cannot open web driver, probably Selenium docker is down");
} else {
if (timeout) {
System.out.println("Page got timeout: page source: " + driver.getPageSource());
if (tryNumber == 5) {
Assert.fail("Page got timeout 3 times!!!");
}
}
driver.quit();
}
}
}
}
Please advise on what am I doing wrong.

Apparently the button did not appear when I tried to 'click' it. Changed the timeout slightly and issue was resolved.

Related

How can I display an already saved image in mongodb using angularjs?

I already know how to save images in mongodb using angularjs and java to save it in my mongodb, it is working fine.
Now, I need to get the saved image from mongodb and display it in an html page using AngularJS.
ps: I am aware of ngSrc.
My html:
<form method="post" enctype="multipart/form-data" name="formCadastroFotos" id="formCadastroFotos" role="form" novalidate>
<div class="form-group">
<label for="foto">Upload da Foto</label>
<input type="file" name="foto" id="foto" onchange="angular.element(this).scope().uploadFile(this.files)">
</div>
<br>
<div class="alert alert-success" ng-show="show" role="alert">
<center>
<strong>OK!</strong> Foto cadastrada com sucesso.
</center>
</div>
<div class="alert alert-danger" ng-show=showErro role="alert">
<center>
<strong>Erro!</strong> Foto não foi cadastrada com sucesso.
</center>
</div>
</form>
My js:
var myApp = angular.module('AntInformaticaApp', ['ngMessages']);
myApp.controller('CadastroFotosController', ['$scope', '$http', ' $window', function ($scope, $http, $window) {
console.log("Entrou no javascript para envio da foto.");
$scope.uploadFile = function(files) {
var fd = new FormData();
//Take the first selected file
fd.append("file", files[0]);
$http.post('cadastroFotos', fd, {
withCredentials: true,
headers: {'Content-Type': undefined }}).then(function(response) {
$scope.showOk();
}, function(response) {
console.log("erro");
$scope.showNOk();
});
};
$scope.logout = function(){
window.location = '/logout';
}
$scope.showOk = function(){
$scope.show=true;
}
$scope.showNOk = function(){
$scope.showErro=true;
}
}]);
It is going correctly to my java controller:
#Controller
public class CadastroFotosController {
#RequestMapping(value="/cadastroFotos", method=RequestMethod.POST)
public #ResponseBody MsgRetornoDTO cadastroPecas(#RequestParam("file") MultipartFile file) throws IOException {
MsgRetornoDTO retorno = new MsgRetornoDTO();
retorno = UtilGeral.salvaFotoMongo(file);
return retorno;
}
}
public static MsgRetornoDTO salvaFotoMongo(MultipartFile file){
MsgRetornoDTO retorno = new MsgRetornoDTO();
if (!file.isEmpty()) {
try {
Mongo mongo = new Mongo("localhost", 27017);
DB db = mongo.getDB("antinformatica");
DBCollection collection = db.getCollection("dummyCol");
String newFileName = file.getOriginalFilename();
File imageFile = new File(file.getOriginalFilename());
imageFile.createNewFile();
FileOutputStream fos = new FileOutputStream(imageFile);
fos.write(file.getBytes());
fos.close();
// create a "photo" namespace
GridFS gfsPhoto = new GridFS(db, "photo");
// get image file from local drive
GridFSInputFile gfsFile = gfsPhoto.createFile(imageFile);
// set a new filename for identify purpose
gfsFile.setFilename(newFileName);
// save the image file into mongoDB
gfsFile.save();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (MongoException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println("empty file.");
}
retorno.setCodReturn(0);
retorno.setDescReturn("OK");
return retorno;
}
I appreciate your time helping, thanks.
have you got the answer if you get it please reply me or post it i am in same stuck. I also save image in mongodb via Angular.And I know how to fetch that image by download and dont know how to display in html page. If you get it please email id- symonkt#gmail.com

How to redirect to another page using jsoup and continue to print content in ListView?

At general I have website where the main content is the list of posts with text. So I parse every post in this block of HTML code.
<div class="col-xs-12" style="margin:0.5em 0;line-height:1.785em">Some text</div>
For this I created this AsyncTask.
class NewPostsAsyncTask extends AsyncTask<String, Void, String> {
#Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setTitle("Новые");
progressDialog.setMessage("Загрузка...");
progressDialog.setIndeterminate(false);
progressDialog.show();
}
#Override
protected String doInBackground(String... params) {
Document doc;
try {
doc = Jsoup.connect(URL).get();
content = doc.select("[style=margin:0.5em 0;line-height:1.785em]");
titleList.clear();
for (Element contents : content) {
if (!contents.text().contains("18+")) {
titleList.add(contents.text());
}
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
listView.setAdapter(adapter);
progressDialog.dismiss();
}
}
But I have some problem. All posts are not stored on one web page. You must click to link at the end of all posts for redirecting to another page with posts.
And this block have this HTML code.
<div class="row"><div class="col-xs-12">
<div class="paginator">
<span class="pagina">1683</span> " | "
<span class="pagina">1682</span> " | "
<span class="pagina">1681</span> " | "
<span class="pagina">1680</span> " | "
<span class="pagina">1679</span> " | "
<span class="pagina">3</span> " | "
<span class="pagina">2</span> " | "
<span class="pagina">1</span>
</div>
</div>
</div>
How can I go to another page, parse other posts and print them in ListView after previous posts? As a result I want have all posts of this website in one ListView. Can you show me how I should do it?
Here is how I would do it:
Sample code
#Override
protected String doInBackground(String... params) {
Document doc;
// I supposed URL variable is initialized like this: URL="killpls.me";
try {
do {
doc = Jsoup.connect(URL).get();
content = doc.select("[style=margin:0.5em 0;line-height:1.785em]");
titleList.clear();
for (Element contents : content) {
if (!contents.text().contains("18+")) {
titleList.add(contents.text());
}
}
Element anchor = doc.select( //
"#stories > div:nth-child(3) > div:nth-child(1) > div:nth-child(1) > span.pagina:not(:has(a)) + span > a" //
).first();
if (anchor==null) {
break;
} else {
doc = null;
URL = anchor.absUrl("href");
}
} while(canContinue());
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private boolean canContinue() {
// Implement custom logic here ...
// Return true if additionnal posts should be downloaded false otherwise.
return true;
}
Some details
The heart of the method lies on the following line:
Element anchor = doc.select( //
"#stories > div:nth-child(3) > div:nth-child(1) > div:nth-child(1) > span.pagina:not(:has(a)) + span > a" //
).first();
The first() method will return a non null reference as long as a next page exists. When the first page is reached, first() returns null and there is no more page to fetch.
#stories /* Find an element with id `stories`*/
> div:nth-child(3) /* Select its third div child */
> div:nth-child(1) /* Select first div child of the previous div */
> div:nth-child(1) /* Select first div (DIV-a) child of the previous div */
> span.pagina:not(:has(a)) /* Select a span with class `pagina` without any anchor as child */
+ span /* Select closest span next to previous span and child of `DIV-a` */
> a /* Here is the next page to fetch */

Java - JSOUP: selecting a specific part of a website

I try to readout the Office 365 Website to compare it to a proxy configuration. But i cant get the select right so that it just gets me a specific section of those urls and ip addresses.
public class Office365WebsiteParser {
Document doc = null;
String WebseitenInhalt;
public void Parser() {
System.setProperty("http.proxyHost", "xxx");
System.setProperty("http.proxyPort", "8081");
System.setProperty("https.proxyHost", "xxx");
System.setProperty("https.proxyPort", "8081");
for (int i = 1; i <= 5; i++) {
try {
doc = Jsoup.connect("https://technet.microsoft.com/de-de/library/hh373144.aspx").userAgent("Mozilla").get();
break; // Break immediately if successful
} catch (IOException e) {
// Swallow exception and try again
System.out.println("jsoup Timeout occurred " + i + " time(s)");
}
}
if (doc == null) {
System.out.println("Connection timeout after 5 tries");
} else { // Wenn alles funktioniert hat Webseite auswerten
Elements urls_Office365_URLs = doc.select("div.codeSnippetContainerCode");
// HTML auswahl der Webseite nach div class und div id
// urls_Office365_URLs_global = urls_Office365_URLs;
WebseitenInhalt=urls_Office365_URLs.text();
}
}
public void Print() {
System.out.println(WebseitenInhalt);
}
public String get() {
return WebseitenInhalt;
}
}
I just want to select the containers like this:
<div class="codeSnippetContainerCodeContainer">
<div class="codeSnippetToolBar">
<div class="codeSnippetToolBarText">
<a name="CodeSnippetCopyLink" style="display: none;" title="In Zwischenablage kopieren" href="javascript:if (window.epx.codeSnippet)window.epx.codeSnippet.copyCode('CodeSnippetContainerCode_0f6f9acf-6aa4-471f-8600-f8d059f95493');">Kopieren</a>
</div>
</div>
<div id="CodeSnippetContainerCode_0f6f9acf-6aa4-471f-8600-f8d059f95493" class="codeSnippetContainerCode" dir="ltr">
<div style="color:Black;"><pre>
*.live.com
*.officeapps.live.com
*.microsoft.com
*.glbdns.microsoft.com
*.microsoftonline.com
*.office365.com
*.office.com
Portal.Office.com
*.onmicrosoft.com
*.microsoftonline-p.com^
*.microsoftonline-p.net^
*.microsoftonlineimages.com^
*.microsoftonlinesupport.net^
*.msecnd.net^
*.msocdn.com^
*.msn.com^
*.msn.co.jp^
*.msn.co.uk^
*.office.net^
*.aadrm.com^^
*.cloudapp.net^^
*.activedirectory.windowsazure.com^^^
*.phonefactor.net^^^
</pre></div>
</div>
</div>
</div>
Try this CSS selector:
table:has(th:matches(.+-URLs?)) td:first-of-type pre
DEMO

Replacing src attribute in IMG tag in Java

I have a HTML document in which I need to update both text and src attribute of IMG tag. I am working in Java. I want to replace following strings in the HTML: DataName, DataText and DataIcon.
<body>
<h1 align="center">DataName</h1>
<div class="tabber">
<div class="tabbertab">
<h2>Info</h2>
<p>DataText</p>
</div>
<div class="tabbertab">
<h2>Pictures</h2>
<div id="album">
<ul class="gallery">
<li>1<img src=DataIcon alt="landscape image 1" title="landscape image 1" /></li>
<li>2<img src="C:\thesis\100GreatP\eclipse_ws\test\data\pictures\1\pyramid2.jpg" alt="landscape image 2" title="landscape image 2" /></li>
</ul>
</div>
</div>
<div class="tabbertab">
<h2>Video</h2>
</div>
</div>
While I have suceeded to replace strings DataName and DataText, I havent succeed to replace DataIcon by my imageURL stored in the database as String. Checking the debug says that it just simply fails to search for DataIcon string. I am using HTMLparser and I have written following class to apply the problem:
public class MyNodeVisitor extends NodeVisitor {
String name;
String text;
String icon;
public MyNodeVisitor() {
}
public MyNodeVisitor(String IconPath, String Name, String Text){
this.name = Name;
this.text = Text;
this.icon = IconPath;
}
public void visitStringNode (Text string)
{
if (string.getText().equals("DataName")) {
string.setText(name);
}
else if(string.getText().equals("DataIcon")){
string.setText(icon);
}
else if (string.getText().equals("DataText")){
string.setText(text);
}
}
}
The class has been applied in my application code in such way
NodeList nl = new NodeList();
String htmlString = null;
InputStream contentStream = null;
String textString = null;
String resultStr = getDatabaseAttribute(name,"DESCRIPTION");
String resultStr2 = getDatabaseAttribute(name,"NAME");
String resultStr3 = getDatabaseAttribute(name,"ICON_path");
try
{
// Read the URL content into a String using the default encoding (UTF-8).
contentStream = WWIO.openFileOrResourceStream(BROWSER_BALLOON, this.getClass());
htmlString = WWIO.readStreamToString(contentStream, null);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
WWIO.closeStream(contentStream, resultStr);
}
try {
Parser parser = new Parser(htmlString);
nl = parser.parse(null);
nl.visitAllNodesWith(new MyNodeVisitor(resultStr3, resultStr2,resultStr));
nl.toString();
} catch (ParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String output = nl.toHtml();
return output;
Can anybody help me? The whole problem is that it fails to search for DataIcon string in IMG tag. Thanks for your help.
Your img tag isn't an StringNode. You need to override the visitTag(Tag tag) method and work on the Tag object.
Something like (not compiled)
public void visitTag(Tag tag) {
if ("img".equals(tag.getTagName())) {
if ("DataIcon".equals(tag.getAttribute("src"))) {
tag.setAttribute("src", icon);
}
}
}

How to manage back end process with Tomcat?

I have a web page that the user pushes a button and initiates an action in a proprietery app (which resides in Tomcat).
That action is a long running process and the only way I have to see what's going on is to log onto the server and look at a log file.
I wrote a quick Java function that reads the log file and gives feedback on what's happening. (Essentially it just tails the file and parses out the things I need)
I'd like to be able to add a jsp that I can view the output without logging into the server.
===
From a design standpoint, I understand that the JSP should return quickly with a result and not just keep on processing.
So my idea is to create a simple web page that queries the jsp for an update and writes the latest info to the screen. Wait 30 seconds, poll the server again, and append the latest update.
What I'm struggling to grasp is how to get the JSP to communicate with the back end process, and how that back end process should be spawned / killed.
This is a very occasional thing (once every two weeks, start to completion takes an hour or two), so I don't want a daemon running all the time. I want to be able to turn it on temporarily and turn it off.
If I spawn a process from within a simple servlet, how do I end that process when I'm done?
And how do I communicate with it?
You can create a java.lan.Runnable witch reads the file content an saves it into a buffer. The Runnable reads the file content withing a while loop for witch the break condition can be set from outside, Thread that is executing your Runnable will terminate whe the run method of your Runnable terminates.
In your JSP you can create a java.lang.Thread and pass an instance of your Runnable to it. Save th instace of the runable in the ServletContext so you can access it across the requests. If you want to terminate the polling than just set the break condition of your Runnable from the JSP, the rum method will terminate and thus the thread too.
You can use javascript setInterval() function and XMLHttpRequest refresh the page.
here is a sample basic implemntation (I hope this will meet your requirements):
Polling Runnable
package com.web;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
public class FilePollingThread implements Runnable {
private String filepath = null;
private boolean polling = false;
private StringBuffer dataWritenAfterLastPoll = null;
private String error = null;
public FilePollingThread(String filepath) {
this.filepath = filepath;
}
#Override
public void run() {
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(filepath)));
dataWritenAfterLastPoll = new StringBuffer();
polling = true;
String line = null;
while(polling) {
try {
line = br.readLine();
while(line == null) {
try {
Thread.sleep(500L);
} catch (InterruptedException e) {
e.printStackTrace();
error = e.toString();
}
line = br.readLine();
}
dataWritenAfterLastPoll.append(markUp(line));
} catch (IOException e) {
e.printStackTrace();
error = e.toString();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
error = e.toString();
} finally {
if(br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
error = e.toString();
}
}
}
}
private String markUp(String line) {
String markup = "";
if(line != null) {
markup = "<div style=\"height: 6px\"><span style=\"line-height: 1.1;\">" + line + "</span></div>\n";
}
return markup;
}
public synchronized void stopPolling() {
polling = false;
}
public synchronized String poll() {
String tmp = markUp(error == null ? "Not ready" : error);
if(dataWritenAfterLastPoll != null) {
tmp = dataWritenAfterLastPoll.toString();
dataWritenAfterLastPoll = new StringBuffer();
}
return tmp;
}
}
And a JSP witch initiats the polling an keep retrieving data
<?xml version="1.0" encoding="ISO-8859-1" ?>
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# page import="com.web.FilePollingThread" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Poll file</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<link rel="stylesheet" type="text/css" href="style/default.css"></link>
<script type="text/javascript">
var c = 1;
var ih;
var polling = false;
var filepath = null;
function startPolling(interval) {
ih = setInterval(function () {
try {
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function () {
if(xmlHttp.readyState == 4) {
if(xmlHttp.status == 200) {
var w = getElementById('ajax_content');
w.innerHTML = w.innerHTML + xmlHttp.responseText;
getElementById('page_refresh').innerHTML = c++;
polling = true;
window.scrollTo(0, document.body.scrollHeight);
} else {
polling = false;
throw 'HTTP ' + xmlHttp.status;
}
}
};
xmlHttp.open('GET', 'pollfile.jsp?filepath=' + filepath + '&c=' + c, true);
xmlHttp.send();
} catch(e) {
alert('Error at startPolling: ' + e);
clearInterval(ih);
}
}, interval);
}
function startStopPolling() {
var orgPolling = polling;
try {
if(polling) {
polling = false;
clearInterval(ih);
doPolling();
} else {
polling = true;
doPolling();
startPolling(1000);
}
flipStartStopButtonsLabel();
} catch(e) {
polling = orgPolling;
flipStartStopButtonsLabel();
alert('Error at startStopPolling: ' + e);
}
}
function flipStartStopButtonsLabel() {
var label;
if(polling) {
c = 1;
label = 'Stop polling';
getElementById('page_refresh').innerHTML = '0';
} else {
label = 'Sart polling';
getElementById('page_refresh').innerHTML = 'stoped';
}
var buttons = document.getElementsByName('start_stop_polling');
if(buttons) {
for(var i = 0; i < buttons.length; i++) {
buttons[i].value = label;
}
}
}
function doPolling() {
var url = 'pollfile.jsp?polling=';
if(polling) {
filepath = getElementById('filepath');
if(filepath && filepath.value && filepath.value.length > 0) {
url += 'true&filepath=' + encodeURIComponent(filepath.value);
} else {
throw 'No filepath specified.';
}
} else {
url += 'false';
}
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function () {
if(xmlHttp.readyState == 4) {
if(xmlHttp.status != 200) {
throw 'HTTP ' + xmlHttp.status;
}
}
};
xmlHttp.open('POST', url, false);
xmlHttp.send();
}
function clearWindow() {
var w = getElementById('ajax_content');
if(w) {
w.innerHTML = '';
}
}
function getElementById(id) {
try {
if(id) {
elm = document.getElementById(id);
return elm;
}
} catch(e) {
alert('Error at getElementById: ' + e);
}
return null;
}
</script>
</head>
<body>
<%
String polling = request.getParameter("polling");
if("true".equals(polling)) {
String filepath = request.getParameter("filepath");
if(filepath != null && filepath.length() > 0) {
FilePollingThread pollingThread = new FilePollingThread(filepath);
new Thread(pollingThread, "polling thread for file '" + filepath + "'").start();
request.getServletContext().setAttribute("pollingThread", pollingThread);
}
} else if("false".equals(polling)) {
FilePollingThread pollingThread = (FilePollingThread) request.getServletContext().getAttribute("pollingThread");
if(pollingThread != null) {
pollingThread.stopPolling();
}
} else {
FilePollingThread pollingThread = (FilePollingThread) request.getServletContext().getAttribute("pollingThread");
if(pollingThread != null) {
response.getWriter().println(pollingThread.poll());
response.getWriter().close();
return;
}
}
%>
<div class="label">
<span>Page polling:</span>
</div>
<div style="float: left;">
<span id="page_refresh">0</span>
</div>
<div class="clear_both"> </div>
<form id="input_form" action="pollfile.jsp" method="get">
<div>
<div style="float: left;">
<label>Filepath:
<input style="height: 24px;" id="filepath" type="text" size="120" value=""/>
</label>
</div>
<div style="clear: both;"/>
<div style="float: left;">
<input style="height: 24px;" name="start_stop_polling" id="start_stop_polling_button" type="button" onclick="startStopPolling(); return false;" value="Start polling"/>
</div>
<div style="float: left;">
<input style="height: 24px;" name="clear_window" id="clear_window_button" type="button" onclick="clearWindow(); return false;" value="Clear"/>
</div>
<div style="clear: both;"> </div>
</div>
</form>
<div id="ajax_content">
</div>
<div>
<div style="float: left;">
<input style="height: 24px;" name="start_stop_polling" id="start_stop_polling_button" type="button" onclick="startStopPolling(); return false;" value="Start polling"/>
</div>
<div style="float: left;">
<input style="height: 24px;" name="clear_window" id="clear_window_button" type="button" onclick="clearWindow(); return false;" value="Clear"/>
</div>
<div style="clear: both;"> </div>
</div>
</body>
</html>
EDIT
there is a bug in FilePollingThread: if no data is available in the file the thread might get stuck in inner while loop. it should be
while(line == null && polling)
also the JSP will not work on IE (testet on IE9). It seems that the data writen to the response in the line
response.getWriter().println(pollingThread.poll());
contains the HTML of the hole page. If added to the target div IE seems not able to render it.
I made another version using a simple static HTML file an a servlet as it offers more controle on what is writen to response.
If you are interested in the code let me know.
You should consider using something like JMS to control your background process.
For control, your front-end code can send messages start/stop/inspect the process.
For monitoring, your process can publish to a JMS topic for your front end code to read.

Categories

Resources