【问题标题】:Selenium not .click giving "object not callable" errorSelenium not .click 给出“对象不可调用”错误
【发布时间】:2015-11-27 05:51:18
【问题描述】:

我正在尝试自动化我的租赁搜索。我可以获得结果的第一页,但是当我尝试单击下一个按钮时出现错误 - “对象不可调用”我是 Python 新手,只是认为这将是一个有趣的项目来学习 - 任何帮助。

from selenium import webdriver
from bs4 import BeautifulSoup
import datetime
from datetime import timedelta
import time
import re






pages = set()

def getLinks(url):
global pages
# Open web browser and get url - 3 second time delay.
driver = webdriver.Firefox()
driver.get(url)
time.sleep(3)
pageSource = driver.page_source
bsObj = BeautifulSoup(pageSource)
for addr_link in bsObj.findAll("a", href=re.compile("^/homedetails/*")):
    if 'href' in addr_link.attrs:
        if addr_link['href'] not in pages:
            newPage = addr_link.attrs['href']
            pages.add(newPage)
            print(newPage)

#if bsObj.find('li', {'class': "zsg-pagination-next"}) == True:
next_page = bsObj.find('li', {'class': "zsg-pagination-next"}).find("a")
#next_page.click()
print(next_page)
next_page.click()
getLinks(http://www.zillow.com/homes/for_rent/Jackson-County-MO/house,mobile_type/1804_rid/6m_days/39.198737,-93.6866,38.873394,-95.026932_rect/9_zm/)

【问题讨论】:

标签: python selenium beautifulsoup


【解决方案1】:

要完成上述任务,您不需要beautifulsoup,可以使用webdriver 完成。试试下面的代码。

//Code to Fetch All the Link Details Through WebDriver

addr_link = driver.find_elements_by_xpath("//a[contains(@href,'homedetails')]")
for link in addr_link : 
print link.get_attribute("href")


//Code To Click On Next Button
next_btn = driver.find_elements_by_xpath("//a[text()='Next']")
next_btn.click()

【讨论】:

  • 我仍然收到 No attribute to click 消息 - 我也在使用 BeatifulSoup,因为我需要它来获取租赁列表页面上的租金金额和联系经理电话号码
  • 请分享脚本代码和错误信息详情,我去看看。
  • 不要听起来像个白痴 - 但你的意思是,脚本代码在问题中,除非你的意思是别的。当更改为您的建议时,我得到“列表”对象没有属性“点击”
  • 是的,点击不是属性,它是一种方法。所以像 'link.click()' 一样使用它
  • 好的,但我使用了你的确切代码 next_btn.click() 不正确吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-19
  • 2011-01-27
相关资源
最近更新 更多