1
I’m making a script that collects the data of students that are displayed only on an HTML page.
Tags are not identified by id, and had to collect everything in a very inappropriate way (searching for the tag <strong>
for example). However, after searching for the data on the page function buscaDados()
, the script does not continue to the next function which is the listaDados()
.
I couldn’t identify the mistake.
Here the HTML:
<table><tbody><tr><td>
Matricula: 1011<br> Situação <br><strong>INATIVO</strong>
</td><td>
<table><tbody><tr>
<td>Nome<br><strong>Fábio Gonçalves</strong></td>
</tr><tr><td>Rua das Amoras, 515<br><strong><span style="color:#28858C;">Não possui veículo cadastrado</span> </strong></td>
</tr></tbody></table></td>
<td valign="top"><table><tbody><tr>
<td>Email<br><strong>Divulgação não autorizada</strong></td></tr><tr>
<td>Nascimento<br><strong>Divulgação não autorizada </strong></td></tr>
</tbody></table></td></tr>
<tr><td><strong>Ativo desde 01/03/2014</strong></td></tr></tbody></table>
And the script:
<script>
var tabela = [];
var i = 0;
var indiceCRM = 0;
var qtdeCadastros = 0;
var posicao = 0;
var todoHtml = document.getElementsByTagName("html")[0].innerHTML;
var j = 0;
var html = "<table>";
buscaDados();
listaDados();
function pegaCRM(posicao){
posicao = todoHtml.indexOf("CRM:");
var texto = todoHtml.substr(posicao, 11);
tabela.push(texto);
todoHtml = todoHtml.substr(posicao+11,10000000);//Considerando matrículas com 11 Caracteres
posicao = posicao+11;
return posicao;
}
function verificaMesmoCadastro(verificador){
if(!verificador){var verificador = true;
var verifica = document.getElementsByTagName("strong")[i+1].innerText;
if(verifica ==="ATIVO"){verificador = false;}
else if(verifica==="INATIVO"){verificador = false;}
return verificador;}
if(verificador){return true;}}
function buscaDados(){
while(qtdeCadastros<=10){
var status = document.getElementsByTagName("strong")[i].innerText;
tabela.push(status);
console.log(status);
posicao = pegaCRM(posicao);
while(verificaMesmoCadastro(false)){
var dado = document.getElementsByTagName("strong")[i+1].innerText;
console.log(dado);
tabela.push(dado);
i++;
}
if(!verificaMesmoCadastro(false))i++;
console.log(qtdeCadastros);
qtdeCadastros++;
}
}
function listaDados(){
for(reg=0;reg<10;reg++){
html+="<tr>";
while(tabela(j+1)==="ATIVO"||tabela[j+1]==="INATIVO"){
html+="<td>"+tabela[j]+"</td>";
j++;
}
html+="</tr>";
j++;
}
html+="</table>";
document.getElementById("tabela").innerHTML=html;
}
</script>
Something wrong with the console? I think
while(verificaMesmoCadastro(false)){
medium dangerous, easy to have an error and be difficult to debug. What do you want to do with that line?– Sergio
It appears that you have an error in: var checks = Document.getelementsbytagname("Strong")[i+1]. innerText; Error: Uncaught Typeerror: Cannot read Property 'innerText' of Undefined. I want while to be executed only when the value searched for in html is NOT "ACTIVE" or "INACTIVE", because when this value appears, it means that the data of the next student started.
– Matheus Godoi
This error is because i+1 ta giving a position number in the array that does not exist.
– Ricardo
There are a lot of errors in that code, you needed to go through a Eslint. This line
if(!verificador){var verificador = true;
is re-declaring the variable. Is that what you want? Something else: callwhile(verificaMesmoCadastro(false)){
will always doverificaMesmoCadastro()
return true, and create an infinite loop. Create an example that shows the problem in jsFiddle, or the Stackoverflow widget to make it clearer.– Sergio