【问题标题】:TypeError: 'str' object is not callable with driver.current_url() (Python 3.6)(Selenium)TypeError: 'str' 对象不能用 driver.current_url() (Python 3.6)(Selenium) 调用
【发布时间】:2019-01-28 15:19:29
【问题描述】:

我的代码:

https://pastebin.com/WKHZwAib

import selenium
from selenium import webdriver as web

url = 'https://www.wta.org/go-outside/hikes/hike_search? sort=&rating=0&mileage:float:list=0.0&mileage:float:list=25.0&title=&region=all&searchabletext=&filter=Search&subregion=all&b_start:int=0&show_incomplete=on&elevationgain:int:list=0&elevationgain:int:list=5000&highpoint='

driver = web.Chrome('D:/Development/Projects/Test/test/chromedriver')

driver.get(url)

driver.current_url()    

我收到 TypeError: 'str' object is not callable on driver.current_url()。关于如何解决这个问题的任何提示?

我将浏览该站点上的页面并获取每个 URL。如果有另一种方法可以避免这种情况,请告诉我。

提前致谢。

【问题讨论】:

    标签: python selenium web-crawler scrape


    【解决方案1】:

    仅使用driver.current_url 而不是driver.current_url()

    来自docs

    有些属性是可调用的(或方法),有些是不可调用的 (特性)。所有可调用属性都以 round 结尾 括号。

    所以,current_url 是一个属性而不是一个方法,因此你不要调用它。

    使用示例(Python 3):

    my_url = driver.current_url
    print(my_url)
    

    【讨论】:

    • 感谢您的帮助!我发誓我试过了,但我一定没有。
    【解决方案2】:

    用不带括号的driver.current_url 试试。

    https://selenium-python.readthedocs.io/api.html#selenium.webdriver.remote.webdriver.WebDriver.current_url

    消息错误告诉你driver.current_url是一个字符串,由于你不能调用字符串对象,driver.current_url()不能被解释。

    【讨论】:

    • 我发誓我试过这个,但我一定没有。感谢您的回答!
    猜你喜欢
    • 2019-08-24
    • 2023-01-15
    • 2022-01-08
    • 1970-01-01
    • 1970-01-01
    • 2020-09-16
    • 1970-01-01
    • 1970-01-01
    • 2013-08-05
    相关资源
    最近更新 更多