Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在具有firefox配置文件的Web服务器上使用selenium_Python_Selenium_Firefox_Server_Pyvirtualdisplay - Fatal编程技术网

Python 在具有firefox配置文件的Web服务器上使用selenium

Python 在具有firefox配置文件的Web服务器上使用selenium,python,selenium,firefox,server,pyvirtualdisplay,Python,Selenium,Firefox,Server,Pyvirtualdisplay,我正在尝试编写一个脚本,将我的数据从一个站点同步到另一个站点。第一个站点没有公共api。但我知道这些查询,它们可以为我提供json响应以及我需要的所有数据。我决定使用硒。主要的问题是,我必须获得授权才能获取这些数据,但使用selenium进行授权太难了,因为该站点使用recaptcha2。我还想在我的服务器上使用它。所以我也使用了pyvirtualdisplay 我在firefox中创建了一个新的配置文件,然后我在第一个站点上获得了该配置文件的授权,并在脚本中使用了它。像这样的 从pyvirtu

我正在尝试编写一个脚本,将我的数据从一个站点同步到另一个站点。第一个站点没有公共api。但我知道这些查询,它们可以为我提供json响应以及我需要的所有数据。我决定使用。主要的问题是,我必须获得授权才能获取这些数据,但使用selenium进行授权太难了,因为该站点使用recaptcha2。我还想在我的服务器上使用它。所以我也使用了pyvirtualdisplay

我在firefox中创建了一个新的配置文件,然后我在第一个站点上获得了该配置文件的授权,并在脚本中使用了它。像这样的

从pyvirtualdisplay导入显示
从selenium导入webdriver
显示=显示(可见=0,大小=(800600))
display.start()
profile=webdriver.FirefoxProfile('/home/admin/.cache/mozilla/firefox/o0eaxyux.user'))
browser=webdriver.Firefox(配置文件,可执行文件路径=r'./geckodriver')
browser.get(“https://example.com/p/api/v5/profile/blabla")
response=json.load(browser.find_element_by_tag_name('body').text)
打印(答复)
browser.quit()
display.stop()
它在我的电脑上运行得很好。 在服务器上,如果我不使用配置文件,pyvirtualdisplay也可以工作。 但如果在服务器上使用配置文件,则会出现以下错误:

Traceback (most recent call last):
  File "1.py", line 7, in <module>
    browser = webdriver.Firefox(profile, executable_path=r'./geckodriver')
  File "/home/admin/.local/lib/python3.6/site-packages/selenium/webdriver/firefox/webdriver.py", line 174, in __init__
    keep_alive=True)
  File "/home/admin/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "/home/admin/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/home/admin/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/home/admin/.local/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: connection refused

有什么想法吗?

问题是我从另一台电脑复制了firefox配置文件。我不知道如何修复它。但我找到了另一个解决方案:使用cookies而不是配置文件:

  • 创建文件
  • 用法

从这里开始:

geckodriver与服务器上的脚本不在同一路径中(可执行文件路径=r'./geckodriver')不,它在同一路径中,我把它放在那里。正如我上面所说的,若我不使用profile,脚本就可以工作。如下所示:browser=webdriver.Firefox(可执行文件\u path=r'./geckodriver')
1575543823086   mozrunner::runner   INFO    Running command: "/usr/bin/firefox" "-marionette" "-foreground" "-no-remote" "-profile" "/tmp/rust_mozprofilegsEa0V"
import pickle
from pyvirtualdisplay import Display
from selenium import webdriver

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

profile = webdriver.FirefoxProfile('/home/admin/.cache/mozilla/firefox/o0eaxyux.user')
browser = webdriver.Firefox(profile, executable_path=r'./geckodriver')
browser.get("https://example.com/p/api/v5/profile/blabla")
pickle.dump(browser.get_cookies() , open("cookies.pkl","wb"))

browser.quit()
display.stop()
import pickle
from pyvirtualdisplay import Display
from selenium import webdriver

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

browser = webdriver.Firefox(profile, executable_path=r'./geckodriver')
browser.get("https://example.com/p/api/v5/profile/blabla")
cookies = pickle.load(open("cookies.pkl", "rb"))
for cookie in cookies:
    browser.add_cookie(cookie)
browser.get("https://example.com/p/api/v5/profile/blabla")
response = json.loads(browser.find_element_by_tag_name('body').text)
print(response)

browser.quit()
display.stop()