使用javascript从chrome控制台抓取网站

使用javascript从chrome控制台抓取网站,javascript,browser,console,wait,Javascript,Browser,Console,Wait,我正试图抓取一个包含1000个元素的表格的网页。每当单击表中的元素时,网页都会更新标记。从chrome控制台,我想点击每个元素,等待标签更新并下载标签。目前我正在做以下工作: for(i = 0; i < 1000; i++){ document.querySelectorAll('element_in table')[i].click() text = document.querySelector('tag_to_read_from').innerHTML // downlo

我正试图抓取一个包含1000个元素的表格的网页。每当单击表中的元素时,网页都会更新标记。从chrome控制台,我想点击每个元素,等待标签更新并下载标签。目前我正在做以下工作:

for(i = 0; i < 1000; i++){
  document.querySelectorAll('element_in table')[i].click()
  text = document.querySelector('tag_to_read_from').innerHTML
  // download text
}
(i=0;i<1000;i++)的
{
document.querySelectorAll('element_in table')[i]。单击()
text=document.querySelector('tag_to_read_from')。innerHTML
//下载文本
}
问题在于,单击表中的元素后,更新标记时会出现延迟。因此,由于javascript的异步特性,该脚本正在下载1000个空文件

是否有办法在单击某个元素后等待X秒,然后下载更新的标记?

函数scrape(index,max){
function scrape (index, max) {
  document.querySelectorAll('element_in table')[index].click();
  
  setTimeout(() => {
    // download text

    if (index < max) scrape(++index, max);
  }, 5000);
}

scrape(0, 1000);
document.querySelectorAll('element_in table')[index]。单击(); 设置超时(()=>{ //下载文本 如果(指数<最大值)刮伤(++指数,最大值); }, 5000); } 刮伤(0,1000);

您可以使用超时来延迟逻辑。只需选择一个足够长的时间。

setTimeout
是等待一段时间的好方法。因为这是您在浏览器控制台中执行的操作,所以您不必比这复杂得多。然而,您必须重新购买如何执行循环,尽管您希望使用它来等待“标记正在更新”。