硒错误消息"selenium.webdriver没有属性执行脚本". [英] Selenium error message "selenium.webdriver has no attribute execute script"

查看:261
本文介绍了硒错误消息"selenium.webdriver没有属性执行脚本".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用硒刮擦无限滚动页面.

I am using selenium to scrape an infinite scrolling page.

我正在尝试使用以下代码:

I am trying to use this code:

import time
import pandas as np
import numpy as np

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By

browser = webdriver.Chrome()
url = 'https://twitter.com/search?f=tweets&q=csubwaystats%20since%3A2018-05-28%20until%3A2018-08-28'

browser.get(url)
time.sleep(1)

SCROLL_PAUSE_TIME = 0.5

# Get scroll height
last_height = webdriver.execute_script("return document.body.scrollHeight")

while True:
    # Scroll down to bottom
    webdriver.execute_script("window.scrollTo(0,document.body.scrollHeight);")

    # Wait to load page
    time.sleep(SCROLL_PAUSE_TIME)

    # Calculate new scroll height and compare with last scroll height
    new_height = webdriver.execute_script("return document.body.scrollHeight")
    if new_height == last_height:
        break
    last_height = new_height

我从多个来源获得了此代码,最近的是:

I obtained this code from multiple sources, the most recent being:

如何我可以在python中使用Selenium Webdriver滚动网页吗?

我将其更新为包括"webdriver"而不是"driver",因为我将硒导入为webdriver.否则它将无法正常工作.

I updated it to include "webdriver" instead of "driver" because I import selenium as webdriver. It doesn't work otherwise.

我的问题是,当我运行代码时,我得到了:

My issue is that when I run the code I get:

AttributeError: module 'selenium.webdriver' has no attribute 'execute_script'

我不太了解这意味着什么以及如何解决?我还没有找到有关此方面的信息.

I don't really understand what this means and how to fix it? I haven't been able to find information on this.

我是python的新手,所以可能缺少一些明显的东西,但是任何建议都将不胜感激.

I am new to python and so am probably missing something obvious but any advice would be appreciated.

推荐答案

webdriver是模块的名称,而不是您的实例.实际上,您已使用以下行将创建的实例分配给名称browser:browser = webdriver.Chrome()

webdriver is the name of the module, not your instance of it. In fact, you assigned the instance you created to the name browser with this line: browser = webdriver.Chrome()

因此,您不必使用webdriver.execute_script()(会给您一个AttributeError),而必须使用实例来调用它,例如:browser.execute_script().

so instead of calling webdriver.execute_script() (which will give you an AttributeError), you must call it using your instance, like this: browser.execute_script().

这篇关于硒错误消息"selenium.webdriver没有属性执行脚本".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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