Selenium error message “selenium.webdriver has no attribute execute script”

前端 未结 3 717
北恋
北恋 2021-01-13 23:24

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 sele         


        
相关标签:
3条回答
  • 2021-01-13 23:37

    To make it work you have to create an instance of webdriver, e.g.:

    from selenium import webdriver
    
    driver = webdriver.Chrome() # webdriver.Ie(), webdriver.Firefox()...
    last_height = driver.execute_script("return document.body.scrollHeight")
    

    You can download Chromedriver from here

    You also need to add path to Chromedriver to your environment variable PATH or just put downloaded file into the same folder as your Python executable...

    0 讨论(0)
  • 2021-01-13 23:39
    AttributeError: module 'selenium.webdriver' has no attribute 'execute_script'
    

    You are getting this error because 'execute_script' is not a class attribute, you just can not use it directly. Since it is an instance attribute you should create an instance of the class. Please check here to learn more about classes.

    This will work fine now since 'execute_script' is running as an instance attribute.

    last_height = browser.execute_script("return document.body.scrollHeight")
    

    Your final code would have looked like this:

    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 = browser.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
    
    0 讨论(0)
  • 2021-01-13 23:56

    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()

    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().

    0 讨论(0)
提交回复
热议问题