Ãö©óselenium ¦h¶}¤À¶ ¤Á´« ²¾°Ê Ãö³¬ ·s¤À¶ «±Ò µ¥»yªk
- ©«¤l
- 354
- ¥DÃD
- 5
- ºëµØ
- 0
- ¿n¤À
- 387
- ÂI¦W
- 0
- §@·~¨t²Î
- windows7
- ³nÅ骩¥»
- vba,vb,excel2007
- ¾\ŪÅv
- 20
- ©Ê§O
- ¨k
- µù¥U®É¶¡
- 2017-1-8
- ³Ì«áµn¿ý
- 2024-8-2
|
Ãö©óselenium ¦h¶}¤À¶ ¤Á´« ²¾°Ê Ãö³¬ ·s¤À¶ «±Ò µ¥»yªk
¥»©«³Ì«á¥Ñ singo1232001 ©ó 2023-7-21 13:14 ½s¿è
¯d§@³Æ§Ñ ³o°ÝÃDµª®× ¤@ª½¤£¦n§ä ¤]¥u¦³«D±`¤j¶q¨Ï¥Îª¦ÂÎ ¸û®e©ö·|¸I¨ì
¥Ñ©ó¸ê®Æ¤£¬O«Ü¥R¨¬ §xÂZ«Ü¤[ ¦ý¥Ñ©óè¦n¥»¨¦³»Ý¨D ´N´ú¸Õ¤U¥h ¨Ã¥B¥h°ê¥~§ì¸ê®Æ
°®¯Ü¼g¤@½g¤å³¹
¹ï©óselenium ¤À¶¾Þ§@ªº¥\¯à µy·L»¡©ú¤@¤U
Á|googleºô¶¬°¨Ò
url = "https://www.google.com.tw/"
1.·s¶}¤À¶ (»Ý¦A¤w¦³driver.get url ¤w¶}±Ò¤@Ó°ò¥»¤À¶®É)
driver.executeScript ("window.open('" & url & "')") '³oºØ°µªk ¤À¶±Ò¥ÎµJÂI·|¶¶«K¹L¥h
¦ÓY¨Ï¥Î
'driver.executeScript ("window.open()")
'driver.SwitchToNextWindow
'driver.get url
«h®e©ö¹J¨ì ¤À¶ªº±Ò¥ÎµJÂI¥X°ÝÃDªº±¡ªp ÁöµM«j±jÁÙ¬O¥i¥H¥Î
2.¹J¨ì"¦PTitle"¦ý"cookieºô§}·|Åܤƪº"¤À¶¶W³sµ²«ö¶s©Î¶W³sµ²¿ï¶µ
¥ý¥hfindª«¥ó¤¸¯À¬OþÓ §ä¨ì«á ¦A§Q¥ÎAttribute §âhrefªººô§}¸ê®Æ¨ú¥X
href_url = Findª«¥ó¤¸¯À¤U.Attribute("href")
driver.executeScript ("window.open('" & href_url & "')")
3.Ãö³¬¤À¶ driver.close
Ãö³¬¤À¶¥²¶·¦³«áÄò°Ê§@
¨Ò:°²³]¦³5Ó¤À¶ 1 2 3 4 5 , ¨Ã¥B¦A¤À¶5 °õ¦ædriver.closeªºª¬ªp¤U
ÁöµMÃö³¬¤À¶5«á ·|³Ñ¤U1 2 3 4 ¦ý¬O1 2 3 4 ³£µLªk¾Þ±± ¦]¬°±Ò¥Î(¿E¬¡)ªºµJÂIÁÙ¦A5¤W±
³o®ÉÔ ¦³4ºØ¤èªk¥i¥H³B²z
3-1 ¨Ï¥Î driver.SwitchToNextWindow
¥i¥H¸õ¨ì¤U¤@Ó¤À¶ ¥B¥i¥H´`Àô
±µÄò¤Wzª¬ªp Ãö³¬¤À¶5¤§«á ¨Ï¥Î¸õ¤U¤@¤À¶ ·|¦^¨ì¤À¶1 YÄ~Äò¨Ï¥Î ·|Ä~Äò¨ì¤À¶2 ¦Ó¥BÁÙ¥i¥HÄ~Äò©¹¤À¶3¸õ
(¦ý¦¹ºØ¥\¯à ·|¦b¹B®ð®tªº±¡ªp¤U¤j·§2~3% ·|§¹¥þµL§@¥Î¥¢®Ä ¦ýª¬ªp¤£¬OÀH¾÷ ·|«½Æ¥X²{)
3-2¨Ï¥Î driver.SwitchToPreviousWindow
¥i¸õ¦^¤W¤@Ó¤À¶ ¦ýµLªk´`Àô Y123Ó¤À¶ ¦b¤À¶2¨Ï¥Î¸õ¦^¤W¤@Ó¤À¶ ·|¦^¨ì¤À¶1
¦ýYÄ~Äò¨Ï¥Î ¤S·|¦^¨ì¤À¶2
¸É¥R:Y3-1 ,3-2 ·Qn¨Ì·Ó"ºô§}ÃöÁä¦r"¬d¸ß ¥i¥H§Q¥Î °j°é »Plikeªº¤è¦¡°µ·f°t
¨Ò:
For i = 1 to 10 '(ÀH«K³]¼Æ¶q ¤Ï¥¿¤ñ¤À¶¼Æ¦h´N¦n ¦Ü¤Ö¯à¶¤@°é)
driver.SwitchToPreviousWindow
if driver.url like "*google*" then exit for
Next
¸É¥R ·íµM¤]¥i¥H¨Ï¥Î driver.Windows.Count ¥i¥H¬d¸ß©Ò¦³¤À¶¼Æ¶q
3-3 ¨Ï¥Î driver.SwitchToWindowByTitle "google"
Y¨Ï¥Î¦¹ºØ¤è¦¡ ¥u¯à°w¹ï¸Ó«ü©wªº¤À¶title ¥²¶·¬O°ß¤@ªº
°²¦p 4Ó¤À¶ªº¦WºÙ ³£¬O "google" ¨º»ò ±N§ä¨ì²Ä¤@Ó¦W¬°googleªº¤À¶
3-4 ¨Ï¥Î ¤U±¨âºØ¤è¦¡ ¥i¥H¨Ì¤À¶¼Æ¯Á¤Þ«ü©w¸õ¶
Debug.print driver.Windows.Item(i).Title
©Î
x = driver.Windows.Item(i).Title
¤£n°Ý§Ú¬°Ô£ªø³o¼Ë §Ú¤]·d¤£À´ «D±`©_§®
·íµM¤]¥i¥H·f°t¤W±ªº°j°é
For i = 1 To driver.Windows.Count
x= driver.windows.Item(i)
If driver.title = "google" Then Exit For
Next
©ÎªÌ
For i = 1 to driver.Windows.Count
x= driver.windows.Item(i)
If driver.url like "*google*" Then Exit For
Next
©ÎªÌ
For each z in driver.windows
x= z.title
If driver.url like "*google*" Then Exit For
Next
©ÎªÌ
For each z in driver.windows
z.title
If driver.url like "*google*" Then Exit For
Next
·íµMÁÙ¦³¤@ºØ«Ü¯S®íªº©µ®É±j¨î«±Ò¤è¦¡ »Ý¥Î¨ì«Ü¦h¥\¯à·f°t »P¥»¦¸¥DÃDµy·L¦³Ãö
¸õ¶¨ê·sªº®ÉÔ ¦³®ÉÔ®e©ö¹J¨ìÂ_ºô ©ÎªÌ ¿ù»~¤§Ãþ ¾ÉPµ{§Ç¤¤Â_
¨Ò¤l¦p¤U
Dim lasttimevalue '³o¦æn¦b³Ì³»³¡
Sub ±j¨î30¬í«á«±Ò() '³o¨âÓµ{§Ç³£¦b ¼Ò²Õ(Module1) ©³¤U
Set driver = Nothing
lasttimevalue = Now + TimeValue("00:00:30")
Application.OnTime lasttimevalue, "Module1.¥Dµ{§Ç"
End sub
Sub ¥Dµ{§Ç()
On error goto errorhandle
..............
........
errorhandle:
If Err.Number = 23 Then Run "Module1.±j¨î30¬í«á«±Ò": Exit Sub '¤£¤@©wn=23 ,¥i¥H¨Ì·Ó¸I¨ìªºerr.number½Õ¾ã ©ÎªÌª½±µ³]>0
End sub |
|
|
|
|
|
|