Skip to content
Advertisements

Python Selenium

Example 1

from selenium import webdriver      # webdriver and selenium
import random
import time                         # time

browser = webdriver.Firefox()       # connect firefox

pageCount = 1

url = 'https://stackoverflow.com/questions/tagged/javascript?tab=newest&page='

while pageCount <= 10:
    randomPage = random.randint(5,10)
    newUrl = url + str(randomPage) + '&pagesize=15'
    browser.get(newUrl)
    time.sleep(5)
    pageCount += 1

browser.close()

Example 2

from selenium import webdriver      # webdriver and selenium
import random
import time                         # time

browser = webdriver.Firefox()       # connect firefox

pageCount  = 1
entries    = []
entryCount = 1

url = 'https://stackoverflow.com/questions/tagged/javascript?tab=newest&page='

while pageCount <= 10:
    randomPage = random.randint(5,10)
    newUrl = url + str(randomPage) + '&pagesize=15'
    browser.get(newUrl)
    elements = browser.find_elements_by_css_selector('a.question-hyperlink')
    for element in elements:
        entries.append(element.text)
    #time.sleep(3)
    pageCount += 1

for entry in entries:
    print(str(entryCount)+'------------------------------------------------------')
    print(entry)
    entryCount += 1

browser.close()
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: