1

I am trying to extract the url's for each restaurant from this page and I have written a python script for the same:

import time

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

browser = webdriver.Firefox()
browser.get("http://www.delyver.com/Partners/partner/HSR%20Layout,%20Bengaluru,%20Karnataka,%20India/12.9081357/77.64760799999999")

time.sleep(1)

elem = browser.find_element_by_tag_name("body")

no_of_pagedowns = 40

while no_of_pagedowns:
    elem.send_keys(Keys.PAGE_DOWN)
    time.sleep(0.2)
    no_of_pagedowns-=1

post1 = browser.find_elements_by_css_selector("Parwrsp.Parwrsp-Ado")


for post in post1:
    print post.get('href')  

When i run the script, the browser window opens and i maximize its window size to obtain focus and it automatically scrolls down. But nothing gets printed. I implemented selenium following this link.

What am i doing wrong?

Community
  • 1
  • 1
Bluesir9
  • 121
  • 5
  • 11

1 Answers1

0

Your current CSS selector would not match any element since Parwrsp is a class.

If you want to match multiple classes, write the selector this way:

.Parwrsp.Parwrsp-Ado

And, there is no get() method on a WebElement instance, you meant to use get_attribute():

posts = browser.find_elements_by_css_selector(".Parwrsp.Parwrsp-Ado")
for post in posts:
    print post.get_attribute('href')

Proof of the above making sense:

>>> from selenium import webdriver
>>> 
>>> browser = webdriver.Firefox()
>>> browser.get("http://www.delyver.com/Partners/partner/HSR%20Layout,%20Bengaluru,%20Karnataka,%20India/12.9081357/77.64760799999999")
>>> for post in browser.find_elements_by_css_selector(".Parwrsp.Parwrsp-Ado"):
...     print post.get_attribute('href')
... 
http://www.delyver.com/Partners/partnerdetailsview/947/Purnabramha,-HSR
http://www.delyver.com/Partners/partnerdetailsview/916/Moti-Mahal-Deluxe,-HSR-Layout
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195