Selenium立即返回

时间:2018-04-21 05:36:42

标签: python selenium selenium-webdriver request

我现在正在编写一个脚本来在网站上下载文件,在Python(3.6)和Selenium上使用Firefox作为其驱动程序。但是当第一个请求的下载成功时,当我发出第二个请求时,驱动程序会立即返回。例如,

profile = webdriver.FirefoxProfile()
driver = webdriver.Firefox(firefox_profile=profile)

driver.get(url)
try:
    download_button = driver.find_element_by_xpath("//p[@class='download']/span")
    driver.execute_script("arguments[0].click();", download_button)
except NoSuchElementException:
    print("No such file exists")

driver.close()

在这种情况下,当我第一次发出请求时,上面的脚本有效。但是从我对同一个驱动程序的第二个请求,下载不起作用,因为driver.get(url)立即返回,因此在页面加载完成之前,try ~ except子句开始,这自然不会找到xpath组件,触发NoSuchElementException错误。

我认为有更好的方法,但我不喜欢在每个页面上打开一个新的驱动程序,因为它占用了大量的计算机资源(实际上是我当前的脚本)。另一种解决方法可能是使用driver.implicitly_wait(30),但我不确定页面加载是否在30秒内完成,而是想在页面加载完成后立即开始下载,这就是我的情况。第一次请求。

有没有办法存档?

0 个答案:

没有答案