Is there any way I can add a column based on another column's attribute? Something like this: If a td has attribute colspan="2", then add one new before it. Thank you.
<table>
<tr>
<td>aaa</td>
<td colspan="2">bbb</td>
<td>ccc</td>
<td>ddd</td>
<td colspan="2">eee</td>
</tr>
</table>
The result table should be:
<table>
<tr>
<td>aaa</td>
<td> just added based on the right below column tag</td>
<td colspan="2">bbb</td>
<td>ccc</td>
<td>ddd</td>
<td> just added based on the right below column tag</td>
<td colspan="2">eee</td>
</tr>
</table>
Based on the help from Reigel and Rob, here is the complete example:
<html>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$('td[colspan=2]').before('<td> just added based on the right below column tag</td>') ;
});
</script>
<body>
<table border="1">
<tr>
<td>aaa</td>
<td colspan="2">bbb</td>
<td>ccc</td>
<td>ddd</td>
<td colspan="2">eee</td>
</tr>
</table>
</body>
</html>
try this...
$('table td[colspan=2]').before('<td> just added based on the right below column tag</td>');
$('td[colspan=2]').before(...)
Related
I want to retrieve only the structure of HTML document using C# as there is a requirement to create a template from the document and store in a database,which can be used in the future to compare if such a document was earlier received and process further For eg if have the below simple HTML:
<HTML>
<BODY>
<DIV name="Span1">Simple HTML Form</DIV>
<FORM>
<SPAN name="TextLabel">EID: 12345</SPAN>
<SPAN name="TextLabel1">Date:'2019-07-10'</SPAN>
</FORM>
<table>
<tr>
<td>Name </td>
<td> Occupation</td>
</tr>
<tr>
<td> XYZ </td>
<td> SSE </td>
</tr>
</table>
</BODY>
</HTML>
I want the following output:
<HTML>
<BODY>
<DIV></DIV>
<FORM>
<SPAN></SPAN>
<SPAN></SPAN>
</FORM>
<table>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
</BODY>
</HTML>
Using HtmlAgilityPack might be an option. You can start from this example and develop...
HtmlDocument doc = new HtmlDocument();
string html = #"<HTML>
<BODY>
<DIV name=""Span1"">Simple HTML Form</DIV>
<FORM>
<SPAN name=""TextLabel"">EID: 12345</SPAN>
<SPAN name=""TextLabel1"">Date:'2019-07-10'</SPAN>
</FORM>
<table>
<tr>
<td>Name </td>
<td> Occupation</td>
</tr>
<tr>
<td> XYZ </td>
<td> SSE </td>
</tr>
</table>
</BODY>
</HTML>";
doc.LoadHtml(html);
var nodes = doc.DocumentNode.Descendants();
you can use Regex :
string html = #"<HTML>
<BODY>
<DIV name=""Span1"">Simple HTML Form</DIV>
<FORM>
<SPAN name=""TextLabel"">EID: 12345</SPAN>
<SPAN name=""TextLabel1"">Date:'2019-07-10'</SPAN>
</FORM>
<table>
<tr>
<td>Name </td>
<td> Occupation</td>
</tr>
<tr>
<td> XYZ </td>
<td> SSE </td>
</tr>
</table>
</BODY>
</HTML>";
Regex regex = new Regex(#"<.+?>");
MatchCollection match = regex.Matches(html);
foreach(var item in match)
Console.WriteLine(item);
I'm trying to use a ListView in an ASP.Net page and failing to get the results I was expecting. My page looks like this:
<table>
<tr>
<td><label class="subHeading">Contacts</label></td>
</tr>
<tr>
<asp:ListView runat="server" id="lvwContacts">
<LayoutTemplate>
<div class="tableWrapper">
<div class="tableScroll">
<table>
<tr>
<th><label>Full Name</label></th>
<th><label>Job Title</label></th>
<th><label>Direct Line</label></th>
<th><label>Mobile Phone</label></th>
<th><label>Email</label></th>
</tr>
<tr id="itemPlaceHolder" runat="server"></tr>
</table>
</div>
</div>
</LayoutTemplate>
<ItemTemplate>
<tr>
... etc
but when I look at the output the table is not appearing inside the divs:
<div class="tableWrapper">
<div class="tableScroll"></div>
</div>
<table>
<tbody>
<tr>
<td><label class="subHeading">Contacts</label></td>
</tr>
<tr></tr>
</tbody>
</table>
<table>
<tbody>
<tr>
<th><label>Full Name</label></th>
<th><label>Job Title</label></th>
<th><label>Direct Line</label></th>
<th><label>Mobile Phone</label></th>
<th><label>Email</label></th>
</tr>
... etc
I've tried putting the divs around the whole listview with much the same result. What on earth is going on here? Have I done something stupid or do ListViews really behave like this?
Thanks
John
You must make sure you have valid HTML markup. Currently one of your <tr>'s has a <div> as a child, not a <td> or <th>.
See this demo:
/* style used to illustrate problem */
.tableWrapper {
padding: 10px;
background: red;
}
<label>Invalid markup</label>
<table>
<tr>
<td><label class="subHeading">Contacts</label></td>
</tr>
<tr> <!-- Invalid. child is a div not a td or th -->
<div class="tableWrapper">
<div class="tableScroll">
<table>
<tr>
<th><label>Full Name</label></th>
<th><label>Job Title</label></th>
<th><label>Direct Line</label></th>
<th><label>Mobile Phone</label></th>
<th><label>Email</label></th>
</tr>
</table>
</div>
</div>
</tr>
</table>
<hr>
<label>Valid markup</label>
<table>
<tr>
<td><label class="subHeading">Contacts</label></td>
</tr>
<tr>
<td> <!-- This is required! -->
<div class="tableWrapper">
<div class="tableScroll">
<table>
<tr>
<th><label>Full Name</label></th>
<th><label>Job Title</label></th>
<th><label>Direct Line</label></th>
<th><label>Mobile Phone</label></th>
<th><label>Email</label></th>
</tr>
</table>
</div>
</div>
</td>
</tr>
</table>
Inspect the rendered output of both tables... you will see what happens when the markup is not valid (what you are experiencing) the browser removes the <div> from the table. The second table has correct markup so it renders as-is
I have a login form designed using a <table> in an ASP.NET application.
A client is asking to modify the layout by moving around a few UI controls. They want to move the Forgot Password link below the Login button so they are on separate lines.
However, for all other clients, everything needs to remain the same, so if any CSS or style changes need to be done, I would need to do it programatically. Are there any easy ways to do this?
I am trying to avoid creating duplicate user controls to fit the layout they want, and then hide/show controls, depending on the client.
<table style="TABLE-LAYOUT: fixed; WIDTH: 370px;">
<COLGROUP>
<COL width="120px">
<COL width="250px">
</COLGROUP>
<tr>
<td>Username:</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="text" name="password"></td>
</tr>
<tr>
<td> </td>
<td>
<table>
<tr>
<td style="text-align:right;">
Forgot Password
</td>
<td>
<button type="submit">Login</button>
</td>
</tr>
</table>
</td>
</tr>
</table>
JSfiddle Demo
EDIT: jQuery NOT allowed. JavaScript OK.
Jquery can do this for you. the code might look something like this:
$("#toggle").click(toggleButtonPosition);
function toggleButtonPosition() {
var buttonRow = $("button[type=submit]").closest("tr"), buttonCell, newTR, tr;
if (buttonRow.find("td").length === 2) {
// the table is in its initial configuration. Need to extract the button cell and add it to a new row
buttonCell = buttonRow.find("button").closest("td");
buttonCell.remove();
newTR = $("<tr />").append(buttonCell);
newTR.insertBefore(buttonRow);
} else {
// Reverse the process
buttonCell = buttonRow.find("td");
tr = buttonRow.siblings().first();
tr.append(buttonCell);
buttonRow.remove();
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table style="TABLE-LAYOUT: fixed; WIDTH: 370px;">
<COLGROUP>
<COL width="120px">
<COL width="250px">
</COLGROUP>
<tr>
<td>Username:</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="text" name="password"></td>
</tr>
<tr>
<td> </td>
<td>
<table>
<tr>
<td style="text-align:right;">
Forgot Password
</td>
<td>
<button type="submit">Login</button>
</td>
</tr>
</table>
</td>
</tr>
</table>
Toggle Button Position
You don't need to duplicate the layout. You can do it by adding/ removing the css class depending upon the client using jquery or javascript.
$(document).ready(function(){
var client1 = true; // variable to store client
if(client1){
$('button').removeClass('client2').addClass('client1');
}
else {
$('button').removeClass('client1').addClass('client2');
}});
Css classes to be added/removed from the button depending upon the client. I have put 'client1' css class on the button.
button.client1{display:block;margin:0 auto}
button.client2{float:right;margin-left:4px}
Html code as follows:
<table style="TABLE-LAYOUT: fixed; WIDTH: 370px;">
<COLGROUP>
<COL width="120px">
<COL width="250px">
</COLGROUP>
<tr>
<td>Username:</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="text" name="password"></td>
</tr>
<tr>
<td> </td>
<td>
<table>
<tr>
<td>
<button type="submit" class='client1'>Login</button>
Forgot Password
</td>
</tr>
</table>
</td>
</tr>
You could use jQuery to archive that.
Here is an example of moving the button one step back
$(document).ready(function() {
var loginLayOut = $(".login"); // login Table
var btnTd = loginLayOut.find("button").parent(); // button container, in this case its td
var tbody = btnTd.parent().parent(); // we get the tbody by moving two step back
var tr = $("<tr></tr>"); // create a new tr
tr.append(btnTd); // append the td to the new created tr
tbody.prepend(tr); // insert the tr to the tbody at position 0
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class='login' style="TABLE-LAYOUT: fixed; WIDTH: 370px;">
<COLGROUP>
<COL width="120px">
<COL width="250px">
</COLGROUP>
<tr>
<td>Username:</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="text" name="password"></td>
</tr>
<tr>
<td> </td>
<td>
<table>
<tr>
<td style="text-align:right;">
Forgot Password
</td>
<td>
<button type="submit">Login</button>
</td>
</tr>
</table>
</td>
</tr>
</table>
I have 2 test cases in which i want to select the html and need to get the values of all td's
1. Need to select the 2nd html displayed in the webpage and iterate through the td's and get the value
2. Need to select the 3rd html displayed in the webpage and iterate through the td's and get the value.
Below is the html
<html> - 1st html in the page
<body>
<table>
<tbody>
<tr>
<td>..</td>
....
</tr>
.....
....
</tbody>
</table>
</body>
</html>
<iframe> - Parent
<iframe tabindex="-1" frameborder="no" width="100%" src="about:blank" class="active">
<html> - 2nd html in the page
<body>
<table>
<tbody>
<tr>
<td>..</td>
<td>..</td>
<td>..</td>
</tr>
<tr>
<td>..</td>
<td>..</td>
<td>..</td>
</tr>
<tr>
<td>..</td>
<td>..</td>
<td>..</td>
</tr>
....
....
.....
<tbody>
</table>
</body>
</html>
</iframe>
<iframe tabindex="-1" frameborder="no" width="100%" src="about:blank" class="active">
<html> - 3rd html in the page
<body>
<table>
<tbody>
<tr>
<td>..</td>
<td>..</td>
<td>..</td>
</tr>
<tr>
<td>..</td>
<td>..</td>
<td>..</td>
</tr>
<tbody>
</table>
</body>
</html>
</iframe>
</iframe>
FYI, I'm switching to the IFrame before finding the element
driver.SwitchTo().Frame(); - outerframe
driver.SwitchTo().Frame(); - InnerFrame
I tried the below one to find the element, but it is selecting the first html element on the webpage.
List<IWebElement> elements = driver.FindElement(By.XPath("/html/body/table/tbody")).FindElements(By.TagName("tr")).ToList();
Could anyone help me out on this?
Thanks
Try this:
var frames = driver.FindElements(By.TagName("iframe")).ToList()
driver.switchTo().frames[1];
tds = WebDriver.FindElements.(By.Tagname("td");;
var td = tds.Where(ele=>ele.Text=="Something").ToList();
You should use code like below:
List<WebElement> elements = driver.findElements(By.xpath(""));
for(WebElement el: elements){
String value = el.findElement(By.tagName(td)).getText();
System.out.println(value);
}
While iterating frame td's you should select frame first then apply same as above code
I want to develop a program to automatically lookup words in Longman online dictionary and copy its definition and meanings. I am using visual studio and C# language and I have developed the part which browse to the website and search for a word. However, the problem is in navigating through Longman online website when there are some word forms. for example for this link the html code of the suggested words is as following:
<div class="content1">
<style>
.dictionary-results-title .topic_bullet {
margin: 0px;
}
</style>
<div class="border-search">
<div class="dictionary-results-title">
Results from the Longman Dictionary of Contemporary English:
</div>
<div class="dictionary-results-title">
<span class="dictionary-results-title-topic-new">
Click on topic labels to navigate through our Topic Dictionary
</span>
</div>
<!-- google_ad_section_start -->
<div id="42385" class="folded">
<table id="hwdfolded" class="hwdfolded" cellspacing="0" cellpadding="0">
<tr>
<td class="hwdunSelHG"></td>
<td class="hwdunSelHM"></td>
<td class="hwdunSelHD"></td>
</tr>
<tr>
<td class="hwdunSelMG"></td>
<td class="hwdunSelMM">
<a href="/dictionary/superman">
<span class="headword">superman</span></a>
<span class="homographs"></span>
<span class="wordclass">noun</span>
<span class="topiclinks"></span>
</td>
<td class="hwdunSelMD"></td>
</tr>
<tr>
<td class="hwdunSelBG"></td>
<td class="hwdunSelBM"></td>
<td class="hwdunSelBD"></td>
</tr>
</table>
</div>
<div id="42386" class="folded">
<table id="hwdfolded" class="hwdfolded" cellspacing="0" cellpadding="0">
<tr>
<td class="hwdunSelHG"></td>
<td class="hwdunSelHM"></td>
<td class="hwdunSelHD"></td>
</tr>
<tr>
<td class="hwdunSelMG"></td>
<td class="hwdunSelMM">
<a href="/dictionary/Superman">
<span class="headword">Superman</span></a>
<span class="homographs"></span>
<span class="wordclass"></span>
<span class="topiclinks"></span>
</td>
<td class="hwdunSelMD"></td>
</tr>
<tr>
<td class="hwdunSelBG"></td>
<td class="hwdunSelBM"></td>
<td class="hwdunSelBD"></td>
</tr>
</table>
</div>
<script language="JavaScript" type="text/javascript">
parent.curEntryId=42385; parent.prevEntryId=42385; parent.nextEntryId=42385;
parent.gsSenseId=null; parent.giPhrId=null;
</script>
</div>
</div>
I have found the way to find the ID of the words like id="42385" and id="42386" but I cannot navigate through them. There is a table inside each element with these ids. As you can see in the html code the second data of the second row of the table contains the links for each word.
the code I have written to click on them is like this:
HtmlElement Word = webBrowser1.Document.GetElementById("hwdfolded");
foreach (HtmlElement ele in Word.Parent.Parent.Children)
{
if (ele.Id != null && ele.InnerText.ToLower().Contains(Stword))
{
HtmlElement clickon = webBrowser1.Document.GetElementById(ele.Id);
clickon.InvokeMember("click");
//ele.InvokeMember("click");
while (webBrowser1.ReadyState != WebBrowserReadyState.Interactive)
Application.DoEvents();
do
{
Application.DoEvents();
} while (webBrowser1.ReadyState != WebBrowserReadyState.Complete);
break;
}
}
Note that Stword contains the string of the word I am searching for in this example it contains "superman" and also the ele.Id contains one the specified Ids and I checked it in debug mode. But the click command not works. I will appreciate it if you can tell me the solution or give me another solution which is better.
I suggest that you use a scraping tool to perform the navigation through the page. With Selenium it is really easy to obtain elements by XPATH and navigate through them and also obtain the text inside them. Hope it helps.