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中同步运行循环_Python_Selenium_Asynchronous_Selenium Webdriver_Synchronous - Fatal编程技术网

python中同步运行循环

python中同步运行循环,python,selenium,asynchronous,selenium-webdriver,synchronous,Python,Selenium,Asynchronous,Selenium Webdriver,Synchronous,我有一个代码块,它在一个无限高的网站上爬行(比如FACEBOOK) Python selenium脚本要求页面javascript转到页面底部,以便进一步向下加载页面。但最终,循环会异步运行,网站的速率限制器会阻止脚本 我需要页面先等待页面加载,然后再继续,但我没有做到这一点 以下是我迄今为止一直在尝试的事情 守则如下: while int(number_of_news) != int(len(news)) : driver.execute_script("window.scrollTo

我有一个代码块,它在一个无限高的网站上爬行
(比如FACEBOOK)

Python selenium脚本要求页面javascript转到页面底部,以便进一步向下加载页面。但最终,循环会异步运行,网站的速率限制器会阻止脚本

我需要页面先等待页面加载,然后再继续,但我没有做到这一点

以下是我迄今为止一直在尝试的事情

守则如下:

while int(number_of_news) != int(len(news)) :
    driver.execute_script("window.scrollTo(document.body.scrollHeight/2, document.body.scrollHeight);")
    news = driver.find_elements_by_class_name("news-text")
    print(len(news))
输出类似于

我将其解释为当值为
43,63。。。等等

我也试着让它递归,但结果还是一样的。递归代码如下所示:

def call_news(_driver, _news, _number_of_news):
    _driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    _news = driver.find_elements_by_class_name("news-text")
    print(len(_news))
    if int(len(_news)) != int(number_of_news) :
        call_news(_driver, _news, _number_of_news)
    else :
        return _news

欢迎提供任何提示。

您可以设置
页面加载\u超时
使驱动程序等待页面加载

driver.set_page_load_timeout(10)
另一种选择是等待元素的数量改变

current_number_of_news = 0
news = []
while int(number_of_news) != int(len(news)) :
    driver.execute_script("window.scrollTo(document.body.scrollHeight/2, document.body.scrollHeight);")
    while (current_number_of_news == len(news)) :
        news = driver.find_elements_by_class_name("news-text")
    current_number_of_news = len(news)
    print(len(news))

在每次滚动之后,您是否有一些元素,您可以在这些元素上设置一个等待,以便在加载新的新闻项目后可见。例如,用于滚动更多或某些唯一类以查看最后一条新闻的指令文本等。@Grasshopper问题在于滚动时我没有唯一的类/id名称。我所拥有的只是一个特定类名“news text”的元素,在每个滚动之后等待该类的所有元素如何?不知道python中的确切方法,但在java中,您可以等待定位器的所有元素的可见性或存在性。您能再解释一下您的答案吗。@CodeGirl这将使驱动程序最多等待10秒以加载页面。在驱动程序创建后定义一次。不完全是。。。这意味着如果页面在10秒内没有加载,它将抛出一个错误。因此,如果页面在10秒内加载,它将不会等待该时间。另外,这不适用于像滚动启动的异步调用。@Grasshopper请再次阅读我的评论。我没说会一直等下去。@伙计,但页面已经加载了。。。滚动时,元素不断添加到页面中。就像Facebook:帖子在向下滚动时不断添加。页面最初已加载。