“'WebElement' object is not iterable”

问题代码:

# -*- coding: UTF-8 -*-
from selenium import webdriver


def main():
    url = 'http://example.webscraping.com/places/default/search'
    driver = webdriver.Chrome()
    driver.get(url)
    driver.find_element_by_id('search_term').send_keys('.')
    js = "document.getElementById('page_size').options[1].text='260'"
    driver.execute_script(js)
    driver.find_element_by_id('search').click()
    driver.implicitly_wait(30)                      # 设置超时
    links = driver.find_element_by_css_selector('#results a')
    countries = [link.text for link in links]       # 报错
    print countries
    driver.close()

if __name__ == '__main__':
    main()

报错:

'WebElement' object is not iterable

解决:

links = driver.find_element_by_css_selector('#results a')
改为:
links = driver.find_elements_by_css_selector('#results a')

你可能感兴趣的:(答疑解惑)