如何将chrome选项转换为selenium请求



我正试图向jenkins服务器发出POST请求,我需要无头登录。。。然而,我似乎不知道如何将chrome选项传递到selenium请求中。。。

首先,我运行了这个简单的请求来测试选项和我的chrome设置,它运行得很好。。。

# pip3 install selenium
from bs4 import BeautifulSoup 
import pandas as pd
import time
from selenium import webdriver
link = "https://www.google.com"
PROXY = "proxy.com:80" # IP:PORT or HOST:PORT
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server=%s' % PROXY)
chrome_options.add_argument('--headless')
chrome_options.add_argument('ignore-certificate-errors')
chrome = webdriver.Chrome(options=chrome_options)
chrome.get(link)

time.sleep(3)
page = chrome.page_source
chrome.quit()
soup = BeautifulSoup(page, 'html.parser')
container = soup.find_all('div')
print(container)

我有完美的输出,但这一次,我试图以无头的方式发送一个带有身份验证的帖子,错误指向chrome崩溃。。。

我正在处理的新代码是:

# pip install selenium
# pip3 install beautifulsoup4
# pip3 install wheel
# pip3 install pandas
# pip3 install selenium-requests
import os
from bs4 import BeautifulSoup 
import pandas as pd
import time
from selenium import webdriver
from seleniumrequests import Chrome
juser = os.environ['jen_user']
jtoken = os.environ['jen_token']
jhost = os.environ['jen_host']
jproxi = os.environ['jen_proxi']
webdriver = Chrome()
chrome_options = webdriver.ChromeOptions()
#chrome_options.add_argument('--proxy-server=%s' % jproxi)
chrome_options.add_argument('--headless')
chrome_options.add_argument('--ignore-certificate-errors')
#chrome_options.add_argument('--user-agent=%s' % user_agent)
#chrome_options.add_argument('--disable-gpu')
chrome = webdriver.Chrome(options=chrome_options)
chrome.request('POST', jhost, data={juser:jtoken})
# Dump test data
time.sleep(3)
page = chrome.page_source
chrome.quit()
soup = BeautifulSoup(page, 'html.parser')
container = soup.find_all('div')
print(container)

我收到的错误是:

Traceback (most recent call last):
File "test.py", line 19, in <module>
webdriver = Chrome()
File "/home/adminx/.local/lib/python3.8/site-packages/seleniumrequests/request.py", line 144, in __init__
super(RequestsSessionMixin, self).__init__(*args, **kwargs)
File "/home/adminx/.local/lib/python3.8/site-packages/selenium/webdriver/chrome/webdriver.py", line 70, in __init__
super(WebDriver, self).__init__(DesiredCapabilities.CHROME['browserName'], "goog",
File "/home/adminx/.local/lib/python3.8/site-packages/selenium/webdriver/chromium/webdriver.py", line 93, in __init__
RemoteWebDriver.__init__(
File "/home/adminx/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 269, in __init__
self.start_session(capabilities, browser_profile)
File "/home/adminx/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 360, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/home/adminx/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 425, in execute
self.error_handler.check_response(response)
File "/home/adminx/.local/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 247, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: crashed.
(unknown error: DevToolsActivePort file doesn't exist)
(The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
Stacktrace:
#0 0x55d454bd7a23 <unknown>
#1 0x55d4546a2e18 <unknown>
#2 0x55d4546c61f1 <unknown>
#3 0x55d4546c191a <unknown>
#4 0x55d4546fc74a <unknown>
#5 0x55d4546f6883 <unknown>
#6 0x55d4546cc3fa <unknown>
#7 0x55d4546cd4c5 <unknown>
#8 0x55d454c0716d <unknown>
#9 0x55d454c1d5bb <unknown>
#10 0x55d454c08e75 <unknown>
#11 0x55d454c1de85 <unknown>
#12 0x55d454bfc86f <unknown>
#13 0x55d454c38ae8 <unknown>
#14 0x55d454c38c68 <unknown>
#15 0x55d454c53aad <unknown>
#16 0x7fea13be7609 <unknown>

ENV:

我正在ubuntu 20(WSL(上用python测试这个脚本。我在一个没有特殊配置的普通jenkins服务器上运行这个脚本。(这是dockerhub的标准docker图像。(然而,我似乎有一个问题,铬选项没有被硒请求选中。。。以前有人遇到过这种情况吗?

提前感谢!

E

因此,答案是必须使用会话。也许这将节省人们以后的时间。

with requests.Session() as s:
site_session = s.get(jhost, verify=False)
save_cookies(site_session.cookies, "cookies.pkl")
page = browser.request('POST', jhost, auth=auth, verify=False, cookies=load_cookies("cookies.pkl"))
print(page.status_code)

最新更新