python - 类型错误 : 'FirefoxWebElement' object is not iterable

标签 python python-3.x selenium xpath web-scraping

<分区>

我想通过 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 返回 elements 的列表

不是只返回一个元素的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
...

关于python - 类型错误 : 'FirefoxWebElement' object is not iterable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50895680/

相关文章:

Python + GStreamer - 无法连接

python - django-paypal recurring_payment_id 为空

python-3.x - Popen 的上下文管理器

javascript - 我如何使用 selenium 和 java 捕获浏览器控制台日志

python - 试图遍历网页以抓取所有足球运动员的名字,但只得到第一个?

python - 如何在elasticsearch-dsl python中选择特定字段

python - 使用 if 语句验证字典成员资格

python - PySerial 非阻塞读取循环

python - 如何在python中使用re从PT格式日期时间中提取分钟和秒

Python,py.test 'HTMLReport' 对象没有属性 'execute'