Python 类型错误:';FirefoxWebElement';对象是不可编辑的
我想通过Python、selenium和firefox获取Airbnb列表页面的URL,但是,我的程序运行不好 我的错误代码如下Python 类型错误:';FirefoxWebElement';对象是不可编辑的,python,python-3.x,selenium,xpath,web-scraping,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-")]/
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
我试图改变我的代码,另一个代码如下;
(错误消息与我的第一个代码相同。)
如果你有时间,我很高兴给你回信。
谢谢。您需要使用
find\u elements\u by\u xpath
,其中返回元素列表
不通过只返回一个元素的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
...
您需要使用
find\u elements\u by\u xpath
,其中返回元素的列表
不通过只返回一个元素的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
...