单击操纵符中的某个元素后,如何等待网络空闲?

时间:2019-01-26 10:48:08

标签: javascript node.js puppeteer

单击伪造者中的元素后,如何等待网络空闲?

const browser = await puppeteer.launch({headless: false});
await page.goto(url, {waitUntil: 'networkidle'});

await page.click('.to_cart'); //Click on element trigger ajax request
//Now I need wait network idle(Wait for the request complete)
await page.click('.to_cart');

UPD:单击元素后无导航

3 个答案:

答案 0 :(得分:1)

木偶戏中有两种方法。

示例:

await promise.all([page.waitForRequest(callback), page.waitForResponse(callback)])

答案 1 :(得分:0)

直接来自the docs

  

此问题在页面导航到新URL或重新加载时解决。当您运行将间接导致页面导航的代码时,它很有用。考虑以下示例:

const [response] = await Promise.all([
  page.waitForNavigation(), // The promise resolves after navigation has finished
  page.click('a.my-link'), // Clicking the link will indirectly cause a navigation
]);

答案 2 :(得分:0)

我遇到了类似的问题,并且在Puppeteer Control networkidle等待时间问题上找到了解决方法功能,以满足我的需要: https://github.com/GoogleChrome/puppeteer/issues/1353#issuecomment-356561654

基本上,您可以创建一个自定义函数,并在执行任何其他步骤之前调用它:

# import required libraries
import bs4, requests, re

# Get the HTML and store it in req variable
req = requests.get('Website')

# Make a BS object to help search HTML
reqSoup = bs4.BeautifulSoup(req.text, 'lxml')

# Search the ATIS under the 'pre' label and store it in reqSoupElems                     
reqSoupElems = reqSoup.select('pre')

# Create Regex's to search the ATIS
timeRegex = re.compile(r'\d\d\d\d\d\d')
WXRegex = re.compile(r'WX:\s.*')

# Create empty lists to store the data extracted for the ATIS
time = []
WX = []

# Create list with strings of time and weather elements extracted from ATIS
for i in range(len(reqSoupElems)):
    # Create time list
    time.append(timeRegex.search(str(reqSoupElems[i])).group())        
    # If the wx group isn't in ATIS append "Nill WX" to WX list
    # Otherwise, append the weather in ATIS to WX list
    if WXRegex.findall(str(reqSoupElems[i])) == []:
        WX.append('Nil WX')
    else:
        WX.append(WXRegex.search(str(reqSoupElems[i])).group())

# Remove "WX: " from the strings within the lists       
for i in range(len(WX)):
    if WX[i][0:4] == 'WX: ':
        WX[i] = WX[i][4:]

# Print to ensure it has worked
print('\n')
print(time)
print(len(time))
print('\n')
print(WX)
print(len(WX))