如何使Selenium WebDriver不等待整个页面加载 [英] How to make Selenium WebDriver not to wait for whole page to load

查看:1251
本文介绍了如何使Selenium WebDriver不等待整个页面加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将python 2.7和Selenium webdriver用于Firefox,但遇到无法解决或无法在互联网上解决的问题. 我的任务是打开约1万个网页(adsl路由器的网络界面-通过ip地址)并上传新固件.我写了代码,但是为了完成它,我必须学习如何使Selenium Webdriver不再像永远一样等待页面加载,而是等待2分钟(这是新固件上载的时间),然后继续下一步. 我以为我可以让它永远等待(等待路由器重新连接-慢得多,但是我可以在没有帮助的情况下进行操作),但是要注意的是,当我单击上传"按钮时,它需要2分钟的时间来上传新固件,然后路由器重新启动以应用更改(少于2分钟),然后尝试连接(大约10秒钟),即使它获得了其他IP,它也将永远不会加载,并且我的程序将永远等待. 因此,我想跳过所有这些,并且希望程序在开始的2分钟后继续到下一个路由器.能做到吗我阅读了有关"pageLoadingStrategy"的内容,但我不明白如何使用它. 如果无法理解,请写信给我,因为英语不是我的母语.在下面,您可以看到代码示例,在button.submit()之后,它应等待2分钟然后继续执行,而不是永远等待:

I am using python 2.7 with Selenium webdriver for Firefox and I have a problem I can't solve or find solved on the internet. My task is to open around 10k web pages (adsl router web interfaces - via ip address) and upload new firmware. I wrote the code but in order to finish it I must learn how to make selenium webdriver not to wait for page load like forever, but to wait for 2 minutes(it is time needed for new firmware to upload) and then proceed to next step. I thought I let it wait for forever (wait for router to reconnect - much slower but doable by me without help) but the catch is when I click upload button it takes 2 minutes of uploading new firmware, then router reboots to apply changes (takes less then 2 minutes), then tries to connect (around 10 seconds) and even then if it gets some other IP it will never load and my program wait for forever. So, I want to skip all of that and I want program to proceed to next router after first 2 minutes. Can it be done? I read something about "pageLoadingStrategy" but I couldn't understand how to use it. Please write me if it is not understandable, because English is not my native language. Below you can see the code sample, after button.submit() it should wait for 2 minutes and proceed and not wait forever:

def firmware_upload():
  global ip
  br.get("http://"+ip+"/upload.html")
  button = br.find_element_by_xpath('//input[@type="file" and @name="filename"]')
  button.send_keys("/home/djura/Downloads/KW5815A_update_140417")
  button.submit()
  print ("Odradjen UPDATE SOFTWARE-a!")
  return

推荐答案

查看是否可行.

try
{
br.manage().timeouts().pageLoadTimeout(2, TimeUnit.MINUTES);
global ip
br.get("http://"+ip+"/upload.html")
button = br.find_element_by_xpath('//input[@type="file" and  @name="filename"]')
button.send_keys("/home/djura/Downloads/KW5815A_update_140417")
button.submit()
print ("Odradjen UPDATE SOFTWARE-a!")
return

}
catch(TimeoutException e)
{
print("2min over");
}

这篇关于如何使Selenium WebDriver不等待整个页面加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆