About xpath in table java - html

I am trying to find the xpath of the button(its an input for real) from the name of the person in the corresponding line.
the right "connection" button from the name.
I have tried several xpath may saucun do not click the button I want.
It Clicks on the first button of the whole table each time
I tried this:
*d.driver.findElement(By.xpath("//tr/td[contains(text(), 'TOTO')]/following-sibling::td/input"));
and
*d.driver.findElement(By.xpath("//td[contains(text(), 'TOTO')]/following-sibling::td/input"));
and
*d.driver.findElement(By.xpath("//tr/td[contains(text(), 'TOTO')]/following-sibling::td/input[type='submit']"));
and
*d.driver.findElement(By.xpath("//tr/td[contains(text(),'TOTO')]//td[5]/input"));
I enclose the table.
Here is the HTML code :
<tr>
<td>FR1547</td>
<td>CAILLOUX</td>
<td>CHRISTIANE</td>
<td></td>
<td></td>
<td>UTILISATEUR</td>
<td><input value="connection" onclick="document.getElementById('hiddenCuid').value = 'FR1547'" type="submit"></td>
</tr>
<tr>
<td>US7784</td>
<td>TOTO</td>
<td>CHRISTINE</td>
<td></td>
<td></td>
<td>UTILISATEUR</td>
<td><input value="connection" onclick="document.getElementById('hiddenCuid').value = 'US7784'" type="submit"></td>
</tr>
Second button :
<tr>
<td>US4487</td>
<td>PONT</td>
<td>CHRISTIANE</td>
<td></td>
<td></td>
<td>UTILISATEUR, MANAGER</td>
<td><input value="connection" onclick="document.getElementById('hiddenCuid').value = 'US4487'" type="submit"></td>
</tr>

Try this xpath And check with Debugger, Whether its locates correctly:
//input[contains(#onclick,'US4487')]

try this xpath :
//td[text()='TOTO']/parent::tr/child::input

Related

Xpath - Selecting self or closest previous non-empty element

I have the following document:
<html>
<body>
<div>
<table>
<tr>
<td>390920000</td>
<td>A</td>
</tr>
<tr>
<td>390920000</td>
<td></td>
</tr>
<tr>
<td>3924100011</td>
<td>B</td>
</tr>
<tr>
<td>3924100011</td>
<td></td>
</tr>
<tr>
<td>3924100019</td>
<td></td>
</tr>
<tr>
<td>3924100019</td>
<td>C</td>
</tr>
</table>
</div>
</body>
</html>
What I would like is to use an xpath to select /html/body/div/table/tr/td[2], but for each empty element select the previous non-empty element instead. So instead of getting the values 'A','','B','','','C' I would like to get 'A','A','B','B','B','C'. Is this possible?
Btw, nevermind that this is an html and not an xml. I am using HtmlAgilityPack so I create ordinary xpath expressions to select html elements.
If XPath 3 is fine, the following should work:
//table/tr ! head((., reverse(preceding-sibling::*))[normalize-space(td[2]/text()) != ""])/td[2]

XPath - Duplicating results based on tag counts

I have a table that I would like to enter into a spreadsheet/database by duplicating the title based on the number of rows within a sub-table.
I would like to avoid post-processing if possible, so I'm looking for an XPath expression that does this.
For example:
<table>
<tr>
<th>Title One</th>
</tr>
<tr>
<td>
<table>
<tr>
<td>Row one</td>
</tr>
<tr>
<td>Row Two</td>
</tr>
<tr>
<td>Row Three</td>
</tr>
<tr>
<td>Row Four</td>
</tr>
</table>
</td>
</tr>
</table>
From above, is there an XPath expression that would return 'Title One' 4 times, based on the number of tr//td tags in the subtable? For example:
Title One
Title One
Title One
Title One
XPath 2.0 or 3.0 can do that in a single expression:
for $r in 1 to count(/table/tr[2]/td/table/tr/td) return /table/tr[1]/th/string()
It can be done easily programmaticaly; here I use bash, but the logic can be used in any language of your choice :
count=$(xmllint --xpath 'count(//td[starts-with(text(), "Row")])' table.html)
for ((i=0; i<count; i++)) {
xmllint --xpath '//table/tr/th/text()' table.html
echo
}
OUTPUT :
Title One
Title One
Title One
Title One

Using xidel to extract a key-value pair

I have multiple tables on a website like so:
<table>
<tr>
<td>Name</td>
<td>foo</td>
</tr>
<tr>
<td>Count</td>
<td>15</td>
</tr>
<tr>
<td>Date</td>
<td>2014-11-17</td>
</tr>
</table>
<table>
<tr>
<td>Name</td>
<td>bar</td>
</tr>
<tr>
<td>Count</td>
<td>42</td>
</tr>
<tr>
<td>Date</td>
<td>2014-12-24</td>
</tr>
</table>
...
I want to receive something like this
foo 15
bar 42
My first attempt in xidel was xidel --xpath "//table/tr[1]/td[2]" --xpath "//table/tr[2]/td[2]" but this is giving
foo
bar
15
42
How can I extract two values in one line?
Using XPath or XQuery 3.0: //table/tbody/(tr[1]/td[2] || ' ' || tr[2]/td[2]). I think you need to request that version explicitly, at least I needed to do so on http://videlibri.sourceforge.net/cgi-bin/xidelcgi. And I parsed as HTML where the parser adds a tbody element and the path needs that too.
xidel-0.9.5.4998.exe -s --input-format=xml <input> ^
--xquery "//table/concat(tr[1]/td[2],' ',tr[2]/td[2])"
foo 15
bar 42

Angularjs, two data in a single row in table

I have a problem to implement table with thin width.
myData = { name:"Foo", age:11, sex:"M", weight:77, height:77, hobby:'gaming'}
I wanna table like belows.
<table>
<tr>
<td>name</td><td>Foo</td><td>age</td><td>11</td>
</tr>
<tr>
<td>sex</td><td>M</td><td>weight</td><td>77</td>
</tr>
<tr>
<td>height</td><td>77</td><td>hobby</td><td>gaming</td>
</tr>
</table>
Is it possible to show data like this using ngRepeat and its built-in variable?
The question John posted would solve your problem but I think it would be less of a hack to use ng-repeat-start and ng-repeat-end e.g.:
<table>
<tr ng-repeat-start="item in myData">
<td>name</td><td>{{item.name}}</td><td>age</td><td>{{item.age}}</td>
</tr>
<tr>
<td>sex</td><td>{{item.sex}}</td><td>weight</td><td>{{item.weight}}</td>
</tr>
<tr ng-repeat-end>
<td>height</td><td>{{item.height}}</td><td>hobby</td><td>{{item.hobby}}</td>
</tr>
</table>
If you have yr myData like this :
myData = [{ name:"Foo", age:11, sex:"M", weight:77, height:77, hobby:'gaming'},{ name:"Foo", age:11, sex:"M", weight:77, height:77, hobby:'gaming'},{ name:"Foo", age:11, sex:"M", weight:77, height:77, hobby:'gaming'}]
Then Your table will be like this :
<table>
<tr ng-repeat="row in myData">
<td>{{row.name}}</td>
<td>{{row.age}}</td>
<td>{{row.sex}}</td>
<td>{{row.weight}}</td>
<td>{{row.height}}</td>
<td>{{row.hobby}}</td>
</tr>
</table>

Selenium IDE: Why "Element X = Y not found" message, despite successful execution of test step?

Why does Selenium IDE give me the following error message, even when it successfully clicks on the UI button? I've tried all the available click, clickAndWait, Pause (pictured here), options I know of.
Exact Log:
[info] Executing: |click | class=button save | |
[error] Element class=button save not found
HTML:
</tr>
<tr>
<td>clickAndWait</td>
<td>id=login</td>
<td></td>
</tr>
<tr>
<td>click</td>
<td>link=Add</td>
<td></td>
</tr>
<tr>
<td>waitForElementPresent</td>
<td>class=icon-capability</td>
<td></td>
</tr>
<tr>
<td>click</td>
<td>link=Capability</td>
<td></td>
</tr>
<tr>
<td>waitForElementPresent</td>
<td>class=btn btn-primary</td>
<td></td>
</tr>
<tr>
<td>type</td>
<td>name=name</td>
<td>secondly</td>
</tr>
<tr>
<td>click</td>
<td>name=create</td>
<td></td>
</tr>
<tr>
<td>pause</td>
<td></td>
<td></td>
</tr>
<tr>
<td>click</td>
<td>class=button save</td>
<td></td>
</tr>
</tbody></table>
</body>
</html>
my guess is the selector. You are looking for *[class='button save']
If the element you are selecting is:
// doesn't match
<button id="something" class="save button"></button>
// matches
<button id="something_else" class="button save"></button>
My guess is that something dynamically is happening. Try matching on something more unique than a class. If it has an ID attribute, use that. If it doesn't have that and it has a name attribute, use that.
If it doesn't have anything to match on BUT the class, then try using CSS.
css=button.button.save