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()
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. Show activity on this post. Below code worked for me.
What is a Selenium WebElement? A WebElement, in this case, a Selenium WebElement is essentially an HTML element on a website. HTML documents consist of HTML elements. Each HTML element consists of a start tag and an end tag.
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
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With