chrome_options.binary_location() TypeError: 'str' 对象不可调用 [英] chrome_options.binary_location() TypeError: 'str' object is not callable

查看:199
本文介绍了chrome_options.binary_location() TypeError: 'str' 对象不可调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望大家都平安.我是 python 新手,我很想运行这段代码,但我不明白是什么问题以及如何解决这个问题.

I hope everyone are well. I'm a new in python and itry alot to run this code but i can't understand what is the problem and how i can solve this.

我的代码是:

from selenium import webdriver
from time import sleep

url = raw_input("Enter URL to get visits (With http://): ")
proxy_path = raw_input("Enter path to proxy file:")

with open(proxy_path) as f:
    content = f.readlines()
    f.close()

proxies = 0
with open(proxy_path) as infp:
    for line in infp:
       if line.strip():
        proxies += 1

print 'Loaded %d proxies' %proxies    



# For debugging purposes
#print content[run_through]
run_through = 1
while True:
    #print "Start of loop"
    print "Ran %s times" %run_through
    try:
        use_proxy = content[run_through]
    except IndexError:
        print "Out of proxies"
        break
    print "Using: %s" %use_proxy
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument('--proxy-server=%s' %use_proxy)
    chrome_options.binary_location(value = u"C:\\ProgramFiles\\Google\\Chrome\\chrome.exe")
    browser = webdriver.Chrome(chrome_options=chrome_options)
    #print "Browser started"
   try:
       browser.get(url)
       #print "Opened URL"
       sleep(10)
       browser.find_element_by_id('skip_button').click()
       sleep(10)
       browser.quit()
       #print "Adding one to proxy count"

    except Exception,e:
        print "Skipping proxy. Error occured"
        # For debugging, uncomment line below
        #print str(e)
        browser.quit()
        run_through += 1
        continue

    run_through += 1

    if run_through >= proxies:
        print "No more proxies"
        break

print 'Done!'

我收到此错误:

Traceback (most recent call last):
  File "code.py", line 40, in <module>
    chrome_options.binary_location(value = u"C:\\ProgramFiles\\Google\\Chrome\\c
hrome.exe")
TypeError: 'str' object is not callable

希望我的问题很清楚.

推荐答案

您正在执行 chrome_options.binary_location() 但 binary_location 是一个字符串,而不是一个函数.

You're doing chrome_options.binary_location() but binary_location is a string, not a function.

在查看之后源代码,看来我们这里只有一个getter和一个setter.@l4mpi 似乎是正确的 - 只需执行 chrome_options.binary_location = "C:\\ProgramFiles\\Google\\Chrome\\chrome.exe" 就可以了.

After looking over the source code, it appears that we've just got a getter and a setter here. @l4mpi appears to be correct - simply doing chrome_options.binary_location = "C:\\ProgramFiles\\Google\\Chrome\\chrome.exe" ought to work.

您可以在此处找到 Selenium 文档:http://selenium-python.readthedocs.org/

You can find the Selenium docs here: http://selenium-python.readthedocs.org/

旧(错误)答案:

您在这里可能想要的是 chrome_options.setBinary("C:\\ProgramFiles\\Google\\Chrome\\chrome.exe"),如 在文档中引用.

What you probably wanted here was chrome_options.setBinary("C:\\ProgramFiles\\Google\\Chrome\\chrome.exe"), as referenced in the docs.

这篇关于chrome_options.binary_location() TypeError: 'str' 对象不可调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆