微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

python – TypeError:’FirefoxWebElement’对象不可迭代

参见英文答案 > TypeError: ‘WebElement’ object is not iterable error                                    2个
我想通过Python,selenium,firefox获取Airbnb的列表页面的URL,但是,我的程序运行不正常.

我的错误代码如下;

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()

我试图改变我的代码,另一个代码如下;
(错误信息与我的第一个代码相同.)

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()

如果你有时间,我很高兴你回复.
谢谢.

解决方法:

您需要使用find_elements_by_xpath返回元素列表

不是只返回一个元素的find_element_by_xpath

...
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
...

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐