本文介绍了类型错误:“FirefoxWebElement"对象不可迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想通过 Python、selenium、firefox 获取 Airbnb 列表页面的 URL,但是我的程序运行不正常.
I would like to get URLs of Airbnb's listing pages by Python, selenium, firefox, however, my program doesn't work well.
我的错误代码如下;
Original exception was:
Traceback (most recent call last):
File "pages.py", line 19, in <module>
for links in driver.find_element_by_xpath('//div[contains(@id, "listing-")]//a[contains(@href, "rooms")]'):
TypeError: 'FirefoxWebElement' object is not iterable
这是我的代码!
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()
我试图更改我的代码,另一个代码如下;(错误信息与我的第一个代码相同.)
I tried to change my code, another code is as bellow;(Error message is same as my first code.)
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()
如果您有时间,我很高兴为您回复.谢谢.
I am glad to you reply if you have a time.Thank you.
推荐答案
你需要使用 find_elements_by_xpath
其中返回 elements
You need to use find_elements_by_xpath
where return a list of elements
不是 find_element_by_xpath
只返回一个元素
Not find_element_by_xpath
that returned only one element
...
links = driver.find_elements_by_xpath('//div[contains(@id, "listing-")]//a[contains(@href, "rooms")]')
for link in links:
print(link.get_attribute('href')
...
输出
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
...
这篇关于类型错误:“FirefoxWebElement"对象不可迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!