具有预期条件的硒:' str'对象不可调用

时间:2017-03-05 17:12:58

标签: python selenium phantomjs

我正在

Traceback (most recent call last):
  File "/Users/xyz/Documents/Projects/apartmentsearch/stanga.py", line 46, in <module>
    wait.until(EC.visibility_of_element_located(By.CLASS_NAME('sk-fading-circle spinner-container-3')))
TypeError: 'str' object is not callable

运行此代码时

from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

PHANTOMJS_PATH = './pjs/bin/phantomjs'

browser = webdriver.PhantomJS(PHANTOMJS_PATH)
browser.get('REDACTED')

wait = WebDriverWait(browser, 10)
wait.until(EC.visibility_of_element_located(By.CLASS_NAME('sk-fading-circle spinner-container-3')))
wait.until(EC.invisibility_of_element_located(By.CLASS_NAME('sk-fading-circle spinner-container-3')))

soup = BeautifulSoup(browser.page_source, "html.parser")

print(soup.prettify())

运行python 2.7。编辑过的网站网址,但它可以正常工作,我可以在没有等待的情况下加载页面。

在线代码片段似乎表明这样的代码应该是可运行的。我错过了什么?我会用断点来弄明白,但PyCharm由于某种原因不能在我的断点处停下来,这也是我想知道的......

1 个答案:

答案 0 :(得分:2)

您指定的定位器错误。它必须是(By, selector_string)

的元组(或数组)
wait = WebDriverWait(browser, 10)
wait.until(EC.visibility_of_element_located((By.CLASS_NAME, 'sk-fading-circle 
spinner-container-3')))
wait.until(EC.invisibility_of_element_located((By.CLASS_NAME, 'sk-fading-circle 
spinner-container-3')))

此外,您似乎正在尝试为CLASS_NAME使用多个课程。我认为这不会起作用。您应该尝试使用(By.CSS_SELECTOR, '.sk-fading-circle.spinner-container-3')