Best Python code snippet using playwright-python
_interaction.py
Source:_interaction.py
...39 åªéè¦å¨ç¹æ®æ
åµä¸ä½¿ç¨æ¤é项ï¼ä¾å¦å¯¼èªå°æ æ³è®¿é®ç页é¢ã40 é»è®¤ä¸º falseã41 :param position: ç¸å¯¹äºå
ç´ å¡«å
æ¡çå·¦ä¸è§ä½¿ç¨çç¹ã å¦ææªæå®ï¼å使ç¨å
ç´ çä¸äºå¯è§ç¹ã42 :param timeout: 以毫ç§ä¸ºåä½çæé¿æ¶é´ï¼é»è®¤ä¸º 30 ç§ï¼ä¼ é 0 以ç¦ç¨è¶
æ¶ãå¯ä»¥ä½¿ç¨43 browser_context.set_default_timeout(timeout)44 æ page.set_default_timeout(timeout) æ¹æ³æ´æ¹é»è®¤å¼ã45 """46 element = self._find_element_cross_frame(selector)47 element.check(48 force=force,49 no_wait_after=no_wait_after,50 position=position,51 timeout=timeout52 )53 def click(54 self,55 selector: str,56 *,57 button: Literal["left", "middle", "right"] = None,58 click_count: int = None,59 delay: float = None,60 force: bool = None,61 modifiers: Optional[62 List[Literal["Alt", "Control", "Meta", "Shift"]]63 ] = None,64 no_wait_after: bool = None,65 position: Position = None,66 timeout: float = None,67 ) -> NoneType:68 """æ¤æ¹æ³åå»å¹é
éæ©å¨çå
ç´ ã69 :param selector: ç¨äºæç´¢å
ç´ çéæ©å¨ã å¦ææå¤ä¸ªå
ç´ æ»¡è¶³éæ©å¨ï¼å°ä½¿ç¨ç¬¬ä¸ä¸ªã70 :param button: é¼ æ çå·¦ãä¸ï¼æ»è½®ï¼ãå³æé®ãé»è®¤ä¸ºå·¦ã71 :param click_count: é»è®¤ä¸º 1ã72 :param delay: å¨ mousedown å mouseup ä¹é´çå¾
çæ¶é´ï¼ä»¥æ¯«ç§ä¸ºåä½ï¼ã é»è®¤ä¸º 0ã73 :param force: æ¯å¦ç»è¿å¯æä½æ§æ£æ¥ã é»è®¤ä¸º falseã74 :param modifiers: è¦æä¸ç修饰é®ã ç¡®ä¿å¨æä½æé´ä»
æä¸è¿äºä¿®é¥°ç¬¦ï¼ç¶åå°å½å修饰符æ¢å¤åæ¥ã å¦ææªæå®ï¼å使ç¨å½åæä¸ç修饰符ã75 :param no_wait_after: å¯å¨å¯¼èªçæä½æ£å¨çå¾
è¿äºå¯¼èªåç并çå¾
页é¢å¼å§å è½½ã ä½ å¯ä»¥éè¿è®¾ç½®æ¤æ å¿éæ©éåºçå¾
ã76 åªéè¦å¨ç¹æ®æ
åµä¸ä½¿ç¨æ¤é项ï¼ä¾å¦å¯¼èªå°æ æ³è®¿é®ç页é¢ã é»è®¤ä¸º falseã77 :param position: ç¸å¯¹äºå
ç´ å¡«å
æ¡çå·¦ä¸è§ä½¿ç¨çç¹ã å¦ææªæå®ï¼å使ç¨å
ç´ çä¸äºå¯è§ç¹ã78 x <float>79 y <float>80 :param timeout: 以毫ç§ä¸ºåä½çæé¿æ¶é´ï¼é»è®¤ä¸º 30 ç§ï¼ä¼ é 0 以ç¦ç¨è¶
æ¶ã81 å¯ä»¥ä½¿ç¨ browser_context.set_default_timeout(timeout) æ page.set_default_timeout(timeout) æ¹æ³æ´æ¹é»è®¤å¼ã82 """83 element = self._find_element_cross_frame(selector)84 element.click(85 button=button,86 click_count=click_count,87 delay=delay,88 force=force,89 modifiers=modifiers,90 no_wait_after=no_wait_after,91 position=position,92 timeout=timeout93 )94 def cell_inner_text(self, *, row_header: str = None, column_headers: List[str] = None):95 """æ ¹æ®åæ é¢ `column_headers` åè¡æ é¢ `row_header` è·å¾ææ¬å¼ã96 ä»
æä¾è¡æ é¢ `row_header` æ¶ï¼å°è·å¾å
¶å³ä¾§æè¿çä¸ä¸ªå段çææ¬å¼ã97 :param column_headers: åæ é¢98 :param row_header: è¡æ é¢99 """100 return self.get_table_cell(row_header=row_header, column_headers=column_headers).inner_text()101 def cell_input_value(self, *, row_header: str = None, column_headers: List[str] = None):102 """æ ¹æ®åæ é¢ `column_headers` åè¡æ é¢ `row_header` è·å¾ <input> æ <textarea> æ <select> ç `value` å±æ§å¼ã103 ä»
æä¾è¡æ é¢ `row_header` æ¶ï¼å°è·å¾å
¶å³ä¾§æè¿çä¸ä¸ªè¾å
¥å段ç `value` å±æ§å¼ã104 对äºéè¾å
¥å
ç´ æåºå¼å¸¸ã105 :param column_headers: åæ é¢106 :param row_header: è¡æ é¢107 """108 _el = self.get_table_cell(row_header=row_header, column_headers=column_headers)109 tag_name = str(_el.get_property("tagName")).lower()110 if tag_name not in ["input", "textarea", "select"]:111 raise Error(112 "cell_input_value ä»
ä½ç¨äº <input>|<textarea>|<select> å
ç´ ï¼"113 f"ä¸æ¯æ <{tag_name}>ã")114 return _el.get_attribute("value")115 def dblclick(116 self,117 selector: str,118 *,119 modifiers: Optional[120 List[Literal["Alt", "Control", "Meta", "Shift"]]121 ] = None,122 position: Position = None,123 delay: float = None,124 button: Literal["left", "middle", "right"] = None,125 timeout: float = None,126 force: bool = None,127 no_wait_after: bool = None,128 ) -> NoneType:129 """åå»å¹é
éæ©å¨çå
ç´ ã130 :param selector: ç¨äºæç´¢å
ç´ çéæ©å¨ã å¦ææå¤ä¸ªå
ç´ æ»¡è¶³éæ©å¨ï¼å°ä½¿ç¨ç¬¬ä¸ä¸ªã131 :param button: é¼ æ çå·¦ãä¸ï¼æ»è½®ï¼ãå³æé®ãé»è®¤ä¸ºå·¦ã132 :param delay: å¨ mousedown å mouseup ä¹é´çå¾
çæ¶é´ï¼ä»¥æ¯«ç§ä¸ºåä½ï¼ã é»è®¤ä¸º 0ã133 :param force: æ¯å¦ç»è¿å¯æä½æ§æ£æ¥ã é»è®¤ä¸º falseã134 :param modifiers: è¦æä¸ç修饰é®ã ç¡®ä¿å¨æä½æé´ä»
æä¸è¿äºä¿®é¥°ç¬¦ï¼ç¶åå°å½å修饰符æ¢å¤åæ¥ã å¦ææªæå®ï¼å使ç¨å½åæä¸ç修饰符ã135 :param no_wait_after: å¯å¨å¯¼èªçæä½æ£å¨çå¾
è¿äºå¯¼èªåç并çå¾
页é¢å¼å§å è½½ã ä½ å¯ä»¥éè¿è®¾ç½®æ¤æ å¿éæ©éåºçå¾
ã136 åªéè¦å¨ç¹æ®æ
åµä¸ä½¿ç¨æ¤é项ï¼ä¾å¦å¯¼èªå°æ æ³è®¿é®ç页é¢ã é»è®¤ä¸º falseã137 :param position: ç¸å¯¹äºå
ç´ å¡«å
æ¡çå·¦ä¸è§ä½¿ç¨çç¹ã å¦ææªæå®ï¼å使ç¨å
ç´ çä¸äºå¯è§ç¹ã138 x <float>139 y <float>140 :param timeout: 以毫ç§ä¸ºåä½çæé¿æ¶é´ï¼é»è®¤ä¸º 30 ç§ï¼ä¼ é 0 以ç¦ç¨è¶
æ¶ã141 å¯ä»¥ä½¿ç¨ browser_context.set_default_timeout(timeout) æ page.set_default_timeout(timeout) æ¹æ³æ´æ¹é»è®¤å¼ã142 """143 element = self._find_element_cross_frame(selector)144 element.dblclick(145 selector=selector,146 modifiers=modifiers,147 position=position,148 delay=delay,149 button=button,150 timeout=timeout,151 force=force,152 no_wait_after=no_wait_after,153 )154 def dispatch_event(155 self,156 selector: str,157 *,158 event_type: str = None,159 event_init: Dict = None,160 ) -> NoneType:161 """触åäºä»¶ã162 ä¸é¢ç代ç ç段å¨å
ç´ ä¸è°åº¦ç¹å»äºä»¶ã163 æ 论å
ç´ çå¯è§æ§ç¶æå¦ä½ï¼ç¹å»äºä»¶é½ä¼è¢«è°åº¦ã164 è¿çåäºè°ç¨165 [element.click()](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/click)ã166 ```py167 page.dispatch_event(\"button#submit\", \"click\")168 ```169 å¨åå°ï¼å®æ ¹æ®ç»å®ç `type` å建ä¸ä¸ªäºä»¶å®ä¾ï¼å¹¶ä½¿ç¨ `eventInit` å±æ§å¯¹å
¶è¿è¡åå§å170 并å¨å
ç´ ä¸è°åº¦å®ãäºä»¶é»è®¤ä¸ºâå·²ç»åâãâå¯åæ¶âåâå泡âã171 å¦æä½ å¸æå°æ´»å¨å¯¹è±¡ä¼ éå°äºä»¶ä¸ï¼è¿å¯ä»¥æå® `JSHandle` ä½ä¸ºå±æ§å¼ï¼172 ```py173 # 请注æï¼åªè½å¨ Chromium å firefox ä¸å建 data_transfer174 data_transfer = page.evaluate_handle(\"new DataTransfer()\")175 page.dispatch_event(\"#source\", \"dragstart\", { \"dataTransfer\": data_transfer })176 ```177 :param selector: ç¨äºæç´¢å
ç´ çéæ©å¨ã å¦ææå¤ä¸ªå
ç´ æ»¡è¶³éæ©å¨ï¼å°ä½¿ç¨ç¬¬ä¸ä¸ªã178 :param event_type: DOM äºä»¶ç±»åï¼âclickâãâdragstartâçã179 :param event_init: å¯éçç¹å®äºäºä»¶çåå§åå±æ§ã180 """181 element = self._find_element_cross_frame(selector)182 element.dispatch_event(183 type=event_type,184 event_init=event_init,185 )186 def drag_and_drop(187 self,188 source: str,189 target: str,190 source_position: Position = None,191 target_position: Position = None,192 timeout: float = None,193 ):194 """æ§è¡ä» `selector_from` éæ©çå
ç´ å° `selector_to` éæ©çå
ç´ çææ¾æä½ã195 该æ¹æ³ä¸æ¯æè·¨Frameæç´¢å
ç´ 196 :param source: èµ·ç¹å
ç´ 197 :param target: ç»ç¹å
ç´ 198 :param source_position: å¨è¯¥ç¹ç¸å¯¹äºå
ç´ çå¡«å
æ¡çå·¦ä¸è§åå»æºå
ç´ ã å¦ææªæå®ï¼å使ç¨å
ç´ çæäºå¯è§ç¹ã199 :param target_position: æ¤æ¶ç¸å¯¹äºå
ç´ å¡«å
æ¡çå·¦ä¸è§è½å¨ç®æ å
ç´ ä¸ã å¦ææªæå®ï¼å使ç¨å
ç´ çæäºå¯è§ç¹ã200 :param timeout: 以毫ç§ä¸ºåä½çæé¿æ¶é´ï¼é»è®¤ä¸º 30 ç§ï¼ä¼ é 0 以ç¦ç¨è¶
æ¶ãå¯ä»¥ä½¿ç¨201 browser_context.set_default_timeout(timeout)202 æ page.set_default_timeout(timeout) æ¹æ³æ´æ¹é»è®¤å¼ã203 """204 if ">>>" in source or ">>>" in target:205 unsupported_selector_engine = Error("该æ¹æ³ä¸æ¯æè·¨ Frame æç´¢è¯æ³ã")206 raise unsupported_selector_engine207 self._obj.drag_and_drop(208 source=source,209 target=target,210 source_position=source_position,211 target_position=target_position,212 timeout=timeout,213 )214 def fill(215 self,216 selector: str,217 value: str,218 *,219 no_wait_after: bool = None,220 timeout: float = None,221 clear: bool = True,222 ) -> NoneType:223 """æ¸
空 `selector` æ¾å°çææ¬å段ï¼ç¶åä½¿ç¨ `value` å¡«å
å®ã224 æ¤æ¹æ³çå¾
å
ç´ å¹é
éæ©å¨ï¼çå¾
å¯æä½æ§æ£æ¥ï¼èç¦å
ç´ ï¼å¡«å
å®å¹¶å¨å¡«å
å触åè¾å
¥äºä»¶ã225 å¦æå¹é
éæ©å¨çå
ç´ ä¸æ¯ input ãtextarea æ contenteditable å
ç´ ï¼åæ¤æ¹æ³ä¼å¼åé误ã226 :param selector: ç¨äºæç´¢å
ç´ çéæ©å¨ã å¦ææå¤ä¸ªå
ç´ æ»¡è¶³éæ©å¨ï¼å°ä½¿ç¨ç¬¬ä¸ä¸ªã227 :param value: 为 input ãtextarea æ contenteditable å
ç´ å¡«å
çå¼ã228 :param no_wait_after: å¯å¨å¯¼èªçæä½æ£å¨çå¾
è¿äºå¯¼èªåç并çå¾
页é¢å¼å§å è½½ã229 å¯ä»¥éè¿è®¾ç½®æ¤æ å¿éæ©éåºçå¾
ã230 åªéè¦å¨ç¹æ®æ
åµä¸ä½¿ç¨æ¤é项ï¼ä¾å¦å¯¼èªå°æ æ³è®¿é®ç页é¢ã231 é»è®¤ä¸º falseã232 :param timeout: 以毫ç§ä¸ºåä½çæé¿æ¶é´ï¼é»è®¤ä¸º 30 ç§ï¼ä¼ é 0 以ç¦ç¨è¶
æ¶ãå¯ä»¥ä½¿ç¨233 browser_context.set_default_timeout(timeout)234 æ page.set_default_timeout(timeout) æ¹æ³æ´æ¹é»è®¤å¼ã235 :param clear: å¦æå¨å¡«å
ä¹åä¸åºæ¸
é¤è¯¥å段ï¼å设置为 falseã é»è®¤ä¸º trueã236 """237 element = self._find_element_cross_frame(selector)238 if clear:239 # æ¸
空240 element.fill(241 value='',242 force=True,243 no_wait_after=no_wait_after,244 timeout=timeout,245 )246 # å¡«å
247 element.fill(248 value=value,249 force=True,250 no_wait_after=no_wait_after,251 timeout=0,252 )253 def focus(self, selector: str):254 """æ¤æ¹æ³ä½¿ç¨éæ©å¨ `selector` è·åå
ç´ å¹¶èç¦å®ã255 å¦æ没æä¸éæ©å¨å¹é
çå
ç´ ï¼è¯¥æ¹æ³å°çå¾
å¹é
å
ç´ åºç°å¨ DOM ä¸ã256 :param selector: ç¨äºæç´¢å
ç´ çéæ©å¨ã å¦ææå¤ä¸ªå
ç´ æ»¡è¶³éæ©å¨ï¼å°ä½¿ç¨ç¬¬ä¸ä¸ªã257 """258 element = self._find_element_cross_frame(selector)259 element.focus()260 def get_attribute(self, selector: str, name: str) -> Union[NoneType, str]:261 """è¿åå
ç´ å±æ§å¼ã262 :param selector: ç¨äºæç´¢å
ç´ çéæ©å¨ã å¦ææå¤ä¸ªå
ç´ æ»¡è¶³éæ©å¨ï¼å°ä½¿ç¨ç¬¬ä¸ä¸ªã263 :param name: è¦è·åå
¶å¼çå±æ§å称ã264 """265 element = self._find_element_cross_frame(selector)266 return element.get_attribute(name)267 def get_table_cell(self, row_header: str, column_headers: List[str] = None):268 """è·å¾åå
æ ¼ã269 å¦ææä¸ä¸ªä¸è¥¿çèµ·æ¥åäºç»´è¡¨ï¼é£ä¹å°±å¯ä»¥ä½¿ç¨è¡æ é¢æåæ é¢å»åå¾åå
æ ¼ã270 å¦æä¸æä¾ `column_headers` ï¼é£ä¹å°è¿åææ¬å¼å¹é
`row_header` çå
ç´ å³ä¾§çä¸ä¸ªå
ç´ ã271 :param column_headers: åæ é¢272 :param row_header: è¡æ é¢273 """274 def get_top_column_header_element(cell_text, limit_x):275 __column_header_elements = self._obj.query_selector_all(f"text='{cell_text}'")276 for __column_header_element in __column_header_elements:277 if __column_header_element.bounding_box()["x"] >= limit_x:278 return __column_header_element279 def get_target_column_header_element(cell_text, limit_min_x, limit_max_x):280 __column_header_elements = self._obj.query_selector_all(f"text='{cell_text}'")281 for __column_header_element in __column_header_elements:282 if (283 __column_header_element.bounding_box()["x"] >= limit_min_x284 and __column_header_element.bounding_box()["x"]285 + __column_header_element.bounding_box()["width"] <= limit_max_x286 ):287 return __column_header_element288 row_header_element = self._obj.query_selector(f"text='{row_header}'")289 if not column_headers:290 return self._obj.query_selector(f"*:right-of(:text('{row_header}'))")291 row_header_element_box = row_header_element.bounding_box()292 row_header_element_position = Position(293 x=row_header_element_box["x"] + row_header_element_box["width"] / 2,294 y=row_header_element_box["y"] + row_header_element_box["height"] / 2295 )296 column_min_x = row_header_element_box["x"] + row_header_element_box["width"]297 if len(column_headers) > 1:298 top_column_header_element = get_top_column_header_element(column_headers[0], column_min_x)299 coordinate_range = (300 top_column_header_element.bounding_box()["x"],301 top_column_header_element.bounding_box()["x"] + top_column_header_element.bounding_box()["width"]302 )303 target_column_header_element = get_target_column_header_element(304 column_headers[1],305 coordinate_range[0],306 coordinate_range[1]307 )308 else:309 target_column_header_element = get_top_column_header_element(column_headers[0], column_min_x)310 target_column_header_element_position = Position(311 x=target_column_header_element.bounding_box()["x"] + target_column_header_element.bounding_box()[312 "width"] / 2,313 y=target_column_header_element.bounding_box()["y"] + target_column_header_element.bounding_box()[314 "height"] / 2315 )316 cell_position = Position(317 x=target_column_header_element_position.get("x"),318 y=row_header_element_position.get("y")319 )320 return self._obj.evaluate_handle(321 f"document.elementFromPoint({cell_position.get('x')}, {cell_position.get('y')})")322 def go_back(323 self,324 timeout: float = None,325 wait_until: Literal["domcontentloaded", "load", "networkidle"] = None326 ):327 """导èªå°åå²è®°å½çä¸ä¸é¡µã328 è¿å主è¦èµæºååºã329 å¨å¤æ¬¡éå®åçæ
åµä¸ï¼å¯¼èªå°ä½¿ç¨ä¸æ¬¡éå®åçååºè¿è¡è§£æã330 å¦æä¸è½è¿åï¼åè¿å nullã331 :param timeout:æ大æä½æ¶é´ä»¥æ¯«ç§ä¸ºåä½ï¼é»è®¤ä¸º 30 ç§ï¼ä¼ é 0 以ç¦ç¨è¶
æ¶ã332 å¯ä»¥ä½¿ç¨ browser_context.set_default_navigation_timeout(timeout)ã333 browser_context.set_default_timeout(timeout)ã334 page.set_default_navigation_timeout(timeout)335 æ page.set_default_timeout(timeout) æ¹æ³æ´æ¹é»è®¤å¼ã336 :param wait_until: <"load"|"domcontentloaded"|"networkidle"> å½è®¤ä¸ºæä½æåæ¶ï¼é»è®¤å è½½ã äºä»¶å¯ä»¥æ¯ï¼#337 'domcontentloaded' - å½ DOMContentLoaded äºä»¶è¢«è§¦åæ¶ï¼è®¤ä¸ºæä½å®æã338 'load' - å½å è½½äºä»¶è¢«è§¦åæ¶ï¼è®¤ä¸ºæä½å·²ç»å®æã339 'networkidle' - å½è³å° 500 毫ç§æ²¡æç½ç»è¿æ¥æ¶ï¼è®¤ä¸ºæä½å®æã340 """341 if type(self._obj).__name__ == "Page":342 return self._obj.go_back(343 timeout=timeout,344 wait_until=wait_until,345 )346 elif type(self._obj).__name__ == "Frame":347 return self._obj.page.go_back(348 timeout=timeout,349 wait_until=wait_until,350 )351 else:352 raise TypeError(f"{self._obj}çç±»ååºå½æ¯ Page ç±»åæ Frame ç±»åã")353 def go_forward(354 self,355 timeout: float = None,356 wait_until: Literal["domcontentloaded", "load", "networkidle"] = None357 ):358 """导èªå°åå²è®°å½çä¸ä¸é¡µã359 è¿å主è¦èµæºååºã360 å¨å¤æ¬¡éå®åçæ
åµä¸ï¼å¯¼èªå°ä½¿ç¨ä¸æ¬¡éå®åçååºè¿è¡è§£æã361 å¦æä¸è½è¿åï¼åè¿å nullã362 :param timeout:æ大æä½æ¶é´ä»¥æ¯«ç§ä¸ºåä½ï¼é»è®¤ä¸º 30 ç§ï¼ä¼ é 0 以ç¦ç¨è¶
æ¶ã363 å¯ä»¥ä½¿ç¨ browser_context.set_default_navigation_timeout(timeout)ã364 browser_context.set_default_timeout(timeout)ã365 page.set_default_navigation_timeout(timeout)366 æ page.set_default_timeout(timeout) æ¹æ³æ´æ¹é»è®¤å¼ã367 :param wait_until: <"load"|"domcontentloaded"|"networkidle"> å½è®¤ä¸ºæä½æåæ¶ï¼é»è®¤å è½½ã äºä»¶å¯ä»¥æ¯ï¼#368 'domcontentloaded' - å½ DOMContentLoaded äºä»¶è¢«è§¦åæ¶ï¼è®¤ä¸ºæä½å®æã369 'load' - å½å è½½äºä»¶è¢«è§¦åæ¶ï¼è®¤ä¸ºæä½å·²ç»å®æã370 'networkidle' - å½è³å° 500 毫ç§æ²¡æç½ç»è¿æ¥æ¶ï¼è®¤ä¸ºæä½å®æã371 """372 if type(self._obj).__name__ == "Page":373 return self._obj.go_forward(374 timeout=timeout,375 wait_until=wait_until,376 )377 elif type(self._obj).__name__ == "Frame":378 return self._obj.page.go_forward(379 timeout=timeout,380 wait_until=wait_until,381 )382 else:383 raise TypeError(f"{self._obj}çç±»ååºå½æ¯ Page ç±»åæ Frame ç±»åã")384 def goto(385 self,386 url: str,387 *,388 timeout: float = None,389 wait_until: Literal["domcontentloaded", "load", "networkidle"] = None,390 referer: str = None391 ):392 """导èªå° `url`393 è¿å主è¦èµæºååºã å¨å¤æ¬¡éå®åçæ
åµä¸ï¼å¯¼èªå°ä½¿ç¨ä¸æ¬¡éå®åçååºè¿è¡è§£æã394 å¦æåºç°ä»¥ä¸æ
åµï¼è¯¥æ¹æ³å°æåºé误ï¼395 åå¨ SSL é误ï¼ä¾å¦ï¼å¨èªç¾åè¯ä¹¦çæ
åµä¸ï¼ã396 ç®æ ç½åæ æã397 导èªæé´è¶
æ¶ã398 è¿ç¨æå¡å¨æ²¡æååºææ æ³è®¿é®ã399 主èµæºå 载失败ã400 å½è¿ç¨æå¡å¨è¿åä»»ä½ææç HTTP ç¶æ代ç æ¶ï¼è¯¥æ¹æ³ä¸ä¼æåºé误ï¼å
æ¬ 404âæªæ¾å°âå 500âå
é¨æå¡å¨é误âã401 å¯ä»¥éè¿è°ç¨ response.status æ¥æ£ç´¢æ¤ç±»ååºçç¶æ代ç ã402 注æ:403 该æ¹æ³è¦ä¹æåºé误ï¼è¦ä¹è¿å主èµæºååºã å¯ä¸çä¾å¤æ¯å¯¼èªå° about:blank æ导èªå°å
·æä¸åæ£åçç¸å URLï¼è¿å°æå并è¿å nullã404 :param url:页é¢å¯¼èªå°ç URLã ç½ååºå
æ¬æ¹æ¡ï¼ä¾å¦ https://ã405 å½éè¿ä¸ä¸æé项æä¾ base_url 并ä¸ä¼ éç URL æ¯è·¯å¾æ¶ï¼å®ä¼éè¿æ°ç URL() æé å½æ°å并ã406 :param timeout: æ大æä½æ¶é´ä»¥æ¯«ç§ä¸ºåä½ï¼é»è®¤ä¸º 30 ç§ï¼ä¼ é 0 以ç¦ç¨è¶
æ¶ã407 å¯ä»¥ä½¿ç¨ browser_context.set_default_navigation_timeout(timeout)ã408 browser_context.set_default_timeout(timeout)ã409 page.set_default_navigation_timeout(timeout)410 æ page.set_default_timeout(timeout) æ¹æ³æ´æ¹é»è®¤å¼ã411 :param wait_until: <"load"|"domcontentloaded"|"networkidle"> å½è®¤ä¸ºæä½æåæ¶ï¼é»è®¤å è½½ã äºä»¶å¯ä»¥æ¯ï¼#412 'domcontentloaded' - å½ DOMContentLoaded äºä»¶è¢«è§¦åæ¶ï¼è®¤ä¸ºæä½å®æã413 'load' - å½å è½½äºä»¶è¢«è§¦åæ¶ï¼è®¤ä¸ºæä½å·²ç»å®æã414 'networkidle' - å½è³å° 500 毫ç§æ²¡æç½ç»è¿æ¥æ¶ï¼è®¤ä¸ºæä½å®æã415 :param referer: å¼ç¨æ 头å¼ã å¦ææä¾ï¼å®å°ä¼å
äº page.set_extra_http_headers(headers) 设置çå¼ç¨æ 头å¼.416 """417 return self._obj.goto(418 url=url,419 timeout=timeout,420 wait_until=wait_until,421 referer=referer,422 )423 def hover(424 self,425 selector: str,426 timeout: float = None,427 modifiers: Optional[428 List[Literal["Alt", "Control", "Meta", "Shift"]]429 ] = None,430 position: Position = None,431 ):432 """é¼ æ æ¬å433 æ¤æ¹æ³éè¿æ§è¡ä»¥ä¸æ¥éª¤å°é¼ æ æ¬åå¨å
ç´ ä¸ï¼434 çå¾
对å
ç´ çå¯æä½æ§æ£æ¥ã435 å¦æéè¦ï¼å°å
ç´ æ»å¨å°è§å¾ä¸ã436 ä½¿ç¨ page.mouse å°é¼ æ æ¬åå¨å
ç´ çä¸å¿ææå®ä½ç½®ä¸ã437 çå¾
å¯å¨ç导èªæåæ失败438 :param position: ç¸å¯¹äºå
ç´ å¡«å
æ¡çå·¦ä¸è§ä½¿ç¨çç¹ã å¦ææªæå®ï¼å使ç¨å
ç´ çä¸äºå¯è§ç¹ã439 :param modifiers: è¦æä¸ç修饰é®ã ç¡®ä¿å¨æä½æé´ä»
æä¸è¿äºä¿®é¥°ç¬¦ï¼ç¶åå°å½å修饰符æ¢å¤åæ¥ã å¦ææªæå®ï¼å使ç¨å½åæä¸ç修饰符440 :param selector: ç¨äºæç´¢å
ç´ çéæ©å¨ã å¦ææå¤ä¸ªå
ç´ æ»¡è¶³éæ©å¨ï¼å°ä½¿ç¨ç¬¬ä¸ä¸ªã441 :param timeout: æ大æä½æ¶é´ä»¥æ¯«ç§ä¸ºåä½ï¼é»è®¤ä¸º 30 ç§ï¼ä¼ é 0 以ç¦ç¨è¶
æ¶ã442 å¯ä»¥ä½¿ç¨ browser_context.set_default_navigation_timeout(timeout)ã443 browser_context.set_default_timeout(timeout)ã444 page.set_default_navigation_timeout(timeout)445 æ page.set_default_timeout(timeout) æ¹æ³æ´æ¹é»è®¤å¼ã446 """447 element = self._find_element_cross_frame(selector=selector)448 element.hover(449 modifiers=modifiers,450 timeout=timeout,451 position=position,452 )453 def inner_html(self, selector: str) -> str:454 """å
ç´ ç innerHTML å¼ã455 :param selector: ç¨äºæç´¢å
ç´ çéæ©å¨ã å¦ææå¤ä¸ªå
ç´ æ»¡è¶³éæ©å¨ï¼å°ä½¿ç¨ç¬¬ä¸ä¸ªã456 """457 element = self._find_element_cross_frame(selector)458 return element.inner_html()459 def inner_text(self, selector: str) -> str:460 """å
ç´ ç innerText å¼ã461 :param selector: ç¨äºæç´¢å
ç´ çéæ©å¨ã å¦ææå¤ä¸ªå
ç´ æ»¡è¶³éæ©å¨ï¼å°ä½¿ç¨ç¬¬ä¸ä¸ªã462 """463 element = self._find_element_cross_frame(selector)464 return element.inner_text()465 def input_value(self, selector: str, timeout: float = None) -> str:466 """å
ç´ ç value å±æ§çå¼ã467 :param selector: ç¨äºæç´¢å
ç´ çéæ©å¨ã å¦ææå¤ä¸ªå
ç´ æ»¡è¶³éæ©å¨ï¼å°ä½¿ç¨ç¬¬ä¸ä¸ªã468 :param timeout: æ大æä½æ¶é´ä»¥æ¯«ç§ä¸ºåä½ï¼é»è®¤ä¸º 30 ç§ï¼ä¼ é 0 以ç¦ç¨è¶
æ¶ã469 å¯ä»¥ä½¿ç¨ browser_context.set_default_navigation_timeout(timeout)ã470 browser_context.set_default_timeout(timeout)ã471 page.set_default_navigation_timeout(timeout)472 æ page.set_default_timeout(timeout) æ¹æ³æ´æ¹é»è®¤å¼473 """474 element = self._find_element_cross_frame(selector)475 return element.input_value(timeout=timeout)476 def is_checked(self, selector: str) -> bool:477 """è¿åæ¯å¦éä¸å
ç´ ãå¦æå
ç´ ä¸æ¯å¤éæ¡æåéè¾å
¥ï¼åå¼åå¼å¸¸ã"""478 return self._find_element_cross_frame(selector).is_checked()479 def is_disabled(self, selector: str) -> bool:480 """è¿åå
ç´ æ¯å¦è¢«ç¦ç¨ï¼ä¸å¯ç¨ç¸åã"""481 return self._find_element_cross_frame(selector).is_disabled()482 def is_editable(self, selector: str) -> bool:483 """è¿åå
ç´ æ¯å¦å¯ç¼è¾ã"""484 return self._find_element_cross_frame(selector).is_editable()485 def is_enabled(self, selector: str) -> bool:486 """è¿åå
ç´ æ¯å¦è¢«å¯ç¨ã"""487 return self._find_element_cross_frame(selector).is_enabled()488 def is_hidden(self, selector: str) -> bool:489 """è¿åå
ç´ æ¯å¦éèï¼ä¸å¯è§ç¸åã ä¸å¹é
ä»»ä½å
ç´ çéæ©å¨è¢«è®¤ä¸ºæ¯éèçã"""490 return self._find_element_cross_frame(selector).is_hidden()491 def is_visible(self, selector: str) -> bool:492 """è¿åå
ç´ æ¯å¦å¯è§ã ä¸å¹é
ä»»ä½å
ç´ çéæ©å¨è¢«è®¤ä¸ºæ¯ä¸å¯è§çã"""493 return self._find_element_cross_frame(selector).is_visible()494 def press(495 self,496 selector: str,497 key: str,498 delay: float = None,499 timeout: float = None,500 no_wait_after: bool = None,501 ) -> None:502 """模ææå¨è¾å
¥ã503 èç¦å
ç´ ï¼ç¶å使ç¨keyboard.down(key) åkeyboard.up(key)ã504 key å¯ä»¥æå®é¢æç keyboardEvent.key å¼æè¦ä¸ºå
¶çæææ¬çå个å符ã å¯ä»¥å¨æ¤å¤æ¾å°é®å¼çè¶
éã é®çä¾åæ¯ï¼505 F1 - F12, Digit0- Digit9, KeyA- KeyZ, åå¼å·, åå·, çå·, åææ , Backspace, Tab, Delete, Escape, ArrowDown, End,506 Enter, Home, Insert, PageDown, PageUp, ArrowRight, ArrowUp, ççã507 è¿æ¯æ以ä¸ä¿®æ¹å¿«æ·é®ï¼ShiftãControlãAltãMetaãShiftLeftã508 æä½ Shift é®å°é®å
¥ä¸å¤§åé®å¯¹åºçææ¬ã509 å¦æé®æ¯å个å符ï¼åå®åºå大å°åï¼å æ¤å¼aåAå°çæä¸åçææ¬ã510 ä¹æ¯æå¿«æ·é®ï¼ä¾å¦é®ï¼âControl+oâæé®ï¼âControl+Shift+Tâã å½ä½¿ç¨ä¿®é¥°ç¬¦æå®æ¶ï¼å¨æä¸åç»é®çåæ¶æä¸å¹¶æä½ä¿®é¥°ç¬¦ã511 :param selector: ç¨äºæç´¢å
ç´ çéæ©å¨ãå¦ææå¤ä¸ªå
ç´ æ»¡è¶³éæ©å¨ï¼å°ä½¿ç¨ç¬¬ä¸ä¸ªã512 :param key: è¦æä¸çé®çå称æè¦çæçå符ï¼ä¾å¦ ArrowLeft æ aã513 :param delay: å¨ keydown å keyup ä¹é´çå¾
çæ¶é´ï¼ä»¥æ¯«ç§ä¸ºåä½ï¼ã é»è®¤ä¸º 0ã514 :param timeout: 以毫ç§ä¸ºåä½çæé¿æ¶é´ï¼é»è®¤ä¸º 30 ç§ï¼ä¼ é 0 以ç¦ç¨è¶
æ¶ã515 å¯ä»¥ä½¿ç¨ browser_context.set_default_timeout(timeout) æ page.set_default_timeout(timeout) æ¹æ³æ´æ¹é»è®¤å¼516 :param no_wait_after: å¯å¨å¯¼èªçæä½æ£å¨çå¾
è¿äºå¯¼èªåç并çå¾
页é¢å¼å§å è½½ã å¯ä»¥éè¿è®¾ç½®æ¤æ å¿éæ©éåºçå¾
ã517 åªéè¦å¨ç¹æ®æ
åµä¸ä½¿ç¨æ¤é项ï¼ä¾å¦å¯¼èªå°æ æ³è®¿é®ç页é¢ã é»è®¤ä¸ºåã518 """519 element = self._find_element_cross_frame(selector)520 element.press(521 key=key,522 delay=delay,523 timeout=timeout,524 no_wait_after=no_wait_after,525 )526 def select_option(527 self,528 selector: str,529 value: Union[str, List[str]] = None,530 index: Union[int, List[int]] = None,531 label: Union[str, List[str]] = None,532 option_element: Union["ElementHandle", List["ElementHandle"]] = None,533 timeout: float = None,534 ) -> List[str]:535 """æ¤æ¹æ³çå¾
å
ç´ å¹é
éæ©å¨ï¼çå¾
å¯æä½æ§æ£æ¥ï¼çå¾
æææå®çé项é½åºç°å¨ <select> å
ç´ ä¸å¹¶éæ©è¿äºé项ã536 å¦æç®æ å
ç´ ä¸æ¯ <select> å
ç´ ï¼åæ¤æ¹æ³ä¼å¼åé误ã ä½æ¯ï¼å¦æ该å
ç´ ä½äºå
·æå
³èæ§ä»¶ç <label> å
ç´ å
ï¼åå°ä½¿ç¨è¯¥æ§ä»¶ã537 è¿åå·²æåéæ©çé项å¼æ°ç»ã538 ä¸æ¦éæ©äºæææä¾çé项ï¼å°±ä¼è§¦åæ´æ¹åè¾å
¥äºä»¶ã539 :param selector:540 :param value: æå¼éæ©çé项ã å¯éçã541 å¦æ <select> å
·æ multiple å±æ§ï¼åéæ©ææç»å®çé项ï¼å¦åä»
éæ©ä¸ä¼ éçé项ä¹ä¸å¹é
ç第ä¸ä¸ªé项ã542 :param index: æç´¢å¼éæ©çé项ã å¯éçã543 :param label: ææ ç¾éæ©çé项ã å¯éçã544 å¦æ <select> å
·æ multiple å±æ§ï¼åéæ©ææç»å®çé项ï¼å¦åä»
éæ©ä¸ä¼ éçé项ä¹ä¸å¹é
ç第ä¸ä¸ªé项ã545 :param option_element: æ ElementHandle å®ä¾éæ©é项ã å¯éçã546 :param timeout: 以毫ç§ä¸ºåä½çæé¿æ¶é´ï¼é»è®¤ä¸º 30 ç§ï¼ä¼ é 0 以ç¦ç¨è¶
æ¶ã547 å¯ä»¥ä½¿ç¨ browser_context.set_default_timeout(timeout) æ page.set_default_timeout(timeout) æ¹æ³æ´æ¹é»è®¤å¼ã548 """549 element = self._find_element_cross_frame(selector=selector)550 return element.select_option(551 timeout=timeout,552 element=option_element,553 index=index,554 value=value,555 label=label556 )557 def select_option_for_ant(558 self,559 selector: str,560 index: int = None,561 label: str = None,...
test_snmp.py
Source:test_snmp.py
...39 while (self.snmp_engine.poll() > 0):40 self.zmq_core.poll(0.1)41 def test_valid_timeout(self):42 """ Valid timeout value updates default SNMP timeout """43 self.snmp_engine.set_default_timeout("123")44 eq_(self.snmp_engine.default_timeout, 123)45 def test_bad_timeout(self):46 """ Invalid timeout value doesnt change timeout """47 default_timeout = self.snmp_engine.default_timeout48 self.snmp_engine.set_default_timeout("Not a number")49 eq_(self.snmp_engine.default_timeout, default_timeout)50 def test_v1get(self):51 """ Simple SNMP v1 fetch of SysObjectID """52 self.set_host(1, 'public')53 assert(self.snmp_engine.get_str(54 self.test_host, self.sysobjid_oid, my_callback1, obj=self))55 self.poll()56 eq_(self.results, [self.expected_sysobjid, ])57 def test_v2get(self):58 """ Simple SNMP v2 fetch of SysObjectID """59 self.set_host(2, 'public')60 self.snmp_engine.get_str(61 self.test_host, self.sysobjid_oid, my_callback1, obj=self)62 self.poll()63 eq_(self.results, [self.expected_sysobjid, ])64 def test_v1_default_bad_comm(self):65 """ Simple SNMP v1 fetch returns default with bad community"""66 self.set_host(1, 'badcomm')67 self.snmp_engine.set_default_timeout(1)68 self.snmp_engine.get_str(69 self.test_host, self.sysobjid_oid, my_callback1,70 default="42", obj=self)71 self.poll()72 eq_(self.results, ["42"])73 def test_v2_default_bad_comm(self):74 """ Simple SNMP v2c fetch returns default with bad community"""75 self.set_host(2, 'badcomm')76 self.snmp_engine.set_default_timeout(1)77 self.snmp_engine.get_str(78 self.test_host, self.sysobjid_oid, my_callback1,79 default="42", obj=self)80 self.poll()81 eq_(self.results, ["42"])82 def test_v1_default_bad_oid(self):83 """ Simple SNMP v1 fetch returns default with bad OID"""84 self.set_host(1, 'public')85 self.snmp_engine.set_default_timeout(1)86 self.snmp_engine.get_str(87 self.test_host, '1.3.6.42.41.40', my_callback1,88 default="42", obj=self)89 self.poll()90 eq_(self.results, ["42"])91 def test_v2_default_bad_oid(self):92 """ Simple SNMP v2c fetch returns default with bad OID"""93 self.set_host(2, 'public')94 self.snmp_engine.set_default_timeout(1)95 self.snmp_engine.get_str(96 self.test_host, '1.3.6.42.41.40', my_callback1,97 default="42", obj=self)98 self.poll()99 eq_(self.results, ["42"])100 def test_v1_table(self):101 """ SNMP v1 table fetch - uses ifTable.ifIndex """102 self.set_host(1, 'public')103 self.snmp_engine.set_default_timeout(1)104 # Get size of iftable105 self.snmp_engine.get_int(106 self.test_host, '1.3.6.1.2.1.2.1.0', my_callback1, obj=self)107 self.poll()108 eq_(len(self.results), 1)109 table_length = self.results[0]110 self.snmp_engine.get_table(111 self.test_host, ('1.3.6.1.2.1.2.2.1.1',), my_callback1,112 table_trim=1, obj=self)113 self.poll()114 eq_(len(self.results),2)115 eq_(len(self.results[1][0]),table_length)116 for oid,val in self.results[1][0].items():117 assert(oid == val)...
connection.py
Source:connection.py
...50 lazy_connect=lazy_connect,51 auth_provider=auth_provider,52 port=cluster_conf[ConfigKey.PORT])53 connection_timeout = cluster_conf.get(ConfigKey.DEFAULT_TIMEOUT, 10.0)54 set_default_timeout(cqlengine_connection.get_connection(), connection_timeout)55 _connection_setup = True56 return True57 except KeyError as exc:58 LOG.debug("Got exception %s with args %s while trying to setup Cassandra connection", exc, exc.args)59 return False60def set_default_timeout(conn=None, default_timeout_in_s=10.0):61 """Sets the default connection timeout that cqlengine uses.62 :param conn: The connection to set default_timeout on.63 :type conn: cassandra.cqlengine.connection.Connection64 :param default_timeout_in_s: The default connection timeout to use in seconds. This timeout will be used unless65 a different timeout is explicitly used on a specific query66 :type default_timeout_in_s: float67 """68 if conn is None:69 conn = cqlengine_connection.get_connection()70 if not conn.session:71 # Session is being loaded lazily, so need to assign the timeout at first session creation72 # Monkey Patch the handle_lazy_connect method so we can intercept the initial call and do session setup73 old_handle_lazy_connect = conn.handle_lazy_connect74 def cb_handle_lazy_connect():75 """handle_lazy_connect monkey patch interceptor method which assigns the default timeout upon76 lazy session creation of cqlengine77 """78 LOG.debug("Intercepted lazy cqlengine connect call")79 conn.handle_lazy_connect = old_handle_lazy_connect80 old_handle_lazy_connect()81 set_default_timeout(default_timeout_in_s)82 conn.handle_lazy_connect = cb_handle_lazy_connect83 else:84 LOG.debug("Default connection timeout set to %s", default_timeout_in_s)85 conn.session.default_timeout = default_timeout_in_s86def is_cassandra_available():87 """88 :return: Whether or not Cassandra is currently available.89 :rtype: bool90 """91 cass_setup = False92 try:93 cass_setup = setup_connection_from_config()94 except AttributeError:95 pass...
ggogle_tans.py
Source:ggogle_tans.py
...10 :return: è¿å两个 list11 """12 browser = playwright.chromium.launch(headless=False)13 context = browser.new_context()14 context.set_default_timeout(0)15 # Open new page16 page1 = context.new_page()17 page1.set_default_timeout(0)18 page1.set_default_navigation_timeout(0)19 page2 = context.new_page()20 page2.set_default_timeout(0)21 page2.set_default_navigation_timeout(0)22 # Go to https://translate.google.cn/?sl=en&tl=zh-CN&op=translate23 page1.goto("https://translate.google.cn/?sl=en&tl=zh-CN&op=translate")24 page2.goto("https://translate.google.cn/?sl=en&tl=zh-CN&op=translate")25 # Click [aria-label="åæ"]26 # page.click("[aria-label=\"åæ\"]")27 # Fill [aria-label="åæ"]28 # ä¸ä¸ªç®åç并è¡ï¼æéè¦ç¿»è¯çå
容åæ两é¨åï¼åå¼å¨ä¸¤ä¸ªç½é¡µä¸ç¿»è¯29 result1 = []30 result2 = []31 trans_num = len(trans_list)32 trans_lis_1 = trans_list[0:trans_num // 2]33 trans_lis_2 = trans_list[trans_num // 2:trans_num]34 for i,j in zip(trans_lis_1, trans_lis_2):...
LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!