TypeError: el objeto ‘FirefoxWebElement’ no es iterable

Me gustaría obtener las URL de las páginas de listado de Airbnb de Python, selenium, firefox, sin embargo, mi progtwig no funciona bien.

Mi código de error es el siguiente;

Original exception was: Traceback (most recent call last): File "pages.py", line 19, in  for links in driver.find_element_by_xpath('//div[contains(@id, "listing-")]//a[contains(@href, "rooms")]'): TypeError: 'FirefoxWebElement' object is not iterable 

¡Aquí está mi código!

 from selenium import webdriver from selenium.webdriver import FirefoxOptions from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import TimeoutException test_url = 'https://www.airbnb.jp/s/%E6%97%A5%E6%9C%AC%E6%B2%96%E7%B8%84%E7%9C%8C/homes?refinement_paths%5B%5D=%2Fhomes&query=%E6%97%A5%E6%9C%AC%E6%B2%96%E7%B8%84%E7%9C%8C&price_min=15000&allow_override%5B%5D=&checkin=2018-07-07&checkout=2018-07-08&place_id=ChIJ51ur7mJw9TQR79H9hnJhuzU&s_tag=z4scstF7' opts = FirefoxOptions() opts.add_argument("--headless") driver = webdriver.Firefox(firefox_options=opts) driver.get(test_url) driver.implicitly_wait(30) for links in driver.find_element_by_xpath('//div[contains(@id, "listing-")]//a[contains(@href, "rooms")]'): listing_url = links.get_attribute('href') print(listing_url) driver.quit() 

Intenté cambiar mi código, otro código es el siguiente; (El mensaje de error es el mismo que mi primer código).

 from selenium import webdriver from selenium.webdriver import FirefoxOptions from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import TimeoutException test_url = 'https://www.airbnb.jp/s/%E6%97%A5%E6%9C%AC%E6%B2%96%E7%B8%84%E7%9C%8C/homes?refinement_paths%5B%5D=%2Fhomes&query=%E6%97%A5%E6%9C%AC%E6%B2%96%E7%B8%84%E7%9C%8C&price_min=15000&allow_override%5B%5D=&checkin=2018-07-07&checkout=2018-07-08&place_id=ChIJ51ur7mJw9TQR79H9hnJhuzU&s_tag=z4scstF7' opts = FirefoxOptions() opts.add_argument("--headless") driver = webdriver.Firefox(firefox_options=opts) driver.get(test_url) driver.implicitly_wait(30) links = driver.find_element_by_xpath('//a[contains(@href, "rooms")]') for link in links: listing_url = link.get_attribute('href') print(listing_url) driver.quit() 

Me alegra que respondas si tienes tiempo. Gracias.

find_elements_by_xpath usar find_elements_by_xpath donde devolver una lista de elements

No find_element_by_xpath que devolvió solo un elemento

 ... links = driver.find_elements_by_xpath('//div[contains(@id, "listing-")]//a[contains(@href, "rooms")]') for link in links: print(link.get_attribute('href') ... 

Salida

 https://www.airbnb.jp/rooms/7793811?location=%E6%97%A5%E6%9C%AC%E6%B2%96%E7%B8%84%E7%9C%8C&check_in=2018-07-07&check_out=2018-07-08 https://www.airbnb.jp/rooms/7793811?location=%E6%97%A5%E6%9C%AC%E6%B2%96%E7%B8%84%E7%9C%8C&check_in=2018-07-07&check_out=2018-07-08 ...