Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python selenium webdriver显式等待连接错误_Python_Selenium_Selenium Webdriver - Fatal编程技术网

Python selenium webdriver显式等待连接错误

Python selenium webdriver显式等待连接错误,python,selenium,selenium-webdriver,Python,Selenium,Selenium Webdriver,我正在运行的脚本不会在运行之间返回一致的数据。我相信当我遍历页面时,并不是等待所有页面全部加载Javascript和AJAX。为了解决此问题,我在下面添加了显式等待,但它返回以下错误: ConnectionRefusedError:[WinError 10061]无法建立连接,因为目标计算机主动拒绝了它 隐式等待将在没有错误的情况下运行,但它也不会返回一致的源代码。我假设您在browser.page\u source上遇到此异常 这是因为您正在执行browser.quit Quit()-用于关闭

我正在运行的脚本不会在运行之间返回一致的数据。我相信当我遍历页面时,并不是等待所有页面全部加载Javascript和AJAX。为了解决此问题,我在下面添加了显式等待,但它返回以下错误:

ConnectionRefusedError:[WinError 10061]无法建立连接,因为目标计算机主动拒绝了它


隐式等待将在没有错误的情况下运行,但它也不会返回一致的源代码。

我假设您在browser.page\u source上遇到此异常 这是因为您正在执行browser.quit Quit()-用于关闭web驱动程序实例或销毁web驱动程序


您需要在退出之前获取页面源,方法是在try块末尾添加print语句,或在打印后移动quit:)

这是正确的。我的理解是,只有在超过等待时间的情况下才会调用Quit(),但事实并非如此。不,finally块是一种确保始终执行其中代码的方法,请删除并查看它将如何执行work@Farmer将
最后
替换为
捕获
,您可能会得到您想要的行为。您可能需要花一些时间阅读
try catch finally
上的文档,以便更好地理解它们的工作原理。@JeffC,在
Python
中,实际上是
try,而不是else finally
构造。。。OP应将
最后
替换为
,除了
@Andersson ah。。。我不是巨蟒。我应该查找它,而不是假设它与Java和C#相同。谢谢。由于
browser.quit()
,您无法获取页面源代码。请注意,
finally
运算符之后的代码块始终执行。因此,您可能需要在
browser.quit()之前执行
browser.page\u source
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

browser = webdriver.Chrome()
url = 'http://www.website.com'
browser.get(url)
try:
    element = WebDriverWait(browser, 10).until(EC.presence_of_all_elements_located((By.ID, "results-main")))
finally:
    browser.quit()

print (browser.page_source)