rod_utils.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. package common
  2. import (
  3. "errors"
  4. "os"
  5. "strings"
  6. "time"
  7. "git.listensoft.net/tool/jspkit/common/lxrod"
  8. "git.listensoft.net/tool/jspkit/common/models"
  9. "git.listensoft.net/tool/jspkit/common/watermark"
  10. "git.listensoft.net/tool/jspkit/logger"
  11. "git.listensoft.net/tool/jspkit/taxerr"
  12. "github.com/go-rod/rod"
  13. "github.com/go-rod/rod/lib/input"
  14. "github.com/go-rod/rod/lib/proto"
  15. "github.com/go-rod/rod/lib/utils"
  16. "go.uber.org/zap"
  17. )
  18. func MustHas(page *rod.Page, xpath string) bool {
  19. x, _, _ := HasX(page, xpath)
  20. return x
  21. }
  22. func MustHasX(page *rod.Page, xpath string) bool {
  23. x, _, _ := HasX(page, xpath)
  24. return x
  25. }
  26. func MustHasXV(page *rod.Page, xpath string) bool {
  27. for _, element := range MustElementsX(page, xpath) {
  28. visible, _ := element.Visible()
  29. if visible {
  30. return true
  31. }
  32. }
  33. return false
  34. }
  35. func MustElementX(page *rod.Page, xpath string) *rod.Element {
  36. for i := 0; i < 5; i++ {
  37. x, _ := ElementX(page, xpath)
  38. if x != nil {
  39. return x
  40. }
  41. utils.Sleep(1)
  42. }
  43. return nil
  44. }
  45. func MustHasXV1(page *rod.Page, xpath string) bool {
  46. for _, element := range MustElementsX1(page, xpath) {
  47. visible, _ := element.Visible()
  48. if visible {
  49. return true
  50. }
  51. }
  52. return false
  53. }
  54. func HasX(page *rod.Page, xpath string) (bool, *rod.Element, error) {
  55. var x *rod.Element
  56. var has bool
  57. var err error
  58. err = rod.Try(func() {
  59. has, x, err = page.HasX(xpath)
  60. if has {
  61. return
  62. }
  63. if err != nil {
  64. panic(err)
  65. }
  66. if has {
  67. return
  68. }
  69. iframe := GetAllIframe(page)
  70. for _, pg := range iframe {
  71. has, x, err = pg.HasX(xpath)
  72. if has {
  73. return
  74. }
  75. if err != nil {
  76. panic(err)
  77. }
  78. if has {
  79. return
  80. }
  81. }
  82. })
  83. if x != nil {
  84. x = x.Timeout(time.Second * 15)
  85. }
  86. return has, x, err
  87. }
  88. func ElementX(page *rod.Page, xpath string) (*rod.Element, error) {
  89. _, element, err := HasX(page, xpath)
  90. if err != nil {
  91. err = taxerr.NewWebStuckTitle(false)
  92. }
  93. return element, err
  94. }
  95. func GetAllIframe(page *rod.Page) []*rod.Page {
  96. fs := make([]*rod.Page, 0)
  97. if page.MustHasX(`//iframe`) {
  98. for _, element := range page.MustElementsX(`//iframe`) {
  99. fs = append(fs, element.MustFrame())
  100. fs = append(fs, GetAllIframe(element.MustFrame())...)
  101. }
  102. }
  103. return fs
  104. }
  105. func MustElementXV(page *rod.Page, xpath string) *rod.Element {
  106. for i := 0; i < 5; i++ {
  107. x, _ := ElementsX(page, xpath)
  108. for _, element := range x {
  109. visible, _ := element.Visible()
  110. if visible {
  111. return element
  112. }
  113. }
  114. utils.Sleep(1)
  115. }
  116. return nil
  117. }
  118. func MustElementsX(page *rod.Page, xpath string) (x []*rod.Element) {
  119. for i := 0; i < 5; i++ {
  120. x, _ = ElementsX(page, xpath)
  121. if len(x) != 0 {
  122. break
  123. }
  124. utils.Sleep(1)
  125. }
  126. return x
  127. }
  128. func MustElementsX1(page *rod.Page, xpath string) (x []*rod.Element) {
  129. x, _ = ElementsX(page, xpath)
  130. return x
  131. }
  132. func ElementsX(page *rod.Page, xpath string) ([]*rod.Element, error) {
  133. var arr []*rod.Element
  134. var err error
  135. err = rod.Try(func() {
  136. r, _ := page.Timeout(ClickTimeOut).ElementsX(xpath)
  137. if err != nil {
  138. panic(err)
  139. }
  140. arr = append(arr, r...)
  141. for _, pg := range GetAllIframe(page) {
  142. pg.MustWaitLoad()
  143. r, err = pg.Timeout(ClickTimeOut).ElementsX(xpath)
  144. if err != nil {
  145. panic(err)
  146. }
  147. arr = append(arr, r...)
  148. }
  149. })
  150. return arr, err
  151. }
  152. func WaitElementX(page *rod.Page, xpath string, interval float64) *rod.Element {
  153. for i := 0; i < 60; i++ {
  154. if has, el, _ := HasX(page, xpath); has {
  155. return el
  156. }
  157. time.Sleep(time.Duration(float64(time.Second) * interval))
  158. }
  159. return nil
  160. }
  161. func WaitNavigation(page *rod.Page, url string) {
  162. navigation := page.Timeout(time.Minute).MustWaitNavigation()
  163. page.Timeout(time.Minute).MustNavigate(url)
  164. logger.Info("调试埋点11-1")
  165. utils.Sleep(1)
  166. WaitTimeOut(navigation, time.Minute)
  167. logger.Info("埋点11-2")
  168. }
  169. func WaitTimeOut(f func(), timeout time.Duration) {
  170. resultChan := make(chan struct{}, 1)
  171. go func() {
  172. f()
  173. resultChan <- struct{}{}
  174. }()
  175. select {
  176. case <-resultChan:
  177. return
  178. case <-time.After(timeout):
  179. panic(taxerr.NewWebStuckTitle(ComInfo.Cscsts))
  180. }
  181. }
  182. func Link(p *rod.Page) {
  183. go func() {
  184. //for {
  185. //utils.Sleep(1)
  186. if p == nil {
  187. return
  188. }
  189. rod.Try(func() {
  190. lxrod.DialogWatch(nil, p)
  191. go p.Browser().EachEvent(func(e *proto.TargetTargetCreated) {
  192. defer func() {
  193. if r := recover(); r != nil {
  194. logger.Error("EachPageCreatedEvent recover:", zap.Any("r", r))
  195. }
  196. }()
  197. if e.TargetInfo.Type != proto.TargetTargetInfoTypePage {
  198. return
  199. }
  200. page := p.Browser().MustPageFromTargetID(e.TargetInfo.TargetID)
  201. lxrod.DialogWatch(nil, page)
  202. })()
  203. // has, _, _ := p.Has("dialog")
  204. // if has {
  205. // _, _ = p.Eval(`
  206. //window.alert = function() {};
  207. //window.confirm = function() { return true; };
  208. //window.prompt = function() {};`)
  209. // }
  210. })
  211. //}
  212. }()
  213. }
  214. // 有就点 没有就不点击
  215. func MustClick(page *rod.Page, xpath string) {
  216. x := MustElementsX(page, xpath)
  217. for _, element := range x {
  218. visible, _ := element.Visible()
  219. if visible {
  220. element.MustClick()
  221. break
  222. }
  223. }
  224. }
  225. // 获取 xpath 对应元素的文本,顺便去掉空白字符。
  226. //
  227. // Deprecated: MustElementX 在没有找到元素时会返回 nil,导致 MustText panic
  228. // 空指针异常,和期望错误(元素未找到)不一致。
  229. func MustText(page *rod.Page, xpath string) string {
  230. //去掉左右空格
  231. return strings.TrimSpace(MustElementX(page, xpath).MustText())
  232. }
  233. // 输入文字
  234. func Input(p *rod.Page, selector, inputVal string) {
  235. err := rod.Try(func() {
  236. if inputVal == "" || inputVal == "*" {
  237. inputVal = "0"
  238. }
  239. e := rod.Try(func() {
  240. p.Timeout(time.Second).MustSearch(selector).MustSelectAllText().MustFrame().Keyboard.MustType(input.Backspace)
  241. })
  242. if e != nil {
  243. if w := errors.Unwrap(e); w != nil {
  244. logger.Info(w.Error() + "-" + selector + "-" + inputVal)
  245. } else {
  246. logger.Info(e.Error() + "-" + selector + "-" + inputVal)
  247. }
  248. }
  249. e = rod.Try(func() {
  250. p.Timeout(time.Second).MustSearch(selector).MustSelectAllText().MustInput(inputVal)
  251. })
  252. if e != nil {
  253. if w := errors.Unwrap(e); w != nil {
  254. logger.Info(strings.Split(w.Error(), "\t")[0] + "-" + selector + "-" + inputVal)
  255. } else {
  256. logger.Info(strings.Split(e.Error(), "\t")[0] + "-" + selector + "-" + inputVal)
  257. }
  258. }
  259. })
  260. if err != nil {
  261. logger.Info(err.Error() + "-" + selector + "-" + inputVal)
  262. }
  263. }
  264. // 输入文字
  265. func MustInputX(p *rod.Page, xPath, inputVal string) {
  266. e := MustElementX(p, xPath)
  267. _ = e.ScrollIntoView()
  268. e.MustClick().MustSelectAllText().MustInput(inputVal)
  269. }
  270. // 拦截请求
  271. func PageHijackReq(b *rod.Page, pattern string, ch chan []byte) *rod.HijackRouter {
  272. router := b.HijackRequests()
  273. router.MustAdd(pattern, func(ctx *rod.Hijack) {
  274. rod.Try(func() {
  275. ctx.MustLoadResponse()
  276. ch <- []byte(ctx.Response.Body())
  277. utils.Sleep(5)
  278. select {
  279. case <-ch:
  280. break
  281. }
  282. })
  283. })
  284. go router.Run()
  285. return router
  286. }
  287. // 获取拦截请求的值
  288. func GetHijackResp(ch chan []byte, t float64) ([]byte, error) {
  289. d := time.Duration(t * float64(time.Second))
  290. select {
  291. case val := <-ch:
  292. return val, nil
  293. case <-time.After(d):
  294. return nil, errors.New("请求网页超时,请稍后重试!")
  295. }
  296. }
  297. // WaitForPageLoad 等待页面完全加载,selector 为等待某个元素加载的选择器,可选
  298. func WaitForPageLoad(page *rod.Page, timeoutDuration time.Duration, selector ...string) {
  299. start := time.Now()
  300. // 1. 等待页面 readyState 为 "complete",加上超时控制
  301. for {
  302. readyState := page.MustEval(`() => document.readyState`).String()
  303. if readyState == "complete" {
  304. break // 页面加载完成,退出循环
  305. }
  306. // 检查是否超时
  307. if time.Since(start) > timeoutDuration {
  308. break
  309. }
  310. time.Sleep(500 * time.Millisecond) // 每次检查之间等待一段时间
  311. }
  312. // 2. 等待页面元素稳定
  313. page.MustWaitStable()
  314. // 3. 如果传入了 selector,等待特定元素加载完成
  315. if len(selector) > 0 {
  316. rod.Try(func() {
  317. page.Timeout(timeoutDuration).MustElement(selector[0]) // 使用传入的选择器等待元素
  318. })
  319. }
  320. // 4. 等待所有网络请求结束
  321. rod.Try(func() {
  322. page.Timeout(timeoutDuration).MustWaitIdle()
  323. })
  324. utils.Sleep(3)
  325. }
  326. func WaitHasX(page *rod.Page, xpath string) (x bool) {
  327. for i := 0; i < 5; i++ {
  328. x, _, _ = HasX(page, xpath)
  329. if !x {
  330. utils.Sleep(1)
  331. }
  332. }
  333. return x
  334. }
  335. func SaveErrImg(p *rod.Page, info models.CompanyInfo) string {
  336. var check_path string
  337. check_path = "./data/sbImg/" + info.TaxNo + "/"
  338. if !PathExists(check_path) {
  339. os.MkdirAll(check_path, os.ModePerm)
  340. }
  341. check_path = check_path + info.Period + "-" + time.Now().Format("2006-01-02-15-04-05") + ".png"
  342. check_path2 := ""
  343. _ = rod.Try(func() {
  344. p.Timeout(ClickTimeOut).MustScreenshot(check_path)
  345. watermark.Add(check_path)
  346. check_path2 = PostSbjt(info.TaxNo, info.Period, check_path)
  347. os.Remove(check_path)
  348. })
  349. return check_path2
  350. }
  351. // InputWithTimeout 规定时间内填入输入值,默认5s
  352. func InputWithTimeout(p *rod.Page, selector, inputVal string, timeout ...int64) {
  353. var t int64
  354. if len(timeout) == 0 {
  355. t = 5
  356. } else {
  357. t = timeout[0]
  358. }
  359. p.Timeout(time.Duration(t) * time.Second).MustSearch(selector).MustSelectAllText().MustInput(inputVal)
  360. }
  361. func SaveErrImgTask(p *rod.Page, info *models.TaxTask) string {
  362. var check_path string
  363. check_path = "./data/sbImg/" + info.TaxNo + "/"
  364. if !PathExists(check_path) {
  365. os.MkdirAll(check_path, os.ModePerm)
  366. }
  367. check_path = check_path + info.Period + "-" + time.Now().Format("2006-01-02-15-04-05") + ".png"
  368. var check_path2 string
  369. _ = rod.Try(func() {
  370. p.Timeout(ClickTimeOut).MustScreenshot(check_path)
  371. watermark.Add(check_path)
  372. check_path2 = PostSbjt(info.TaxNo, info.Period, check_path)
  373. os.Remove(check_path)
  374. })
  375. return check_path2
  376. }
  377. // 使用键盘模拟输入
  378. func InputStrNew(page *rod.Page, el *rod.Element, str string) {
  379. el.MustScrollIntoView()
  380. box := el.MustShape().Box()
  381. Click(page, box.X+(box.Width/2), box.Y+(box.Height/2))
  382. time.Sleep(time.Millisecond * 100)
  383. selectAll2(page)
  384. time.Sleep(time.Millisecond * 100)
  385. _ = page.Keyboard.Type(input.Delete)
  386. _ = page.Keyboard.Type(input.Backspace)
  387. time.Sleep(time.Millisecond * 100)
  388. _ = page.Keyboard.Type(input.Backspace)
  389. for _, v := range str {
  390. _ = page.Keyboard.Type(input.Key(v))
  391. time.Sleep(time.Millisecond * 100)
  392. }
  393. }
  394. func Click(page *rod.Page, x, y float64) {
  395. page.Mouse.MustMoveTo(x, y)
  396. time.Sleep(time.Millisecond * 100)
  397. page.Mouse.MustDown("left")
  398. page.Mouse.MustUp("left")
  399. }
  400. func selectAll2(page *rod.Page) {
  401. page.Keyboard.MustType(input.ControlLeft).MustType('a').MustType('a').MustType(input.ControlLeft)
  402. }
  403. // 输入文字Str
  404. func InputStr(p *rod.Page, selector, inputVal string) {
  405. err := rod.Try(func() {
  406. e := rod.Try(func() {
  407. p.Timeout(time.Duration(1 * float64(time.Second))).MustSearch(selector).MustSelectAllText().MustFrame().Keyboard.MustType(input.Backspace)
  408. })
  409. if e != nil {
  410. logger.Info(e.Error() + "-" + selector + "-" + inputVal)
  411. }
  412. e = rod.Try(func() {
  413. p.Timeout(time.Duration(1 * float64(time.Second))).MustSearch(selector).MustSelectAllText().MustInput(inputVal)
  414. })
  415. if e != nil {
  416. logger.Info(strings.Split(e.Error(), "\t")[0] + "-" + selector + "-" + inputVal)
  417. }
  418. })
  419. if err != nil {
  420. logger.Info(err.Error() + "-" + selector + "-" + inputVal)
  421. }
  422. }
  423. // 输入文字 string
  424. func InputElementXStr(p *rod.Page, xPath, inputVal string) {
  425. elementX := MustElementX(p, xPath)
  426. disabled := elementX.MustAttribute("disabled")
  427. if disabled != nil {
  428. return
  429. }
  430. readonly := elementX.MustAttribute("readonly")
  431. if readonly != nil {
  432. return
  433. }
  434. elementX.MustSelectAllText().MustInput(inputVal)
  435. }
  436. // 等待 xpath 对应的元素加载,相当于:
  437. //
  438. // _ = rod.Try(func() { page.Timeout(times * time.Second).MustElementX(xpath).MustWaitVisible() })
  439. //
  440. // 注意:加载失败返回为 nil,不做处理的话很容易引起空指针 panic。
  441. //
  442. // Deprecated: 直接替换成上面的代码即可。
  443. func WaitElementXFoTimesNil(page *rod.Page, xpath string, times int) *rod.Element {
  444. for i := 0; i < times; i++ {
  445. if has, el, _ := HasX(page, xpath); has && MustElementX(page, xpath).MustVisible() {
  446. return el
  447. }
  448. time.Sleep(time.Second)
  449. }
  450. return nil
  451. }
  452. func MustElementX10(page *rod.Page, xpath string) *rod.Element {
  453. for i := 0; i < 10; i++ {
  454. x, _ := ElementX(page, xpath)
  455. if x != nil {
  456. return x
  457. }
  458. utils.Sleep(1)
  459. }
  460. return nil
  461. }
  462. func MustElementX15(page *rod.Page, xpath string) *rod.Element {
  463. for i := 0; i < 15; i++ {
  464. x, _ := ElementX(page, xpath)
  465. if x != nil {
  466. return x
  467. }
  468. utils.Sleep(1)
  469. }
  470. return nil
  471. }
  472. func MustHasXV30(page *rod.Page, xpath string) bool {
  473. for _, element := range MustElementsX30(page, xpath) {
  474. visible, _ := element.Visible()
  475. if visible {
  476. return true
  477. }
  478. }
  479. return false
  480. }
  481. func MustElementsX30(page *rod.Page, xpath string) (x []*rod.Element) {
  482. for i := 0; i < 30; i++ {
  483. x, _ = ElementsX(page, xpath)
  484. if len(x) != 0 {
  485. break
  486. }
  487. utils.Sleep(1)
  488. }
  489. return x
  490. }
  491. // 等待 xpath 对应的元素加载,相当于:
  492. //
  493. // page.Timeout(times*time.Second).MustElementX(xpath).MustWaitVisible()
  494. //
  495. // 注意:加载失败 panic 的是税局卡顿错误。非税局采集任务不应使用该函数。
  496. //
  497. // Deprecated: 直接替换成上面的代码即可。超时错误有 HandleError 兜底。
  498. func WaitElementXFoTimes(page *rod.Page, xpath string, times int) *rod.Element {
  499. for i := 0; i < times; i++ {
  500. if has, el, _ := HasX(page, xpath); has && MustElementX(page, xpath).MustVisible() {
  501. return el
  502. }
  503. time.Sleep(time.Second)
  504. }
  505. logger.Info("页面加载失败元素未找到,请稍后再试", xpath)
  506. panic(taxerr.NewWebStuckTitle(InfoCscsts))
  507. }
  508. func MustElementsX10(page *rod.Page, xpath string) (x []*rod.Element) {
  509. for i := 0; i < 10; i++ {
  510. x, _ = ElementsX(page, xpath)
  511. if len(x) != 0 {
  512. break
  513. }
  514. utils.Sleep(1)
  515. }
  516. return x
  517. }
  518. func WaitMustElementsX(page *rod.Page, xpath string) (elementsX []*rod.Element) {
  519. for i := 0; i < 5; i++ {
  520. elementsX = MustElementsX(page, xpath)
  521. if len(elementsX) != 0 {
  522. return
  523. }
  524. utils.Sleep(1)
  525. }
  526. return
  527. }
  528. func ClickB(page *rod.Page, el string) {
  529. box := MustElementX(page, el).MustShape().Box()
  530. Click(page, box.X+(box.Width/2), box.Y+(box.Height/2))
  531. }
  532. // 等待 xpath 对应的元素加载,相当于:
  533. //
  534. // page.Timeout(10*time.Second).MustElementX(xpath).MustWaitVisible()
  535. //
  536. // 注意:加载失败 panic 的是税局卡顿错误。非税局采集任务不应使用该函数。
  537. //
  538. // Deprecated: 直接替换成上面的代码即可。超时错误有 HandleError 兜底。
  539. func WaitElementX10(page *rod.Page, xpath string) *rod.Element {
  540. return WaitElementXFoTimes(page, xpath, 10)
  541. }
  542. func MustElementX30(page *rod.Page, xpath string) *rod.Element {
  543. for i := 0; i < 30; i++ {
  544. x, _ := ElementX(page, xpath)
  545. if x != nil {
  546. return x
  547. }
  548. utils.Sleep(1)
  549. }
  550. return nil
  551. }
  552. func WaitHasX10(page *rod.Page, xpath string) (x bool) {
  553. for i := 0; i < 10; i++ {
  554. x, _, _ = HasX(page, xpath)
  555. if !x {
  556. utils.Sleep(1)
  557. }
  558. }
  559. return x
  560. }
  561. // 返回当前元素下一个元素的text 如果没有元素就直接返回空
  562. func MustNextText(page *rod.Page, xpath string) string {
  563. x := MustElementXV(page, xpath)
  564. if x == nil {
  565. return ""
  566. }
  567. return x.MustNext().MustText()
  568. }
  569. // 2033年12月31日 // 2033-12-31
  570. func DataFormat(data string) string {
  571. strings.ReplaceAll(data, "年", "-")
  572. strings.ReplaceAll(data, "月", "-")
  573. strings.ReplaceAll(data, "日", "")
  574. if len(data) > 10 {
  575. data = data[:10]
  576. }
  577. return data
  578. }
  579. // 返回当前元素下一个元素的text 如果没有元素就直接返回空
  580. func MustNext2Text(page *rod.Page, xpath string) string {
  581. x := MustElementXV(page, xpath)
  582. if x == nil {
  583. return ""
  584. }
  585. return x.MustNext().MustNext().MustText()
  586. }
  587. // 拦截
  588. func HijackReq(page *rod.Page, uri, xPath string) []byte {
  589. ch := make(chan []byte)
  590. r := PageHijackReq(page, uri, ch)
  591. MustElementX(page, xPath).MustClick()
  592. data, err := GetHijackResp(ch, 10)
  593. if err != nil {
  594. panic(taxerr.NewWebStuckTitle(false))
  595. }
  596. _ = r.Stop()
  597. return data
  598. }
  599. // 输入文字 float
  600. func InputElementX(p *rod.Page, xPath, inputVal string) {
  601. disabled := MustElementX(p, xPath).MustAttribute("disabled")
  602. if disabled != nil {
  603. return
  604. }
  605. readonly := MustElementX(p, xPath).MustAttribute("readonly")
  606. if readonly != nil {
  607. return
  608. }
  609. float := StrToFloat(inputVal)
  610. if float == 0 {
  611. inputVal = ""
  612. } else {
  613. inputVal = FloatToStr(float)
  614. }
  615. MustElementX(p, xPath).MustSelectAllText().MustInput(inputVal)
  616. }
  617. func GetText(page *rod.Page, selector string) string {
  618. return page.Timeout(ClickTimeOut).MustSearch(selector).MustText()
  619. }
  620. func WaitHasX15(page *rod.Page, xpath string) (x bool) {
  621. for i := 0; i < 15; i++ {
  622. x, _, _ = HasX(page, xpath)
  623. if !x {
  624. utils.Sleep(1)
  625. }
  626. }
  627. return x
  628. }
  629. // 这个方法存在bug 如果新页面没加载完,爬出异常了,就会存在新页面没关闭的情况
  630. func WaitTimeOutPage(f func() *rod.Page, timeout time.Duration) *rod.Page {
  631. resultChan := make(chan *rod.Page, 1)
  632. go func() {
  633. page := f()
  634. resultChan <- page
  635. }()
  636. select {
  637. case p := <-resultChan:
  638. return p
  639. case <-time.After(timeout):
  640. panic(taxerr.NewWebStuckTitle(ComInfo.Cscsts))
  641. }
  642. }
  643. func MustHasXV10(page *rod.Page, xpath string) bool {
  644. for _, element := range MustElementsX10(page, xpath) {
  645. visible, _ := element.Visible()
  646. if visible {
  647. return true
  648. }
  649. }
  650. return false
  651. }
  652. // 输入文字 string 有的公司有 有的公司没有 工商
  653. func InputElementXStrOrNo(p *rod.Page, xPath, inputVal string) {
  654. elementX := MustElementX(p, xPath)
  655. if elementX == nil {
  656. return
  657. }
  658. disabled := elementX.MustAttribute("disabled")
  659. if disabled != nil {
  660. return
  661. }
  662. readonly := elementX.MustAttribute("readonly")
  663. if readonly != nil {
  664. return
  665. }
  666. elementX.MustSelectAllText().MustInput(inputVal)
  667. }
  668. func CheckLogin(page *rod.Page) error {
  669. for i := 0; i < 30; i++ {
  670. if MustHasXV1(page, `//div[@class='leftMain']//div[contains(@class,'title')]`) {
  671. break
  672. }
  673. if MustHasXV1(page, `//div[@aria-label="密码修改"]`) {
  674. return taxerr.NewUserV3("税局强制要求修改密码", "请修改后重新发起")
  675. }
  676. if MustHasXV1(page, `//div[text()="账户中心"]`) {
  677. return taxerr.NewUserV3("登录后不能进入税局首页", "请检查后重新发起")
  678. }
  679. if MustHasXV1(page, `//div[@aria-label="用户协议"]//span[text()="确认"]`) {
  680. MustClick(page, `//div[@aria-label="用户协议"]//span[text()="确认"]`)
  681. }
  682. if MustHasXV1(page, `//div[contains(text(),"为提升纳税人办税体验")]/../..//span[text()="确 定"]`) {
  683. MustClick(page, `//div[contains(text(),"为提升纳税人办税体验")]/../..//span[text()="确 定"]`)
  684. }
  685. utils.Sleep(1)
  686. }
  687. if !MustHasXV1(page, `//div[@class='leftMain']//div[contains(@class,'title')]`) {
  688. return taxerr.NewSystemV3("税局登录异常", "请稍后重试")
  689. }
  690. i := 0
  691. for {
  692. name := MustText(page, `//div[@class='leftMain']//div[contains(@class,'title')]`)
  693. if name != "" {
  694. return nil
  695. }
  696. utils.Sleep(1)
  697. if i > 30 {
  698. return taxerr.NewSystemV3("税局登录异常", "请稍后重试")
  699. }
  700. i++
  701. }
  702. }