返回列表 上一主題 發帖

[發問] Selenium

本帖最後由 singo1232001 於 2023-7-20 03:01 編輯

回復 1# t8899


<div class="Jc(fe) Mend(4px) Fw(600) D(f) Ai(c) C($c-trend-up)"   ' 這是紅色向上
<div class="Jc(fe) Mend(4px) Fw(600) D(f) Ai(c) C($c-trend-down)"  '這是綠色 向下


所以   
if  網頁元素物件.Attribute("class") Like "*C($c-trend-up)*" Then
debug.print "▲"  '正數
else
debug.print "▼"  '負數
end if


你也可以試著一個一個
debug.print  網頁元素物件.Attribute("class")  
觀察是不是你要的

TOP

本帖最後由 singo1232001 於 2023-7-20 11:05 編輯

回復 1# t8899


Sub ttt()
Set driver = CreateObject("Selenium.ChromeDriver")
driver.get "https://tw.stock.yahoo.com/quote/2330"
driver.Wait 1000
Set ID_0 = driver.findelementbyID("qsp-overview-realtime-info")
Set ID_0_ul_S = ID_0.findelementsbytag("ul")
Set ID_0_ul_S_1_li_S = ID_0_ul_S(1).findelementsbytag("li")

For i = 1 To ID_0_ul_S_1_li_S.Count
Set x = ID_0_ul_S_1_li_S(i).findelementsbytag("span")
  If x(1).Text Like "*漲跌*" Then

      Select Case True
      Case x(2).Attribute("class") Like "*C($c-trend-up)*": tx = "▲ " & x(2).Text
      Case x(2).Attribute("class") Like "*C($c-trend-down)*": tx = "▼ " & x(2).Text
      Case Else: tx = x(2).Text
      End Select
  
      Debug.Print x(1).Text & " " & tx
  Else
      Debug.Print x(1).Text & " " & x(2).Text
  End If
Next
Stop
End Sub

TOP

本帖最後由 singo1232001 於 2023-7-20 11:57 編輯

回復 1# t8899


如果你用xpath 就是這樣
Sub tttt()
Set driver = CreateObject("Selenium.ChromeDriver")
driver.get "https://tw.stock.yahoo.com/quote/2330"
driver.Wait 1000

Set xp = driver.findelementbyxpath("//*[@id=""qsp-overview-realtime-info""]/div[2]/div[2]/div/ul/li[8]")
      Set sn = xp.findelementsbytag("span")

Select Case True
Case sn(2).Attribute("class") Like "*C($c-trend-up)*": tx = "▲ "
Case sn(2).Attribute("class") Like "*C($c-trend-down)*": tx = "▼ "
Case Else: tx = " - "
End Select
Debug.Print sn(1).Text & " " & tx & " " & sn(2).Text
  
Stop
End Sub

TOP

本帖最後由 singo1232001 於 2023-8-24 18:13 編輯

回復 6# t8899


Sub 時間內沒加載完就算失敗()
t = Timer '測試用可刪
Set driver = CreateObject("selenium.chromedriver")
driver.Timeouts.pageload = 10000 '要是秒數過低 沒加載完 就會強制關閉 可以試著測試3秒以下
On Error Resume Next
driver.get "https://finance.yahoo.com/quote/TSM/"
If Err.Number <> 0 Then
Err.Clear
driver.Quit
End If
On Error GoTo 0
Debug.Print Timer - t  '測試用可刪
End Sub




因為不曉得你的需求
所以我順便把重連三次的寫法也給你

Sub 重連三次每次等三秒_第四次就失敗()
Set driver = CreateObject("selenium.chromedriver")
On Error Resume Next
Do
driver.Get "https://finance.yahoo.com111111111/quote/TSM/"
If Err.Number <> 0 Then
    Err.Clear
    Application.Wait Now + TimeValue("00:00:03")
    rT = rT + 1
    If rT = 3 Then driver.Quit: Exit Do
Else
    Exit Do
End If
Loop
On Error GoTo 0
End Sub

TOP

回復 9# Scott090


    Sub test()
Set driver = CreateObject("selenium.chromedriver")
driver.get "https://finance.yahoo.com/quote/TSM/"

Do: Set ids = driver.findelementsbyId("quote-summary")
    If ids.Count > 0 Then Exit Do
    Loop
Do: Set tby = ids(1).findelementsbytag("tbody")
    If tby.Count > 0 Then Exit Do
    Loop
   
Debug.Print tby(1).Text


Set trs = tby(1).findelementsbytag("tr")
ReDim ar(1 To trs.Count, 1)
For i = 1 To trs.Count
    Set tds = trs(i).findelementsbytag("td")
    ar(i, 0) = tds(1).Text
    ar(i, 1) = tds(2).Text
Next

Cells.ClearContents
Range("a1").Resize(UBound(ar), 2) = ar
End Sub

TOP

本帖最後由 singo1232001 於 2023-8-30 09:09 編輯

回復 11# Scott090


把找元素想像成,跟檔案總管找資料夾一樣
常用的有父子關係 兄弟關係 或者ID唯一性
而父元素 有的用ID 有的用tag  有的是順位 有的用副加屬性attribute
這4種方式可以協助你找到需要的父元素 或 子元素
當下看看那些方式適合使用
通常用於網頁有異步的情況 才會需要順著關係或逆著關係找
若網頁沒異步 就不用關係找 直接用byXpath完整路徑找到就可以
就如同 直接複製資料夾路徑那樣 就能前往資料夾位置
通常能解決80~90%尋找問題

補充:
還可能遇到ifram有框架網站
還可能遇到需要用execute注入css 解決剩下10~20%其他有對元素動手腳,網站性特殊保護的問題

TOP

        靜思自在 : 慈悲沒有敵人,智慧不起煩惱。
返回列表 上一主題