Selenium在生产时出现BadStatusLine错误。顺利进行调试

时间:2016-06-22 14:35:26

标签: python selenium selenium-firefoxdriver

有一个脚本可以通过关键字从Instagram清除评论:

# coding: utf-8
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from pyvirtualdisplay import Display


def clear_comments(username, password, words):
    display = Display(visible=0, size=(1024, 768))
    display.start()
    driver = webdriver.Firefox()
    driver.get("http://instagram.com")
    element = WebDriverWait(driver, 3).until(
            EC.presence_of_element_located((By.XPATH, """//*[@id="react-root"]/section/main/article/div[2]/div[2]/p/a"""))
        )
    element.click()
    u = WebDriverWait(driver, 3).until(
            EC.presence_of_element_located((By.NAME, "username"))
        )
    p = driver.find_element_by_name("password")

    u.send_keys(username)
    p.send_keys(password)
    p.send_keys(Keys.RETURN)

    #go to photos list
    element = WebDriverWait(driver, 15).until(
        EC.presence_of_element_located((By.XPATH, """//*[@id="react-root"]/section/nav/div/div/div/div[2]/div[3]/a"""))
    )
    element.click()

    # click on each photo
    WebDriverWait(driver, 15).until(
        EC.presence_of_element_located((By.XPATH, """//*[@id="react-root"]/section/main/article/div/div[1]/div[1]/a[1]"""))
    )
    photos = driver.find_elements_by_css_selector('._myci9 > a')

    for photo in photos:
        photo.click()
        comments = WebDriverWait(driver, 15).until(
            EC.presence_of_element_located((By.XPATH, """/html/body/div[2]/div/div[2]/div/article/div[2]/ul"""))
        )

        comments_list = comments.find_elements_by_css_selector('li')
        for comment in comments_list[1:]:
            for word in words:
                if word in comment.text:
                    comment.find_element_by_css_selector('button').click()
                    driver.find_element_by_xpath("""/html/body/div[3]/div/div[2]/div/div/ul[1]/li/button""").click()
        driver.find_element_by_class_name("_3eajp").click()

    driver.close()
    display.stop()

它在我的开发机器上运行正常但部署版本返回下一个回溯:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/var/www/instaclear/project/instaclear/bot.py", line 30, in clear_comments
    EC.presence_of_element_located((By.XPATH, """//*[@id="react-root"]/section/nav/div/div/div/div[2]/div[3]/a"""))
  File "/var/www/instaclear/env/local/lib/python2.7/site-packages/selenium/webdriver/support/wait.py", line 63, in until
    value = method(self._driver)
  File "/var/www/instaclear/env/local/lib/python2.7/site-packages/selenium/webdriver/support/expected_conditions.py", line 58, in __call__
    return _find_element(driver, self.locator)
  File "/var/www/instaclear/env/local/lib/python2.7/site-packages/selenium/webdriver/support/expected_conditions.py", line 271, in _find_element
    return driver.find_element(*by)
  File "/var/www/instaclear/env/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 681, in find_element
    {'using': by, 'value': value})['value']
  File "/var/www/instaclear/env/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 162, in execute
    response = self.command_executor.execute(driver_command, params)
  File "/var/www/instaclear/env/local/lib/python2.7/site-packages/selenium/webdriver/remote/remote_connection.py", line 349, in execute
    return self._request(url, method=command_info[0], data=data)
  File "/var/www/instaclear/env/local/lib/python2.7/site-packages/selenium/webdriver/remote/remote_connection.py", line 377, in _request
    resp = self._conn.getresponse()
  File "/usr/lib/python2.7/httplib.py", line 1051, in getresponse
    response.begin()
  File "/usr/lib/python2.7/httplib.py", line 415, in begin
    version, status, reason = self._read_status()
  File "/usr/lib/python2.7/httplib.py", line 379, in _read_status
    raise BadStatusLine(line)
BadStatusLine: ''

Ubuntu 14.04.4(服务器) Firefox 29.0.1 硒2.39.0

如何解决?

1 个答案:

答案 0 :(得分:1)

这可能是因为您的预期情况

EC.presence_of_element_located

返回多个元素。

尝试缩小搜索范围,以便条件只返回一个元素。我得到了同样的错误,这对我有用。 没有太多关于EC的文档,但此链接可能对您有所帮助

https://seleniumhq.github.io/selenium/docs/api/py/webdriver_support/selenium.webdriver.support.expected_conditions.html