rod_utils.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. package common
  2. import (
  3. "errors"
  4. "git.listensoft.net/tool/jspkit/taxerr"
  5. "github.com/go-kratos/kratos/v2/log"
  6. "github.com/go-rod/rod"
  7. "github.com/go-rod/rod/lib/input"
  8. "github.com/go-rod/rod/lib/utils"
  9. "strings"
  10. "time"
  11. )
  12. func MustHas(page *rod.Page, xpath string) bool {
  13. x, _, _ := HasX(page, xpath)
  14. return x
  15. }
  16. func MustHasX(page *rod.Page, xpath string) bool {
  17. x, _, _ := HasX(page, xpath)
  18. return x
  19. }
  20. func MustHasXV(page *rod.Page, xpath string) bool {
  21. for _, element := range MustElementsX(page, xpath) {
  22. visible, _ := element.Visible()
  23. if visible {
  24. return true
  25. }
  26. }
  27. return false
  28. }
  29. func MustElementX(page *rod.Page, xpath string) *rod.Element {
  30. for i := 0; i < 5; i++ {
  31. x, _ := ElementX(page, xpath)
  32. if x != nil {
  33. return x
  34. }
  35. utils.Sleep(1)
  36. }
  37. return nil
  38. }
  39. func MustHasXV1(page *rod.Page, xpath string) bool {
  40. for _, element := range MustElementsX1(page, xpath) {
  41. visible, _ := element.Visible()
  42. if visible {
  43. return true
  44. }
  45. }
  46. return false
  47. }
  48. func HasX(page *rod.Page, xpath string) (bool, *rod.Element, error) {
  49. var x *rod.Element
  50. var has bool
  51. var err error
  52. err = rod.Try(func() {
  53. has, x, err = page.HasX(xpath)
  54. if has {
  55. return
  56. }
  57. if err != nil {
  58. panic(err)
  59. }
  60. if has {
  61. return
  62. }
  63. iframe := GetAllIframe(page)
  64. for _, pg := range iframe {
  65. has, x, err = pg.HasX(xpath)
  66. if has {
  67. return
  68. }
  69. if err != nil {
  70. panic(err)
  71. }
  72. if has {
  73. return
  74. }
  75. }
  76. })
  77. if x != nil {
  78. x = x.Timeout(time.Second * 15)
  79. }
  80. return has, x, err
  81. }
  82. func ElementX(page *rod.Page, xpath string) (*rod.Element, error) {
  83. _, element, err := HasX(page, xpath)
  84. if err != nil {
  85. err = taxerr.NewWebStuckTitle(false)
  86. }
  87. return element, err
  88. }
  89. func GetAllIframe(page *rod.Page) []*rod.Page {
  90. fs := make([]*rod.Page, 0)
  91. if page.MustHasX(`//iframe`) {
  92. for _, element := range page.MustElementsX(`//iframe`) {
  93. fs = append(fs, element.MustFrame())
  94. fs = append(fs, GetAllIframe(element.MustFrame())...)
  95. }
  96. }
  97. return fs
  98. }
  99. func MustElementXV(page *rod.Page, xpath string) *rod.Element {
  100. for i := 0; i < 5; i++ {
  101. x, _ := ElementsX(page, xpath)
  102. for _, element := range x {
  103. visible, _ := element.Visible()
  104. if visible {
  105. return element
  106. }
  107. }
  108. utils.Sleep(1)
  109. }
  110. return nil
  111. }
  112. func MustElementsX(page *rod.Page, xpath string) (x []*rod.Element) {
  113. for i := 0; i < 5; i++ {
  114. x, _ = ElementsX(page, xpath)
  115. if len(x) != 0 {
  116. break
  117. }
  118. utils.Sleep(1)
  119. }
  120. return x
  121. }
  122. func MustElementsX1(page *rod.Page, xpath string) (x []*rod.Element) {
  123. x, _ = ElementsX(page, xpath)
  124. return x
  125. }
  126. func ElementsX(page *rod.Page, xpath string) ([]*rod.Element, error) {
  127. var arr []*rod.Element
  128. var err error
  129. err = rod.Try(func() {
  130. r, _ := page.Timeout(ClickTimeOut).ElementsX(xpath)
  131. if err != nil {
  132. panic(err)
  133. }
  134. arr = append(arr, r...)
  135. for _, pg := range GetAllIframe(page) {
  136. pg.MustWaitLoad()
  137. r, err = pg.Timeout(ClickTimeOut).ElementsX(xpath)
  138. if err != nil {
  139. panic(err)
  140. }
  141. arr = append(arr, r...)
  142. }
  143. })
  144. return arr, err
  145. }
  146. // 有就点 没有就不点击
  147. func MustClick(page *rod.Page, xpath string) {
  148. x := MustElementsX(page, xpath)
  149. for _, element := range x {
  150. visible, _ := element.Visible()
  151. if visible {
  152. element.MustClick()
  153. break
  154. }
  155. }
  156. }
  157. func MustText(page *rod.Page, xpath string) string {
  158. //去掉左右空格
  159. return strings.TrimSpace(MustElementX(page, xpath).MustText())
  160. }
  161. // 输入文字
  162. func Input(p *rod.Page, selector, inputVal string) {
  163. err := rod.Try(func() {
  164. if inputVal == "" || inputVal == "*" {
  165. inputVal = "0"
  166. }
  167. e := rod.Try(func() {
  168. p.Timeout(time.Second).MustSearch(selector).MustSelectAllText().MustFrame().Keyboard.MustType(input.Backspace)
  169. })
  170. if e != nil {
  171. if w := errors.Unwrap(e); w != nil {
  172. log.Info(w.Error() + "-" + selector + "-" + inputVal)
  173. } else {
  174. log.Info(e.Error() + "-" + selector + "-" + inputVal)
  175. }
  176. }
  177. e = rod.Try(func() {
  178. p.Timeout(time.Second).MustSearch(selector).MustSelectAllText().MustInput(inputVal)
  179. })
  180. if e != nil {
  181. if w := errors.Unwrap(e); w != nil {
  182. log.Info(strings.Split(w.Error(), "\t")[0] + "-" + selector + "-" + inputVal)
  183. } else {
  184. log.Info(strings.Split(e.Error(), "\t")[0] + "-" + selector + "-" + inputVal)
  185. }
  186. }
  187. })
  188. if err != nil {
  189. log.Info(err.Error() + "-" + selector + "-" + inputVal)
  190. }
  191. }
  192. // 输入文字
  193. func MustInputX(p *rod.Page, xPath, inputVal string) {
  194. e := MustElementX(p, xPath)
  195. _ = e.ScrollIntoView()
  196. e.MustClick().MustSelectAllText().MustInput(inputVal)
  197. }
  198. // 拦截请求
  199. func PageHijackReq(b *rod.Page, pattern string, ch chan []byte) *rod.HijackRouter {
  200. router := b.HijackRequests()
  201. router.MustAdd(pattern, func(ctx *rod.Hijack) {
  202. rod.Try(func() {
  203. ctx.MustLoadResponse()
  204. ch <- []byte(ctx.Response.Body())
  205. utils.Sleep(5)
  206. select {
  207. case <-ch:
  208. break
  209. }
  210. })
  211. })
  212. go router.Run()
  213. return router
  214. }
  215. // 获取拦截请求的值
  216. func GetHijackResp(ch chan []byte, t float64) ([]byte, error) {
  217. d := time.Duration(t * float64(time.Second))
  218. select {
  219. case val := <-ch:
  220. return val, nil
  221. case <-time.After(d):
  222. return nil, errors.New("请求网页超时,请稍后重试!")
  223. }
  224. }