python selenium chromedriver 自动化超时报错:你需要使用多标签保护罩护体

在使用selenium + chrome 作自动化测试的时候,有可能会出现网页连接超时的情况

如果出现网页连接超时,将会导致 webdriver 也跟着无法响应,不能继续进行任何操作

即时是去打开新的连接也会报Time out错误

那么如果有很多连接要去做弹窗抓取,却不想因为其中一两个页面超时而中断进程该怎么办呢?

这时候你需要一个备用标签做金刚保护罩来护体!

具体的思路是在打开需求页面之后,再次开启一个新的标签去访问一个一定不会超时的页面(如百度),此时窗口句柄不要切换,依旧按照自己的逻辑去操作,当出现页面超时情况的时候,关闭原有标签,设置主窗口句柄到百度页面所在的标签通过get访问后续的连接,同时在开启一个标签做保护罩即可。

相关代码实现:

  1. #!/usr/bin/python3
  2. # -*- coding: UTF-8 -*-
  3. import traceback
  4. from xml import etree
  5. from selenium import webdriver
  6. from selenium.webdriver.chrome.options import Options
  7. chrome_options = Options()
  8. prefs = {
  9. 'profile.default_content_setting_values': {
  10. 'images': 2, # 禁用图片的加载
  11. 'javascript': 2 ##禁用js,可能会导致通过js加载的互动数抓取失效
  12. }
  13. }
  14. chrome_options.add_experimental_option("prefs", prefs)
  15. # chrome_options.add_argument("--headless") # 不弹出浏览器
  16. browser = webdriver.Chrome(chrome_options=chrome_options)
  17. browser.implicitly_wait(5) # 操作、获取元素时的隐式等待时间
  18. browser.set_page_load_timeout(10) # 页面加载超时等待时间
  19. main_win = browser.current_window_handle #记录当前窗口的句柄
  20. all_win = browser.window_handles
  21. # 开始访问页面
  22. print('Opening page')
  23. urls = [] # 定义你想要抓取的全部的页面
  24. for url in urls:
  25. try:
  26. if len(all_win) == 1:
  27. print('弹出保护罩')
  28. js = 'window.open("https://www.baidu.com");'
  29. browser.execute_script(js)
  30. # 还是定位在main_win上的
  31. for win in all_win:
  32. if main_win != win:
  33. print('保护罩WIN', win, 'Main', main_win)
  34. browser.switch_to.window(main_win)
  35. browser.get(url) # 此处访问你需要的URL
  36. body = browser.page_source
  37. html = etree.HTML(body)
  38. # 下面是你的抓取逻辑 省略
  39. except:
  40. # 超时
  41. print('Time out')
  42. # 切换新的浏览器窗口
  43. for win in all_win:
  44. if main_win != win:
  45. print('保护罩WIN', win, 'Main', main_win)
  46. print '切换到保护罩'
  47. browser.close()
  48. browser.switch_to.window(win)
  49. main_win = win
  50. js = 'window.open("https://www.baidu.com");'
  51. browser.execute_script(js)
  52. if 'time' in str(traceback.format_exc()):
  53. print '页面访问超时'
点赞 ( 0 )

18 条评论

  1. Dewatogel

    Valuable info. Fortunate me I found your website by chance, and I am shocked why this twist of fate didn't happened in advance! I bookmarked it.

  2. Lunatogel

    My family every time say that I am killing my time here at net, however I know I am getting knowledge everyday by reading such nice articles.

  3. Dewatogel

    Terrific article! That is the kind of information that are meant to be shared around the internet. Shame on the search engines for no longer positioning this submit higher! Come on over and consult with my site . Thanks =)

  4. 바이낸스 가입

    That is really attention-grabbing, You are an excessively skilled blogger. I've joined your feed and stay up for seeking more of your magnificent post. Additionally, I've shared your site in my social networks

  5. 바이낸스 가입

    Pretty! This has been an incredibly wonderful article. Thank you for providing this info.

  6. 바이낸스 가입

    Howdy! I just wish to give you a big thumbs up for your great info you have got here on this post. I will be coming back to your website for more soon.

  7. 바이낸스 가입

    Appreciate the recommendation. Let me try it out.

  8. 바이비트 가입

    Spot on with this write-up, I truly believe that this web site needs much more attention. I'll probably be back again to read more, thanks for the advice!

  9. 바이낸스 가입

    My family all the time say that I am wasting my time here at web, except I know I am getting knowledge all the time by reading thes nice content.

  10. 바이비트 가입

    I think this is among the most vital info for me. And i am glad reading your article. But should remark on few general things, The web site style is perfect, the articles is really excellent : D. Good job, cheers

  11. 바이비트 가입

    It's hard to come by educated people in this particular topic, but you seem like you know what you're talking about! Thanks

  12. 바이낸스 가입

    An intriguing discussion is definitely worth comment. I do believe that you need to publish more about this topic, it may not be a taboo matter but usually people do not discuss these subjects. To the next! Cheers!!

  13. 바이낸스 가입

    Hello! I could have sworn I've been to this website before but after checking through some of the post I realized it's new to me. Anyways, I'm definitely glad I found it and I'll be bookmarking and checking back often!

  14. Bonus

    This post provides clear idea designed for the new people of blogging, that really how to do blogging.

  15. Roxana

    I do not even understand how I stopped up right here, but I assumed this post was once good. I do not recognise who you're however certainly you are going to a famous blogger in case you are not already. Cheers!

  16. Case Converter

    Hey! Would you mind if I share your blog with my twitter group? There's a lot of folks that I think would really enjoy your content. Please let me know. Cheers

  17. comet browser features

    Incredible! This blog looks exactly like my old one! It's on a totally different topic but it has pretty much the same layout and design. Outstanding choice of colors!

  18. WordCamp Asia 2026

    Hello friends, pleasant paragraph and fastidious arguments commented here, I am really enjoying by these.

发表评论

人生在世,错别字在所难免,无需纠正。

插入图片
s
返回顶部