I need to print the output of two tables side by side in HTML using JAVA. But they are not printed side by side. I am using this code:
FileWriter fw = new FileWriter(strFilePath,true); //the true will append the new data
fw.write("<h1>"+name+"</h1>");
fw.write("<table float='left' border='1' BORDERCOLOR=Black width='50%' height='47'>");
fw.write("<tr>");
fw.write("<td width='24%' bgcolor='#CCCCFF'><b><font color='#000000' face='Tahoma' size='2'>Environment</font></b></td>");
fw.write("<td width='24%' bgcolor='#CCCCFF'><b><font color='#000000' face='Tahoma' size='2'>Account</font></b></td>");
fw.write("<td width='23%' bgcolor='#CCCCFF'><b><font color='#000000' face='Tahoma' size='2'>COUNT</font></b></td>");
fw.write("<td width='18%' bgcolor='#CCCCFF' align='center'><b><font color='#000000' face='Tahoma' size='2'>Frequency</font></b></td>");
fw.write("</tr>");
fw.close();
In HTML/CSS you need to give the first table float: left; and the second table margin-left
For Example
table{
border: solid;
}
#one{
float: left;
}
#two{
/* margin has to be atleast the width of #one */
margin-left: 50px;
}
<table id="one">
<tr>
<th>Test</th>
</tr>
<tr>
<td>Test</td>
</tr>
</table>
<table id="two">
<tr>
<th>Test</th>
</tr>
<tr>
<td>Test</td>
</tr>
</table>
Related
I'm once again asking simple questions, please forgive my level of knowledge. Thanks for reading the question.
I'm trying to create a link from the data within a td:nth-child(4), its the location within a table.
The data within the table looks something like example.com
I want to create a ahref, so I can click this.
Thanks for the help.
You could select every td:nth-child(4) using document.querySelectorAll(). Then for each td change its innerHTML property so as to transform it into an anchor element :
let tds = document.querySelectorAll("td:nth-child(4)");
for(td of tds){
td.innerHTML = "<a href='" + td.innerText + "'>" + td.innerText + "</a>";
}
table, th, td {
border: solid 1px black;
}
<table>
<tr>
<th>1</th>
<th>2</th>
<th>3</th>
<th>url</th>
</tr>
<tr>
<td>4</td>
<td>5</td>
<td>6</td>
<td>foo.bar</td>
</tr>
<tr>
<td>7</td>
<td>8</td>
<td>9</td>
<td>bar.foo</td>
</tr>
</table>
Alright, I've been making a table that allows the user to enter certain input and acts like excel. Since this has nothing to do with the purpose of the program and more of how it displays things, I won't discuss its purpose in this post. Is there a way to add more decimal points to the demo output? I have tried adding 'total.toFixed(3) That didn't solve the issue. Then I tried what I have inserted now. I am out of ideas.
import java.text.NumberFormat;
private static NumberFormat percent = NumberFormat.getPercentInstance();
var rate = .00125;
var x = myFunction(.04375, rate);
function myFunction(a, b) {
return a + b;
}
var total = x*100;
percent = new DecimalFormat("0.000#%");
document.getElementById("demo").innerHTML = percent.format(total);
<!DOCTYPE>
<style>
th{
font-family: Arial Narrow;
background: #D6D6D6;
overflow: hidden;
font-size: 28px;
}
table, th, td{
border-color: black;
border: 1px solid black;
border-collapse: collapse;
}
.row2 {
font-family: Arial Narrow;
font-size: 28px;
}
td {
</style>
<html>
<head>
<table border="1">
<tr>
<th colspan="9" >FANNIE MAE MANDATORY</th>
</tr>
<tr class="row2">
<td></td>
<td colspan="2">30</td>
<td colspan="2">20</td>
<td colspan="2">15</td>
<td colspan="2">10</td>
</tr>
<tr class="row2">
<td></td>
<td>1 PT</td>
<td>0 PT</td>
<td>1 PT</td>
<td>0 PT</td>
<td>1 PT</td>
<td>0 PT</td>
<td>1 PT</td>
<td>0 PT</td>
</tr>
<tr class="row2">
<td>$35,000 - $99,999</td>
<td><p id="demo"></p></td>
<td id="c7"></td>
<td id="d7"></td>
<td id="e7"></td>
<td id="f7"></td>
<td id="g7"></td>
<td id="h7"></td>
<td id="i7"></td>
</tr>
</table>
Use a floating point with String.format
String.format("%.3f", number)
Try using string format by mentioning the type of decimals you need such as #xxx.xx it restricts the decimal of 2 based on rounding strategy
I want to get the number of columns in a given row, when I use below snippet
int size = element.findElement(By.xpath("/tr[" + Row + "]")).findElements(By.tagName("td")).size();
I get an exception stating NoSuchElementException.
Below is the HTML block
<table class="DynamicOrderTable" id="customerOrderHeader">
<tbody>
<tr style="background-color: rgb(246, 246, 246);">
<th>Order Number</th>
<th>Version</th>
<th>Customer Name</th>
<th>Order Status</th>
<th>Order Sub-status</th>
</tr>
<tr class="Temp">
<td class="close" id="orderNumber">
<div class="clickableText">1234</div>
</td>
<td class="close">1</td>
<td class="close" id="customerName">ABC101</td>
<td title="OrderResponse Sequence Number:14" class="close" id="orderStatus"><div class="clickableText">Complete</div></td>
<td class="close">Closed</td>
</tr>
</tbody>
</table>
How to get the number of columns in a row when we have thead and tbody tags?
Your code to get column size is correct, just change /tr to .//tr in your xpath as :-
WebElement element = driver.findElement(By.id("customerOrderHeader"));
int size = element.findElement(By.xpath(".//tr[" + Row + "]")).findElements(By.tagName("td")).size();
Or
int size = element.findElements(By.tagName("tr")).get(Row).findElements(By.tagName("td")).size();
If I understood your question correctly, code below should help you to get no. of columns.
Using XPath
int size = driver.findElements(By.xpath("//*[#id="customerOrderHeader"]/tbody/tr[1]/th")).size();
Using CssSelector
int size = driver.findElements(By.cssSelector("table#customerOrderHeader>tbody>tr:first-child>th")).size();
Try the below Code:
int count =driver.findElements(By.xpath(".//tr[#class='Temp']//td")).size();
System.out.println("count is "+count);//output is count is 5
use cells property with row index
document.getElementById("customerOrderHeader").rows[0].cells.length;
After Entering a string into a table with a checkbox next to it, I would like to click on the checkbox. In selenium, how can i iterate through the table and search for a particular text, then check the checkbox next to it.
Here's the html of the table:
<tbody>
<tr class="keyword-list-item">
<td width="75%">
<input class="keyword-selection-checkbox" type="checkbox" data-id="gw_78669090303"/>
<span>+spatspatulalas</span>
</td>
<td width="25%" style="text-align: right; padding-right: 4px;">
<span class="icon iconGoogle"/>
</td>
</tr>
<tr class="keyword-list-item">
<td width="75%">
<input class="keyword-selection-checkbox" type="checkbox" data-id="gw_102731166303"/>
<span>12.10 test post</span>
</td>
<td width="25%" style="text-align: right; padding-right: 4px;">
<span class="icon iconGoogle"/>
</td>
</tr>
You can use xpath for this. Just needs be little smart how you write the xpath. Notice the following xpath find the checkbox using the text of it.
String text = "12.10 test post";
By xpath = By.xpath("//span[contains(text(),'" + text + "')]/../input");
WebElement element = driver.findElement(xpath);
I have a website that contains a table that look like similar(bigger..) to this one:
</table>
<tr>
<td>
<table width="100%" cellspacing="-1" cellpadding="0" border="0" dir="rtl" style="padding-top: 25px;">
<tr>
<td align="right" style="padding-right: 25px;">
<span class="artist_name_txt">
name
<p class="diccografia">subname</p>
</span>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<table width="100%" border="0" cellspacing="0" cellpadding="0" dir="rtl" style="padding-right: 25px; padding-left: 25px">
<tr>
<td class="songs" align="right">
number1
</td>
</tr>
<tr>
<td class="songs" align="right">
number2
.......
</td>
</tr>
</table>
and I need an idea how can i parse the website and extract this table into 2 arrays -
one will be something like names{number1, number2}
and the second will be links{number1link, number2link}
I tried a lot of ways and nothing really helps me.
You should read the JSoup Cookbook - especially the Selector syntax is very powerful.
Here's an example:
final String html = ...
// use connect().get() instead if you connect to an website
Document doc = Jsoup.parse(html);
List<String> names = new ArrayList<>();
List<String> links = new ArrayList<>();
for( Element element : doc.select("a.artist_player_songlist") )
{
names.add(element.text());
links.add(element.attr("href"));
}
System.out.println("Names: " + names);
System.out.println("Links: " + links);
Output:
Names: [number1, number2]
Links: [/number1link, /number2link]
Android Web Scraping with a Headless Browser
Htmlunit on Android application
HttpUnit/HtmlUnit equivalent for android