26

I am trying to extract all the links from wikipedia homepage but this code showing TypeError: 'WebElement' object is not iterable error.

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

browser=webdriver.Chrome()
browser.get('https://en.wikipedia.org/wiki/Main_Page')
search=[]
search=browser.find_element_by_xpath('//*[@href]')


for ii in search:
  print(ii.get_attribute('href'))

time.sleep(4)
browser.close()  

2 Answers 2

103

The problem is that you are using find_element_by_xpath which return only one WebElement (which is not iterable), the find_elements_by_xpath return a list of WebElements.

Solution: replace find_element_by_xpath with find_elements_by_xpath

Reference: selenium-python docs

Sign up to request clarification or add additional context in comments.

2 Comments

The same is true for find_element vs find_elements. Thanks!
This method is deprecated and should be replaced by find_element('xpath', ...) and find_elements('xpath', ...) respectively. The link is correct.
6

Below code worked for me.

from selenium import webdriver
driver=webdriver.Firefox()
driver.get("https://www.google.co.in/")
list_links=driver.find_elements_by_tag_name('a')

for i in list_links:
    print i.get_attribute('href')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.