【问题标题】:selenium firefox Unable to locate windowselenium firefox 无法定位窗口
【发布时间】:2017-12-10 12:04:59
【问题描述】:

我正在使用 ssh 连接到服务器,但我无法使用 selenium3.4 和 firefox56 定位窗口。 找不到解决方案,注意到它主要是一个带有硒的 IE 错误 代码: 我

mport bs4 as bs
from bs4 import BeautifulSoup
import urllib.request
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
import time
import re
from random import randint
import pandas as pd
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from pyvirtualdisplay import Display


def get_soup(url):
    sauce = urllib.request.urlopen(url)
    return BeautifulSoup(sauce, 'lxml')


def get_driver_soup(url):
    # driver = webdriver.Firefox(executable_path='/usr/bin/geckodriver')
    display = Display(visible=0, size=(800, 600))
    display.start()
    driver = webdriver.Firefox('/var/gecodriver19-64')
    driver.get(url)
    try:
        element = WebDriverWait(driver, 20).until(
            EC.presence_of_element_located((By.CLASS_NAME, "product-image-wrapper"))
        )
    finally:
        soup = BeautifulSoup(driver.page_source, 'lxml')
        time.sleep(randint(30, 70))
        driver.quit()
        return soup

完整的回溯::

Traceback (most recent call last):
  File "jomashop.py", line 86, in <module>
    soup = get_driver_soup(companies_list[x] + page_suffix)
  File "jomashop.py", line 32, in get_driver_soup
    soup = BeautifulSoup(driver.page_source, 'lxml')
  File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webdriver.py", line 587, in page_source
    return self.execute(Command.GET_PAGE_SOURCE)['value']
  File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webdriver.py", line 311, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/errorhandler.py", line 237, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchWindowException: Message: Unable to locate window

【问题讨论】:

  • 你能发布完整的回溯吗..
  • 它在哪里?
  • 是的,所以我们可以看到发生错误的行..
  • 通常需要很长时间才能加载页面。或者,如果您拨打很多电话,有时您的 IP 地址可能会被禁止
  • 添加了回溯。我认为这不是问题,因为我之前曾与该网站合作过

标签: python-3.x selenium firefox ssh geckodriver


【解决方案1】:

当您尝试使用 Selenium Driver 返回源 HTML 时,可能会超时。

而不是这个:

finally:
    soup = BeautifulSoup(driver.page_source, 'lxml')

尝试改用请求:

import requests

finally:
    r = requests.get(url) 
    html_bytes = r.text 
    soup = BeautifulSoup(html_bytes, 'lxml') 

这应该只拉 html

【讨论】:

  • 返回:selenium.common.exceptions.WebDriverException:消息:连接被拒绝
  • 这意味着服务器正在阻止您的 IP,猜测.. 我发布的代码不使用 Selenium,因此错误很可能来自 ......WebDriverWait(driver, 20 ).....行..
  • 看看这里..也许这是相关的.. ...*.com/questions/39547598/…
  • WebDriverWait 是正确的,在本地工作。停止在 ssh 上工作
  • 我认为这将是远程服务器.. 有时他们可以禁止您 30 分钟.. 有时永久.. 您真的需要在完全不同的网站上尝试脚本以确保它不是服务器
【解决方案2】:

检查您的 geckodriver.log 文件(应该与 python 文件在同一目录中)

如果它说

Error: GDK_BACKEND does not match available displays 

然后安装pyvirtualdisplay:

 pip install pyvirtualdisplay selenium

你可能也需要 xvfb:

 sudo apt-get install xvfb

然后尝试添加此代码:

from pyvirtualdisplay import Display
display = Display(visible=0, size=(800, 600))
display.start() 

完整示例:

from pyvirtualdisplay import Display
from selenium import webdriver

display = Display(visible=0, size=(800, 600))
display.start()

driver = webdriver.Firefox()
driver.get('http://www.python.org')

driver.close()

【讨论】:

    【解决方案3】:

    错误说明了一切:

    NoSuchWindowException: Message: Unable to locate window
    

    换行:

    driver = webdriver.Firefox('/var/gecodriver19-64')
    

    收件人:

    driver = webdriver.Firefox(executable_path='/var/gecodriver19-64/geckodriver')
    

    【讨论】: