Please someone help me to write relative Xpath for below:
html/body/div[1]/table/tbody/tr/td[2]/table/tbody/tr[4]/td/table/tbody/tr/td[2]/table/tbody/tr[2]/td[1]/table[1]/tbody/tr[2]/td
As it do not contains any unique identifier firepath is giving an absolute Xpath but i want to use relative Xpath.
As the provided html code doesn't contains any class or id, thus you need to use below mentioned Xpath only:
html/body/div[1]/table/tbody/tr[3]/td/table/tbody/tr[1]/td[2]/div/font/b
But if you add some class or id in your html like shown below:
<html><body><div><table width="100%" cellspacing="0" cellpadding="0" border="0"> <tbody> <tr> <tr> <tr valign="top"> <td height="101"> <table width="270" cellspacing="0" cellpadding="2" border="0"> <tbody> <tr bgcolor="#CCCCCC"> <td width="80%"> <td width="20%"> <div id="price" align="right"> <font face="Arial, Helvetica, sans-serif, Verdana" size="2"> <b>$398</b> </font> </div> </td> </tr> <tr> <tr bgcolor="#CCCCCC"> <tr> <tr bgcolor="#CCCCCC"> </tbody> </table> </td> </tr> </tbody> </table></div></body></html>
Then you can use below mentioned Xpath:
.//*[#id='price']/font/b
Related
I am trying to migrate a webforum where I don't have control over database etc and uses Scrapy to pick the pieces. It is based on an old phpBB forum 2.x.
It is not very well structured so a few challenges.
I now have a HTML string where I need to remove surrounding <td></td>, <span></span>and the Report link at bottom
Starting with:
<td colspan="2"><span class="postbody"></span>
<table width="90%" cellspacing="1" cellpadding="3" border="0" align="center">
<tr>
<td><span class="genmed"><b>Some wrote :</b></span></td>
</tr>
<tr>
<td class="quote">
<table width="90%" cellspacing="1" cellpadding="3" border="0" align="center">
<tr>
<td><span class="genmed"><b>Another wrote:</b></span></td>
</tr>
<tr>
<td class="quote">Just for test
a link
</td>
</tr>
</table>
<span class="postbody">
<br>
<br>
Test quote #1</span>
</td>
</tr>
</table>
<span class="postbody">
<br>
<br>
Test quote #2<br>
Another link: linktext<br>
_________________<br>/ author
<br>
text<br>
<div align="right">[ Rapportera
] </div>
</span><span class="gensmall"></span>
</td>
Wanted result:
<table width="90%" cellspacing="1" cellpadding="3" border="0" align="center">
<tr>
<td><span class="genmed"><b>Some wrote :</b></span></td>
</tr>
<tr>
<td class="quote">
<table width="90%" cellspacing="1" cellpadding="3" border="0" align="center">
<tr>
<td><span class="genmed"><b>Another wrote:</b></span></td>
</tr>
<tr>
<td class="quote">Just for test
a link
</td>
</tr>
</table>
<span class="postbody">
<br>
<br>
Test quote #1</span>
</td>
</tr>
</table>
<br>
<br>
Test quote #2<br>
Another link: linktext<br>
_________________<br>/ author
<br>
text<br>
Any tips?
Why not simply do
html = html.strip('<td colspan="2"><span class="postbody"></span>')
and
html = html.strip('</td>').strip().strip('</span>')
I'm trying to do some stuff with python and selenium but I can't get my xpath to work. I have the following code :
<div id="ctl00_Main_treeCategories">
<table>
<tbody>
<tr>
<td><a id="ctl00_Main_treeCategoriesn0">Online Catalogus</a></td>
</tr>
</tbody>
</table>
<div id="ctl00_Main_treeCategoriesn0Nodes" style="display:block;">
<table cellpadding="0" cellspacing="0" style="border-width:0;">
<tbody>
<tr>
<td>
<div style="width:20px;height:1px"></div>
</td>
<td>
<a id="ctl00_Main_treeCategoriesn1">Dakraam</a>
</td>
</tr>
</tbody>
</table>
<div id="ctl00_Main_treeCategoriesn1Nodes" style="display:block;">
<table cellpadding="0" cellspacing="0" style="border-width:0;">
<tbody>
<tr>
<td><div style="width:20px;height:1px"></div></td>
<td><div style="width:20px;height:1px"></div></td>
<td></td>
<td class="treeNode ctl00_Main_treeCategories_2"><a class="ctl00_Main_treeCategories_0 treeNode ctl00_Main_treeCategories_1">Dakraam Duette® & Plissé Saaaaahade</a></td>
</tr>
</tbody>
</table>
</div>
<table cellpadding="0" cellspacing="0" style="border-width:0;">
<tbody><tr>
<td><div style="width:20px;height:1px"></div></td>
<td></td>
<td class="treeNode ctl00_Main_treeCategories_2" style="white-space:nowrap;">
<a class="ctl00_Main_treeCategories_0 treeNode ctl00_Main_treeCategories_1">Duette® Fixé & Plissé Shadeeeee</a>
</td>
</tr>
</tbody>
</table>
</div>
I try to select all tables within the ctl00_Main_treeCategoriesn0Nodes div, and exclude the div on the first level within (ctl00_Main_treeCategoriesn1Nodes).
I tried :
//*[contains(#id, "ctl00_Main_treeCategoriest")]/div/table/
but this gives a syntax error.
You had two errors on the XPath:
Mispelling of the id (that t at the end)
Trailing / (not necessary)
//*[contains(#id, "ctl00_Main_treeCategories")]/div[not(#id="ctl00_Main_treeCategoriesn1Nodes")]/table
Demo: https://3v4l.org/SusIZ
Edit: Excluded inside ctl00_Main_treeCategoriesn1Nodes divs
I need to select radion button with id="optProfile" in 7th tr using text "Internet Users" in td/font tag. I am using below code but it is not working and selecting radio button from 1st tr row. Can anyone help
<html>
<head>
<body onmousedown="infCheckMouseDown(event);" onkeydown="infCheckDownKey(event);" onbeforeunload="INFIEExit();" onload="INFsetInitialFocus();window_onload();CreateScriptingFrames();">
<form action="PEINFCommon.ASP?WCI=Genericcreen&WCE=GenericEvent" onsubmit="return false" name="Genericcreen" method="post">
<table class="TopLevelTable" align="center" width="100%" border="0">
<tbody>
<tr>
<td width="100%">
<table width="100%" border="0">
<tbody>
<tr>
<tr>
<tr>
<tr>
<tr>
<td width="100%">
<div align="center">
<center>
<table width="98%" cellspacing="0" cellpadding="0" border="0" cols="1">
<tbody>
<tr align="center">
<td align="left">
<table width="100%">
<tbody>
<tr>
<tr>
<tr>
<tr height="">
<tr height="">
<tr height="">
<tr height="">
<tr height="">
<tr height="">
<tr height="">
<tr height="">
<tr height="">
<tr height="">
<tr height="">
<tr height="">
<td width="5%">
<input id="optProfile" type="Radio" callfunction="" value="" onblur="ResetScript(this);return true;" onfocus="HighlightScript(this);" onclick="" name="" size="11">
<font class="fLabel"></font>
<br>
<input id="hidCurrentProfile" type="hidden" onblur="ResetScript(this);" onfocus="HighlightScript(this);" value="N" callfunction="" size="11" name="/Root/ACORD/InsuranceSvcRs/nk_ck_com.cc_FetchUserProfileRs/nk_ck_com.cc_SecurityDetails/nk_ck_com.cc_AvailableProfile[12]/nk_ck_com.cc_CurrentProfileInd">
<input id="hidProfile" type="hidden" onblur="ResetScript(this);" onfocus="HighlightScript(this);" value="RACINTER" callfunction="" size="11" name="/Root/ACORD/InsuranceSvcRs/nk_ck_com.cc_FetchUserProfileRs/nk_ck_com.cc_SecurityDetails/nk_ck_com.cc_AvailableProfile[12]/nk_ck_com.cc_ProfileCd">
</td>
<td>
<font class="">Internet Users</font>
</td>
</tr>
<tr height="">
<tr height="">
<tr height="">
<tr>
<tr>
<tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</center>
</div>
</td>
</tr>
<tr>
<tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</form>
</body>
</html>
</frame>
<frame id="Script" scrolling="Yes" frameborder="0" noresize="">
</frameset>
</html>
you need to use ancestor instead of preceding:
//font[text()='Internet Users']/ancestor::tr[1]/td/input[#type='Radio']
and it's also a better practice to tell which one of the tr-ancestors you want -> [1] - since you want the first direct ancestor
Is there a reason why you are not using the id of the radio button?
WebElement wElement = wDriver.findElement(By.Id("optProfile"));
You can use the following XPath
//tr[.//font[text()='Internet Users']]//input[#id='optProfile']
This xpath looks for the tr containing the font tag with text "Internet Users" and then navigates to the input tag with id = 'optProfile'
You can use preceding as below-
By.xpath("//font[contains(text(),'Internet Users')]/preceding::input[#id='optProfile']")
Thanks everyone here for your help. I really appreciate it.Its working with below code
WebElement trs = GlobalVar.wDriver.findElement(By.xpath("//font[text()='Internet Users']/ancestor::tr[1]"));
trs.findElement(By.id("optProfile")).click();
I would try something like this
List<WebElement> trs = driver.findElements(By.cssSelector("table > tr"));
for (WebElement tr : trs)
{
List<WebElement> fonts = tr.findElements(By.tagName("font"));
if (!fonts.isEmpty() && fonts.get(0).getText().trim().equals("Internet Users"))
{
tr.findElement(By.id("optProfile")).click();
break;
}
}
You didn't show the TABLE tag in the HTML snippet so you will likely need to be more specific in the initial CSS selector. Basically this code loops through the TRs looking for "Internet Users" in the FONT tag. Once it finds it, it click the radio button with id=optProfile.
If you will post the TABLE tag in the HTML snippet I can help with the CSS selector.
Edit 1: changed the code to address the case where some TRs don't contain FONT tags.
I want to interchange the positions of two blocks using some trick for email newsletter. I want the first block to the right and second block to the left.
Tried these:
<table cellpadding="0" cellspacing="0" border="0" width="600">
<tr>
<td>
<table cellpadding="0" cellspacing="0" border="0" width="300" align="right">
content 1
</table>
<table cellpadding="0" cellspacing="0" border="0" width="300" align="left">
content 2
</table>
</td>
</tr>
</table>
<table>
<tr>
<td width="300" align="right" class="column">
content 1
</td>
<td width="300" align="left" class="column">
content 2
</td>
</tr>
</table>
But unfortunately none of them worked. Any other suggestion?
use dir="rtl" on container table and dir="ltr" on the interior tables or tds. This makes it so that the container reads the last table as the beginning and displays on left, but when stacked, it still stacks based on order of tables inside parent.
see below:
<table dir="rtl" cellpadding="0" cellspacing="0" border="0" width="600">
<tr>
<td>
<table dir="ltr" cellpadding="0" cellspacing="0" border="0" width="300" align="right">
content 1
</table>
<table dir="ltr" cellpadding="0" cellspacing="0" border="0" width="300" align="left">
content 2
</table>
</td>
</tr>
</table>
<table dir="rtl">
<tr>
<td dir="ltr" width="300" align="right" class="column">
content 1
</td>
<td dir="ltr" width="300" align="left" class="column">
content 2
</td>
</tr>
</table>
I am attempting to pull a value and a header (string) from a website, but unable to find the element using selenium.
My Code
I used Firebug to get the XPath and this is what it determined:
//*[#id="DimensionForm"]/p[1]/table/tbody/tr[3]/td[3]
Code
Dim Right as double
Dim Marker as string
Marker = selenium.findElementByXPath("//*[#id="DimensionForm"]/p[1]/table/tbody/tr[2]/td[3]").getAttribute("value")
Right = selenium.findElementByXPath("//*[#id="DimensionForm"]/p[1]/table/tbody/tr[3]/td[3]").getAttribute("value")
HTML CODE
<form id="DimensionForm" name="validate" action="Dimension" method="post">
<div style="margin-top: 7px"></div>
<p><table width="100%" cellspacing="0" border="0" cellpadding="0" class="element">
<tr>
<td> </td><td class="formtitlenobg" colspan="6" align='right'>
AREA DIMENSIONS (AREA A) <span class='quote'> Front</span> 25.24</td>
</tr>
<tr align="right">
<td class="tablerowlightgreen" width=10> </td>
<th class="formtitle" width=250 align="left">Property</th>
<th class="formtitle" width=50>Check</th> <th class="formtitle" width=75>Front</th>
<th class="formtitle" width=75>Center</th><th class="formtitle" width=75>Left</th>
<th class="formtitle" width=120>Right</th>
<th class="formtitle" width=100>Total</th>
<td class="tablerow" width=50> </td>
<td class="tablerow"> </td>
</tr>
<tr align="right" nowrap>
<td> </td>
<td class="table" align="left"><strong>
Property O</strong></td>
<td class="table">+</td>
<td class="table">10</td>
<td class="table">12</td>
<td class="table"><strong>12</strong></td>
<td class="table"><strong><font class="front">
100</font></strong></td>
<td class="table">120</td>
<td> </td>
<td> </td>
</tr>
</table></td>
</tr></table>
You have incorrectly nested quotes:
selenium.findElementByXPath("//*[#id="DimensionForm"]/p[1]/table/tbody/tr[2]/td[3]")
Perhaps you meant:
selenium.findElementByXPath("//*[#id='DimensionForm']/p[1]/table//tr[2]/td[3]")
Note the single-quotes in the second line!