我正在使用 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,我会得到The proxy server is refusing connections。我去了我的 Firefox 代理设置,发现它们从No proxy或Use system proxy settings更改为Manual 127.0.0.1。我猜它是硒线改变它以检查流量?
我的最终目标是捕捉页面生成的 XHR 响应,这就是我使用 Selenium-wire 的原因。
连接到网站时,什么可能导致 localhost 代理被阻止?我该如何解决?
如果我更换
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)