nlper
nlper

Reputation: 2407

Getting link address using phantomjs

I am trying to get article urls for each div on this page : https://www.google.com/trends/home/all/IN I am able to get image link and title, but for article link it gives

Traceback (most recent call last):
  File "google.py", line 25, in getGooglerends
    print s.find_elements_by_class_name('image-wrapper').get_attribute('href')
AttributeError: 'list' object has no attribute 'get_attribute'

Code:

driver = webdriver.PhantomJS('/usr/local/bin/phantomjs')
driver.set_window_size(1124, 850)
driver.get("https://www.google.com/trends/home/all/IN")
trend = {}
def getGooglerends():
    try:
    #Does this line makes any sense
        #element = WebDriverWait(driver, 20).until(lambda driver: driver.find_elements_by_class_name('md-list-block ng-scope'))
        for s in driver.find_elements_by_class_name('md-list-item-block'):
            print s.find_element_by_tag_name('img').get_attribute('src')
            print s.find_element_by_tag_name('img').get_attribute('alt')
            print s.find_elements_by_class_name('image-wrapper').get_attribute('href')
    except:
        import traceback
        print traceback.format_exc()
getGooglerends()

Any suggestion for getting article link from anchor tag?

Upvotes: 1

Views: 406

Answers (1)

falsetru
falsetru

Reputation: 369074

WebDriver.find_elements_by_class_name returns a list of elements, not a single element.

s.find_elements_by_class_name('image-wrapper')
              ^

Use WebDriver.find_element_by_class_name instead of WebDriver.find_elements_by_class_name.

s.find_element_by_class_name('image-wrapper')

Upvotes: 1

Related Questions