# Page.waitForSelector(selector: str, options: dict = None, **kwargs) → Awaitable[T_co] # 等待与选择器匹配的元素出现,要是页面已经有了匹配的元素,那么会立即返回 # Returns: Return awaitable object which resolves when element specified by selector string is added to DOM. # 参数: # selector (str) – 选择器 # 可选项: # visible (bool):等待元素在DOM中出现,并且可见; i.e. to not have display: none or visibility: hidden CSS properties. Defaults to False. # hidden (bool): 等待元素在DOM中出现,隐藏的都算, i.e. have display: none or visibility: hidden CSS properties. Defaults to False. # timeout (int|float): 最大等待时间(毫秒).默认为30000 (30 秒). 传递0禁用此项.
# Page.addScriptTag(options: Dict[KT, VT] = None, **kwargs) → pyppeteer.element_handle.ElementHandle # 给页面添加一个script标签,可以写入一些东西 # url, path, content三个参数中必须选一个填入 # 会返回已经被添加好的 ElementHandle 实例 # 参数: # url (string): URL of a script to add. # path (string): Path to the local JavaScript file to add. # content (string): JavaScript string to add. # type (string): Script type. Use module in order to load a JavaScript ES6 module await page.addScriptTag({ "content": "console.log('Hello addScriptTag')" })
# Page.addStyleTag(options: Dict[KT, VT] = None, **kwargs) → pyppeteer.element_handle.ElementHandle # 给页面添加一个style标签,可以写入一些东西 # url, path, content三个参数中必须选一个填入 # 参数: # url (string): URL of the link tag to add. # path (string): Path to the local CSS file to add. # content (string): CSS string to add. await page.addScriptTag({ "url": "https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.0.0-alpha1/css/bootstrap.css" })
# Page.click(selector: str, options: dict = None, **kwargs) # 此方法使用选择器获取元素,如果需要,将其滚动到视图中,然后使用鼠标在元素的中心单击 # 如果没有元素匹配选择器,该方法将引发PageError # 参数 # button (str): left, right, or middle, defaults to left. # clickCount (int): defaults to 1. # delay (int|float): Time to wait between mousedown and mouseup in milliseconds. defaults to 0. await page.click('#su')
# Page.setUserAgent(userAgent: str) # 设置UserAgent await page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36')
# Page.setViewport(viewport: dict) # 设置Viewport # 可填入选项: # width (int): page width in pixel. # height (int): page height in pixel. # deviceScaleFactor (float): Default to 1.0. # isMobile (bool): Default to False. # hasTouch (bool): Default to False. # isLandscape (bool): Default to False. await page.setViewport({ 'width': 1024, 'height': 1024, })
# Page.emulateMedia(mediaType: str = None) # 设置CSS media type # 你可以填入 'screen', 'print', None 中的一个 await page.emulateMedia('screen')
# Page.exposeFunction(name: str, pyppeteerFunction: Callable[[…], Any]) # 将一个Python函数绑定到浏览器window对象中,可以在使用过程中调用 # 参数: # name (string) – Name of the function on the window object. # pyppeteerFunction (Callable) – Function which will be called on python process. This function should not be asynchronous function.
# metrics() → Dict[str, Any] # 返回页面 metrics 信息 # 信息批注 # Timestamp (number): The timestamp when the metrics sample was taken. # Documents (int): Number of documents in the page. # Frames (int): Number of frames in the page. # JSEventListeners (int): Number of events in the page. # Nodes (int): Number of DOM nodes in the page. # LayoutCount (int): Total number of full partial page layout. # RecalcStyleCount (int): Total number of page style recalculations. # LayoutDuration (int): Combined duration of page duration. # RecalcStyleDuration (int): Combined duration of all page style recalculations. # ScriptDuration (int): Combined duration of JavaScript execution. # TaskDuration (int): Combined duration of all tasks performed by the browser. # JSHeapUsedSize (float): Used JavaScript heap size. # JSHeapTotalSize (float): Total JavaScript heap size. c = await page.metrics() print(c)
# Page.screenshot(options: dict = None, **kwargs) → Union[bytes, str] # 给网页弄一张截图 # 参数: # path (str): The file path to save the image to. The screenshot type will be inferred from the file extension. # type (str): Specify screenshot type, can be either jpeg or png. Defaults to png. # quality (int): The quality of the image, between 0-100. Not applicable to png image. # fullPage (bool): When true, take a screenshot of the full scrollable page. Defaults to False. # clip (dict): An object which specifies clipping region of the page. This option should have the following fields: # x (int): x-coordinate of top-left corner of clip area. # y (int): y-coordinate of top-left corner of clip area. # width (int): width of clipping area. # height (int): height of clipping area. # omitBackground (bool): Hide default white background and allow capturing screenshot with transparency. # encoding (str): The encoding of the image, can be either 'base64' or 'binary'. Defaults to 'binary'. await page.screenshot({'path': 'example/0_Basic_usage_of_the_library/pyppeteer/baidu.png'})