chrome_options.binary_location()TypeError:'str'对象不可调用

yuy*_*b0y 3 python python-2.7

我希望每个人都很好.我是一个新的python和itry很多运行此代码但我无法理解是什么问题以及我如何解决这个问题.

我的代码是:

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!'
Run Code Online (Sandbox Code Playgroud)

我得到这个错误:

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
Run Code Online (Sandbox Code Playgroud)

希望我的问题很明确.

Gar*_*les 6

你正在做chrome_options.binary_location()但是binary_location是一个字符串,而不是一个函数.

查看源代码后,我们看到我们刚刚获得了一个getter和一个setter.@ l4mpi似乎是正确的 - 只是chrome_options.binary_location = "C:\\ProgramFiles\\Google\\Chrome\\chrome.exe"应该工作.

你可以在这里找到Selenium文档:http://selenium-python.readthedocs.org/


老(错)回答:

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