GerCas
June 25, 2019, 4:56pm
1
Hello to all,
I have some values in a table that is in a iframe that I want to store in a variable.
The xpath of the values is like this:
xpath=//*[@id="888393_1234_993_11"]/td[2]
When I tried the complete xpath I get “0” as result. If I tried adding “.*” after “888393” then the result is more than 6000.
Command | Target | Value | Result
SourceSearch | regex=//*[@id="888393_1234_993_11"]/td[2] | var |
echo | ${var} | | 0
SourceSearch | regex=//*[@id="888393_.*td[2] | var |
echo | ${var} | | 6202
How would be the correct regex to match all the values in table and store in ${var} the number of these elements?
Thanks for any help.
hi
Is many solution
You have selectFrame command selectFrame - Selenium IDE Commands Tutorial
I prefer
command executeScript executeScript, execute script, Arrays - Selenium IDE Commands Tutorial
and
javascript - Get element from within an iFrame - Stack Overflow
where in value commad place
return document.getElementById('888393_1234_993_11').contentWindow.document.querySelector('yours table')
GerCas
June 25, 2019, 8:44pm
3
Hi Emil,
Thanks for your answer. But I don’t know the values in table, so first I need I way to know previously which values are in column 2.
If value 1234 is in table, then the xpath contains 1234 within it. Because of that I was trying with Regex in order to check if I was able to catch all values and store them in a variable to use them later.
I hope make sense.
Thanks
Ok you must looping on table rows
Example
return [...document.getElementById('iframe_id').contentWindow.document.querySelector('your_tbody').querySelectorAll(tr)].map(x => x.querySelector('td')[nb_of_col].innerText)```
expected Array
[val_row_0_td_xyz_value,val_row_1_td_xyz_value,val_row_2_td_xyz_value...]
1 Like
GerCas
June 26, 2019, 6:56pm
5
Emil_Szymecki:
‘iframe_id’
Hi Emil,
Thanks for the help. I’m new to Javascript and I wasn’t able to fix the issue trying your example.
If I try this part of your command in Console of Developer tools in Chrome.
document.getElementById('iframe_id').contentWindow.document.querySelector('tbody').querySelectorAll(tr);
I get this error
VM12695:1 Uncaught ReferenceError: tr is not defined
at <anonymous>:1:104
Now if I try only this part of the command
document.getElementById('iframe_id').contentWindow.document.querySelector('tbody');
I get this html block:
<tbody>
<tr><td><table class="class1"..>
<tbody><tr><th class="left"> </th>
<table width="100%"..>
<tbody>
...
</tbody>
</table></td></tr>
</tbody></table>
<table border="0" ...>
<tbody>
...
</tbody>
</table>
</tr>
</tbody>
.
.
.
<form name="MyForm">
<table class="MyTable" ..>
<tbody>
<tr>
<th width="15"> </th>
</tr>
<tr class="tbl-x" id="1234_992_120_24" onmouseup="..">
<td width="10">
<input type="checkbox" name="Item12340" onclick="javascript:.._item('77373','992','abc')">
</td>
<td align="right">
<a href="javascript:...('77373','992','abc');.._Open('1234','NS');">992</a>
</td>
</tr>
</tbody>
</table>
</form>
</tbody>
So, the values I’m looking for are in 2nd column of the table “MyTable” that is inside the form “MyForm”. In this sample showing only 2 “tr’s” and only 2 “td’s” in second “tr” the value I’d like would be 992. This value appears in tr’s id, within the “onclick” and within the < “a href=”…992…“>992</a” >
How woudl be the way to the get all the values for column 2 of table MyTable?
Thanks for the help so far.
hmm
maybe
return document.getElementById('iframe_id').contentWindow.document.querySelector('copied js path <- anchor have 992').innerText
GerCas
June 26, 2019, 8:07pm
7
I copied the JS Path and this command gets the value I want. The JS Path is like this:
#\\45 00340_992_120_24 > td:nth-child(2) > a
And command that works is:
document.getElementById('iframe_id').contentWindow.document.querySelector("#\\45 00340_992_120_24 > td:nth-child(2) > a").innerText;
"992"
But in this case I need to know previously the JS path and I’d like to store in an array or variable all values in column 2 of that table automatically without to know previously the values or JS paths for each one. Maybe something like mixing your first solution with the code that works or is possible to add a regex? I don’t know.
Thanks
return […document.getElementById('iframe_id').contentWindow.document.querySelector('.MyTable > tbody').querySelectorAll(tr)].slice(1).map(x => x.querySelector('td:nth-child(2) > a').innerText)
or
return […document.getElementById('iframe_id').contentWindow.document.querySelector('.MyTable > tbody').querySelectorAll(tr)].map(x => x.querySelector('td:nth-child(2) > a').innerText)
I can’t see this table this is problem.
GerCas
June 26, 2019, 9:07pm
9
I’m not sure why is not working, trying your commands I get
VM36857:1 Uncaught TypeError: Cannot read property 'contentWindow' of null
at <anonymous>:1:41
This is how it looks the real file if could help.
Thanks again
Do you have selected top iframe ?
GerCas
June 26, 2019, 9:43pm
11
Yes, top iframe is selected. Why?