rod_utils.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  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. // 获取所有 iframe 中 xpath 对应元素的文本,顺便去掉空白字符。
  226. //
  227. // 注意: MustElementX 在没有找到元素时会返回 nil,导致
  228. // MustText panic 空指针异常,和期望错误(元素未找到)不一致。
  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. err := 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. if err != nil {
  376. logger.Error("截图失败", err)
  377. }
  378. return check_path2
  379. }
  380. // 使用键盘模拟输入
  381. func InputStrNew(page *rod.Page, el *rod.Element, str string) {
  382. el.MustScrollIntoView()
  383. box := el.MustShape().Box()
  384. Click(page, box.X+(box.Width/2), box.Y+(box.Height/2))
  385. time.Sleep(time.Millisecond * 100)
  386. selectAll2(page)
  387. time.Sleep(time.Millisecond * 100)
  388. _ = page.Keyboard.Type(input.Delete)
  389. _ = page.Keyboard.Type(input.Backspace)
  390. time.Sleep(time.Millisecond * 100)
  391. _ = page.Keyboard.Type(input.Backspace)
  392. for _, v := range str {
  393. _ = page.Keyboard.Type(input.Key(v))
  394. time.Sleep(time.Millisecond * 100)
  395. }
  396. }
  397. func Click(page *rod.Page, x, y float64) {
  398. page.Mouse.MustMoveTo(x, y)
  399. time.Sleep(time.Millisecond * 100)
  400. page.Mouse.MustDown("left")
  401. page.Mouse.MustUp("left")
  402. }
  403. func selectAll2(page *rod.Page) {
  404. page.Keyboard.MustType(input.ControlLeft).MustType('a').MustType('a').MustType(input.ControlLeft)
  405. }
  406. // 输入文字Str
  407. func InputStr(p *rod.Page, selector, inputVal string) {
  408. err := rod.Try(func() {
  409. e := rod.Try(func() {
  410. p.Timeout(time.Duration(1 * float64(time.Second))).MustSearch(selector).MustSelectAllText().MustFrame().Keyboard.MustType(input.Backspace)
  411. })
  412. if e != nil {
  413. logger.Info(e.Error() + "-" + selector + "-" + inputVal)
  414. }
  415. e = rod.Try(func() {
  416. p.Timeout(time.Duration(1 * float64(time.Second))).MustSearch(selector).MustSelectAllText().MustInput(inputVal)
  417. })
  418. if e != nil {
  419. logger.Info(strings.Split(e.Error(), "\t")[0] + "-" + selector + "-" + inputVal)
  420. }
  421. })
  422. if err != nil {
  423. logger.Info(err.Error() + "-" + selector + "-" + inputVal)
  424. }
  425. }
  426. // 输入文字 string
  427. func InputElementXStr(p *rod.Page, xPath, inputVal string) {
  428. elementX := MustElementX(p, xPath)
  429. disabled := elementX.MustAttribute("disabled")
  430. if disabled != nil {
  431. return
  432. }
  433. readonly := elementX.MustAttribute("readonly")
  434. if readonly != nil {
  435. return
  436. }
  437. elementX.MustSelectAllText().MustInput(inputVal)
  438. }
  439. // 等待所有 iframe 中 xpath 对应的元素加载。
  440. // 注意:加载失败返回为 nil,不做处理的话很容易引起空指针 panic。
  441. func WaitElementXFoTimesNil(page *rod.Page, xpath string, times int) *rod.Element {
  442. for i := 0; i < times; i++ {
  443. if has, el, _ := HasX(page, xpath); has && MustElementX(page, xpath).MustVisible() {
  444. return el
  445. }
  446. time.Sleep(time.Second)
  447. }
  448. return nil
  449. }
  450. func MustElementX10(page *rod.Page, xpath string) *rod.Element {
  451. for i := 0; i < 10; i++ {
  452. x, _ := ElementX(page, xpath)
  453. if x != nil {
  454. return x
  455. }
  456. utils.Sleep(1)
  457. }
  458. return nil
  459. }
  460. func MustElementX15(page *rod.Page, xpath string) *rod.Element {
  461. for i := 0; i < 15; i++ {
  462. x, _ := ElementX(page, xpath)
  463. if x != nil {
  464. return x
  465. }
  466. utils.Sleep(1)
  467. }
  468. return nil
  469. }
  470. func MustHasXV30(page *rod.Page, xpath string) bool {
  471. for _, element := range MustElementsX30(page, xpath) {
  472. visible, _ := element.Visible()
  473. if visible {
  474. return true
  475. }
  476. }
  477. return false
  478. }
  479. func MustElementsX30(page *rod.Page, xpath string) (x []*rod.Element) {
  480. for i := 0; i < 30; i++ {
  481. x, _ = ElementsX(page, xpath)
  482. if len(x) != 0 {
  483. break
  484. }
  485. utils.Sleep(1)
  486. }
  487. return x
  488. }
  489. // 等待所有 iframe 中 xpath 对应的元素加载。
  490. // 注意:加载失败 panic 的是税局卡顿错误。非税局采集任务不应使用该函数。
  491. func WaitElementXFoTimes(page *rod.Page, xpath string, times int) *rod.Element {
  492. for i := 0; i < times; i++ {
  493. if has, el, _ := HasX(page, xpath); has && MustElementX(page, xpath).MustVisible() {
  494. return el
  495. }
  496. time.Sleep(time.Second)
  497. }
  498. logger.Info("页面加载失败元素未找到,请稍后再试", xpath)
  499. panic(taxerr.NewWebStuckTitle(InfoCscsts))
  500. }
  501. func MustElementsX10(page *rod.Page, xpath string) (x []*rod.Element) {
  502. for i := 0; i < 10; i++ {
  503. x, _ = ElementsX(page, xpath)
  504. if len(x) != 0 {
  505. break
  506. }
  507. utils.Sleep(1)
  508. }
  509. return x
  510. }
  511. func WaitMustElementsX(page *rod.Page, xpath string) (elementsX []*rod.Element) {
  512. for i := 0; i < 5; i++ {
  513. elementsX = MustElementsX(page, xpath)
  514. if len(elementsX) != 0 {
  515. return
  516. }
  517. utils.Sleep(1)
  518. }
  519. return
  520. }
  521. func ClickB(page *rod.Page, el string) {
  522. box := MustElementX(page, el).MustShape().Box()
  523. Click(page, box.X+(box.Width/2), box.Y+(box.Height/2))
  524. }
  525. // 等待所有 iframe 中 xpath 对应的元素加载。
  526. // 注意:加载失败 panic 的是税局卡顿错误。非税局采集任务不应使用该函数。
  527. func WaitElementX10(page *rod.Page, xpath string) *rod.Element {
  528. return WaitElementXFoTimes(page, xpath, 10)
  529. }
  530. func MustElementX30(page *rod.Page, xpath string) *rod.Element {
  531. for i := 0; i < 30; i++ {
  532. x, _ := ElementX(page, xpath)
  533. if x != nil {
  534. return x
  535. }
  536. utils.Sleep(1)
  537. }
  538. return nil
  539. }
  540. func WaitHasX10(page *rod.Page, xpath string) (x bool) {
  541. for i := 0; i < 10; i++ {
  542. x, _, _ = HasX(page, xpath)
  543. if !x {
  544. utils.Sleep(1)
  545. }
  546. }
  547. return x
  548. }
  549. // 返回当前元素下一个元素的text 如果没有元素就直接返回空
  550. func MustNextText(page *rod.Page, xpath string) string {
  551. x := MustElementXV(page, xpath)
  552. if x == nil {
  553. return ""
  554. }
  555. return x.MustNext().MustText()
  556. }
  557. // 2033年12月31日 // 2033-12-31
  558. func DataFormat(data string) string {
  559. strings.ReplaceAll(data, "年", "-")
  560. strings.ReplaceAll(data, "月", "-")
  561. strings.ReplaceAll(data, "日", "")
  562. if len(data) > 10 {
  563. data = data[:10]
  564. }
  565. return data
  566. }
  567. // 返回当前元素下一个元素的text 如果没有元素就直接返回空
  568. func MustNext2Text(page *rod.Page, xpath string) string {
  569. x := MustElementXV(page, xpath)
  570. if x == nil {
  571. return ""
  572. }
  573. return x.MustNext().MustNext().MustText()
  574. }
  575. // 拦截
  576. func HijackReq(page *rod.Page, uri, xPath string) []byte {
  577. ch := make(chan []byte)
  578. r := PageHijackReq(page, uri, ch)
  579. MustElementX(page, xPath).MustClick()
  580. data, err := GetHijackResp(ch, 10)
  581. if err != nil {
  582. panic(taxerr.NewWebStuckTitle(false))
  583. }
  584. _ = r.Stop()
  585. return data
  586. }
  587. // 输入文字 float
  588. func InputElementX(p *rod.Page, xPath, inputVal string) {
  589. disabled := MustElementX(p, xPath).MustAttribute("disabled")
  590. if disabled != nil {
  591. return
  592. }
  593. readonly := MustElementX(p, xPath).MustAttribute("readonly")
  594. if readonly != nil {
  595. return
  596. }
  597. float := StrToFloat(inputVal)
  598. if float == 0 {
  599. inputVal = ""
  600. } else {
  601. inputVal = FloatToStr(float)
  602. }
  603. MustElementX(p, xPath).MustSelectAllText().MustInput(inputVal)
  604. }
  605. func GetText(page *rod.Page, selector string) string {
  606. return page.Timeout(ClickTimeOut).MustSearch(selector).MustText()
  607. }
  608. func WaitHasX15(page *rod.Page, xpath string) (x bool) {
  609. for i := 0; i < 15; i++ {
  610. x, _, _ = HasX(page, xpath)
  611. if !x {
  612. utils.Sleep(1)
  613. }
  614. }
  615. return x
  616. }
  617. // 这个方法存在bug 如果新页面没加载完,爬出异常了,就会存在新页面没关闭的情况
  618. func WaitTimeOutPage(f func() *rod.Page, timeout time.Duration) *rod.Page {
  619. resultChan := make(chan *rod.Page, 1)
  620. go func() {
  621. page := f()
  622. resultChan <- page
  623. }()
  624. select {
  625. case p := <-resultChan:
  626. return p
  627. case <-time.After(timeout):
  628. panic(taxerr.NewWebStuckTitle(ComInfo.Cscsts))
  629. }
  630. }
  631. func MustHasXV10(page *rod.Page, xpath string) bool {
  632. for _, element := range MustElementsX10(page, xpath) {
  633. visible, _ := element.Visible()
  634. if visible {
  635. return true
  636. }
  637. }
  638. return false
  639. }
  640. // 输入文字 string 有的公司有 有的公司没有 工商
  641. func InputElementXStrOrNo(p *rod.Page, xPath, inputVal string) {
  642. elementX := MustElementX(p, xPath)
  643. if elementX == nil {
  644. return
  645. }
  646. disabled := elementX.MustAttribute("disabled")
  647. if disabled != nil {
  648. return
  649. }
  650. readonly := elementX.MustAttribute("readonly")
  651. if readonly != nil {
  652. return
  653. }
  654. elementX.MustSelectAllText().MustInput(inputVal)
  655. }
  656. func CheckLogin(page *rod.Page) error {
  657. for i := 0; i < 30; i++ {
  658. if MustHasXV1(page, `//div[@class='leftMain']//div[contains(@class,'title')]`) {
  659. break
  660. }
  661. if MustHasXV1(page, `//div[@aria-label="密码修改"]`) {
  662. return taxerr.NewUserV3("税局强制要求修改密码", "请修改后重新发起")
  663. }
  664. if MustHasXV1(page, `//div[text()="账户中心"]`) {
  665. return taxerr.NewUserV3("登录后不能进入税局首页", "请检查后重新发起")
  666. }
  667. if MustHasXV1(page, `//div[@aria-label="用户协议"]//span[text()="确认"]`) {
  668. MustClick(page, `//div[@aria-label="用户协议"]//span[text()="确认"]`)
  669. }
  670. if MustHasXV1(page, `//div[contains(text(),"为提升纳税人办税体验")]/../..//span[text()="确 定"]`) {
  671. MustClick(page, `//div[contains(text(),"为提升纳税人办税体验")]/../..//span[text()="确 定"]`)
  672. }
  673. utils.Sleep(1)
  674. }
  675. if !MustHasXV1(page, `//div[@class='leftMain']//div[contains(@class,'title')]`) {
  676. return taxerr.NewSystemV3("税局登录异常", "请稍后重试")
  677. }
  678. i := 0
  679. for {
  680. name := MustText(page, `//div[@class='leftMain']//div[contains(@class,'title')]`)
  681. if name != "" {
  682. return nil
  683. }
  684. utils.Sleep(1)
  685. if i > 30 {
  686. return taxerr.NewSystemV3("税局登录异常", "请稍后重试")
  687. }
  688. i++
  689. }
  690. }