由于代理配置,selenium-wire 阻塞连接

selenium-wire blocking connection due to proxy config

我正在使用 selenium-wire 和 firefox webdriver 访问网站(在线游戏)。我是 运行 我本地网络上的一个 python 脚本,访问 Internet 不需要代理。

这是我的代码的摘录:

#!C:/Python38-32/python.exe
    
from seleniumwire import webdriver  # Import from seleniumwire
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import ActionChains

# Create a new instance of the Firefox driver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

binary = FirefoxBinary(r'C:\Program Files\Mozilla Firefox\firefox.exe')
driver = webdriver.Firefox(firefox_binary=binary)

# Go to the home page
driver.get('https://fr0.forgeofempires.com/page/')

iframe = driver.find_element_by_tag_name('iframe')
iframe_switched = driver.switch_to.frame(iframe)
    
useridInput = driver.find_element_by_id('login_userid')
useridInput.click();
    
useridInput.send_keys('myuser'); 

login_passwordInput = driver.find_element_by_id('login_password')
login_passwordInput.click();

login_passwordInput.send_keys('mypass'); 

loginButton = driver.find_element_by_id('login_Login')
loginButton.click();

defaultContent_switched = driver.switch_to.default_content()

WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.ID, "play_now_button"))
)
play_nowButton = driver.find_element_by_id('play_now_button')
play_nowButton.click();

WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.LINK_TEXT, "ServerName"))
)
Button = driver.find_element_by_link_text('ServerName')
Button.click();

在此之前一切正常。该页面现在应该以新的 URL (https://xxx.forgeofempires.com/game) 加载游戏,但它卡住了。如果我按 F5,我会得到 代理服务器拒绝连接。我查看了我的 Firefox 代理设置,发现它们从 No proxyUse system proxy settings 更改为 Manual 127.0.0.1 。我猜是 Selenium-wire 改变了它以检查流量?

我的最终目标是捕获页面生成的 XHR 响应,这就是我使用 Selenium-wire 的原因。

连接网站时本地主机代理被阻止的原因是什么?我该如何解决?

如果我替换

from seleniumwire import webdriver  # Import from seleniumwire

来自

from selenium import webdriver  # Import from selenium

它工作正常,但我将无法捕捉到 XHR 响应。我也试过 mitmproxy 但没能成功。

更新 1:

我做了一个完全不工作的例子,它表明 Selenium-wire 做错了什么。在下面的示例中,Google 的结果页面无法加载。

#!C:/Python38-32/python.exe
    
from seleniumwire import webdriver  # Import from seleniumwire
#from selenium import webdriver  # Import from selenium

from selenium.webdriver.common.keys import Keys

# Create a new instance of the Firefox driver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

binary = FirefoxBinary(r'C:\Program Files\Mozilla Firefox\firefox.exe')
driver = webdriver.Firefox(firefox_binary=binary)

driver.maximize_window()

# Go to the home page
driver.get('https://www.google.se/')

useridInput = driver.find_element_by_name('q')
useridInput.click();
    
useridInput.send_keys('test'); 

driver.find_element_by_name("q").send_keys(Keys.ENTER)

我在最后添加了这一行以使其工作:

value = input("SCRIPT ENDED\n")

可以在 here 及以下找到原因:

Selenium Wire works by transparently configuring the browser to point at Selenium Wire's own proxy server. That proxy server (running on 127.0.0.1:49818 in your example above) is used to capture requests made by the browser whilst Selenium Wire is running. After Selenium Wire ends, it shuts down its proxy server because it thinks it's done. However, if the browser is left open (which seems to be the case in your example above), the browser will still be pointing at the proxy server. Trying to use the browser will not work, because the proxy server has now gone away with the shutdown of Selenium Wire.

If you wish to manually interact with the browser, you'll need to keep Selenium Wire running by ensuring that your program does not end. You could potentially use a time.sleep() for that, or perhaps some other mechanism such as input().