简体   繁体   中英

how can i get an attribute value of all links on a page?

i'm trying to get a list of values of 'data-ctorig' property on all links marked with 'gs-title' class on this page.

i can get one the value of first link document.querySelector('a.gs-title').getAttribute('data-ctorig') but can have others. also, i can get a list of nodelist (idk what is this) using document.querySelectorAll('a.gs-title') but i dont know how can i get a list of attribute values based in this node list.

also i've tried use :nth-of-type() but i got only null after first value

var x = document.querySelector("a.gs-title:nth-of-type(1)").getAttribute("data-ctorig")

i'm also using python with selenium webdriver to do this so if someone know how to do it on python 'll help me on the same way.

You meant this? :)

 var links = document.querySelectorAll('a.gs-title'); var list = []; links.forEach((link) => { list.push(link.getAttribute('data-ctorig')); }); console.log(list); 
 <a class="gs-title" data-ctorig="Test1"><b>AMANDA</b> MIZUKAMI</a> <a class="gs-title" data-ctorig="Test2"><b>AMANDA</b> MIZUKAMI</a> <a class="gs-title" data-ctorig="Test3"><b>AMANDA</b> MIZUKAMI</a> 

First use querySelectorAll to get all the .gs-title nodes.

var gstitles = document.querySelectorAll('a.gs-title')

You can use gstitles.length now to determine how many there are.

gstitles.length;

Next we need to setup a for loop. Check out the code snippet below.

for (i=0;i<gstitles.length;i++) 
{
console.log(gstitles[i].getAttribute('data-ctorig'));
}

 var gstitles = document.querySelectorAll('a.gs-title') for (i=0;i<gstitles.length;i++) { console.log(gstitles[i].getAttribute('data-ctorig')); } 
 <div class="gs-title"><a class="gs-title" href="https://www.teses.usp.br/teses/disponiveis/27/27151/tde-19032008-183924/publico/AmandaTojal.pdf" target="_blank" dir="ltr" data-cturl="https://www.google.com/url?q=https://www.teses.usp.br/teses/disponiveis/27/27151/tde-19032008-183924/publico/AmandaTojal.pdf&amp;sa=U&amp;ved=0ahUKEwiWo7TB3-bhAhUF16wKHaWeCXoQFggEMAA&amp;client=internal-uds-cse&amp;cx=011662445380875560067:cack5lsxley&amp;usg=AOvVaw2g3t_0fFH8wjhfjcku0DL3" data-ctorig="https://www.teses.usp.br/teses/disponiveis/27/27151/tde-19032008-183924/publico/AmandaTojal.pdf"><b>AMANDA</b> PINTO DA FONSECA TOJAL</a></div><div class="gs-title gsc-table-cell-thumbnail gsc-thumbnail-left"><a class="gs-title" href="https://www.teses.usp.br/teses/disponiveis/27/27151/tde-19032008-183924/publico/AmandaTojal.pdf" target="_blank" dir="ltr" data-cturl="https://www.google.com/url?q=https://www.teses.usp.br/teses/disponiveis/27/27151/tde-19032008-183924/publico/AmandaTojal.pdf&amp;sa=U&amp;ved=0ahUKEwiWo7TB3-bhAhUF16wKHaWeCXoQFggEMAA&amp;client=internal-uds-cse&amp;cx=011662445380875560067:cack5lsxley&amp;usg=AOvVaw2g3t_0fFH8wjhfjcku0DL3" data-ctorig="https://www.teses.usp.br/teses/disponiveis/27/27151/tde-19032008-183924/publico/AmandaTojal.pdf"><b>AMANDA</b> PINTO DA FONSECA TOJAL</a></div><div class="gs-title"><a class="gs-title" href="https://www.google.com/url?q=http://www.teses.usp.br/teses/disponiveis/17/17153/tde-06012017-103806/publico/AmandaMizukamiDOCorrig.pdf&amp;sa=U&amp;ved=0ahUKEwiWo7TB3-bhAhUF16wKHaWeCXoQFggGMAE&amp;client=internal-uds-cse&amp;cx=011662445380875560067:cack5lsxley&amp;usg=AOvVaw0Jdjapa8W60DfKRyUIAdoH" target="_blank" dir="ltr" data-cturl="https://www.google.com/url?q=http://www.teses.usp.br/teses/disponiveis/17/17153/tde-06012017-103806/publico/AmandaMizukamiDOCorrig.pdf&amp;sa=U&amp;ved=0ahUKEwiWo7TB3-bhAhUF16wKHaWeCXoQFggGMAE&amp;client=internal-uds-cse&amp;cx=011662445380875560067:cack5lsxley&amp;usg=AOvVaw0Jdjapa8W60DfKRyUIAdoH" data-ctorig="http://www.teses.usp.br/teses/disponiveis/17/17153/tde-06012017-103806/publico/AmandaMizukamiDOCorrig.pdf"><b>AMANDA</b> MIZUKAMI</a></div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM