Chrome DevTools SetTimeZoneOverrideCommand无法匹配IP时区及Selenium代理相关问题咨询
Hey there! Let's break down your three issues one by one, and fix up your code to get everything working as expected:
1. Keeping the Same Proxy for IP Checks When Using Rotating Proxies
When you're using rotating proxies, the key is to reuse the same proxy instance that your Selenium driver is using for your IP check (instead of making a separate HTTP request that might route through a different proxy). Here's how to adjust your workflow:
- Ditch the standalone
GetCurrentIP()function that makes an independent web request. Instead, extract the IP directly from the Selenium browser instance after navigating to whoer.net—this guarantees you're using the exact same proxy as the browser session. - If you need to rotate proxies but lock one in for a full session, store the selected proxy in a variable before assigning it to ChromeOptions, then reuse that variable for any subsequent checks (like the ip-api.com lookup, if needed).
2. Fixing SetTimezoneOverrideCommand Not Working
Looking at your original code, you created the SetTimezoneOverrideCommand object—but you never actually sent it to Chrome DevTools! That's why Whoer.net still shows your local system time. Here's the fix:
- Initialize a DevTools session linked to your Chrome driver.
- Send both the geolocation and timezone override commands before (or right after) navigating to Whoer.net.
- Your
Get_TimeZone_GeoLocationfunction already fetches valid IANA timezone IDs (likeAmerica/New_York), so that part was spot-on—you just needed to send the command to apply it.
3. Modifying DNS Routing for Whoer.net
Yes, you can override DNS entries for Chrome using DevTools' SetDnsOverrideCommand from the Network domain. This lets you map Whoer.net's domain to a specific IP address, and it only affects the Selenium-controlled Chrome instance (not your entire system).
Revised VB.NET Code with All Fixes
Here's your updated code that addresses every issue:
Imports System Imports System.IO Imports System.Text.RegularExpressions Imports OpenQA.Selenium Imports OpenQA.Selenium.Chrome Imports Zu.ChromeDevTools Imports Zu.ChromeDevTools.Emulation Imports Zu.ChromeDevTools.Network ' For DNS override functionality Module Program Sub Main(args As String()) ' Select and store your proxy (reuse this for any related requests) Dim selectedProxy As New Proxy With { .Kind = ProxyKind.Manual, .IsAutoDetect = False, .HttpProxy = "p.webshare.io:9999", .SslProxy = "p.webshare.io:9999" } ' Configure Chrome options Dim browser_option As New ChromeOptions Dim NewUAC As String = Random_UAC() browser_option.AddArgument("user-agent=" & NewUAC) browser_option.AddArgument("--window-size=800,600") browser_option.AddArgument("--incognito") browser_option.AddArgument("--disable-infobars") browser_option.AddArgument("--disable-blink-features=AutomationControlled") browser_option.Proxy = selectedProxy ' Initialize driver and link DevTools session Dim driverService = ChromeDriverService.CreateDefaultService driverService.HideCommandPromptWindow = True Dim browser As New ChromeDriver(driverService, browser_option) Dim devTools = browser.CreateDevToolsSession() ' Navigate to Whoer.net and grab IP using the same proxy browser.Navigate.GoToUrl("https://whoer.net") System.Threading.Thread.Sleep(3000) ' Wait for page content to load ' Extract IP reliably via element selector (no messy HTML parsing) Dim currentIpElement = browser.FindElement(By.CssSelector(".your-ip")) Dim CurrentIP As String = currentIpElement.Text.Trim() Console.WriteLine("Changed IP: " & CurrentIP) ' Fetch geolocation and timezone data Dim CurrentGeo() As String = Get_TimeZone_GeoLocation(CurrentIP, selectedProxy).Split(",") Console.WriteLine("Current Loc: " & String.Join(", ", CurrentGeo)) ' Apply geolocation override Dim ChromeGeoLocation As New SetGeolocationOverrideCommand ChromeGeoLocation.Latitude = Convert.ToDouble(CurrentGeo(0)) ChromeGeoLocation.Longitude = Convert.ToDouble(CurrentGeo(1)) ChromeGeoLocation.Accuracy = 1 devTools.SendCommand(ChromeGeoLocation) ' Apply timezone override (THIS WAS MISSING IN YOUR ORIGINAL CODE!) Dim ChromeTimeZone As New SetTimezoneOverrideCommand ChromeTimeZone.TimezoneId = CurrentGeo(2) devTools.SendCommand(ChromeTimeZone) ' Optional: Override Whoer.net DNS (map domain to custom IP) Dim dnsOverride As New SetDnsOverrideCommand dnsOverride.Domains = New Dictionary(Of String, String) From { {"whoer.net", "199.59.243.120"} ' Replace with your target IP } devTools.SendCommand(dnsOverride) ' Refresh to see all changes take effect browser.Navigate.Refresh() System.Threading.Thread.Sleep(5000) ' Cleanup browser.Quit() End Sub Function Get_TimeZone_GeoLocation(ByVal CurrentIP As String, ByVal proxy As Proxy) As String Dim DetectAddress As String = "http://ip-api.com/csv/" & CurrentIP Dim objHttpRequest As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create(DetectAddress) ' Use the same proxy as the browser to avoid IP mismatch objHttpRequest.Proxy = New System.Net.WebProxy(proxy.HttpProxy) ' Use Using statements to properly dispose resources Using objHttpResponse As System.Net.HttpWebResponse = objHttpRequest.GetResponse() Using objStrmReader As New StreamReader(objHttpResponse.GetResponseStream) Dim sHTML As String = objStrmReader.ReadToEnd() Dim sHTML_Item() As String = sHTML.Split(",") Dim latitude = sHTML_Item(7) Dim longitude = sHTML_Item(8) Dim timezone = sHTML_Item(9) Return $"{latitude},{longitude},{timezone}" End Using End Using End Function Function Random_UAC() As String Dim strUACs As String = IO.File.ReadAllText("UAC_List.txt") ' Remove empty entries from UAC list Dim strUAC_Split As String() = strUACs.Split(vbCrLf, StringSplitOptions.RemoveEmptyEntries) Dim rnd As New Random() Dim randomIndex As Integer = rnd.Next(strUAC_Split.Length) Return strUAC_Split(randomIndex) End Function End Module
Key Improvements:
- Proxy Consistency: The IP lookup and timezone fetch now use the same proxy as the browser session.
- Working Timezone Override: Added
devTools.SendCommand(ChromeTimeZone)to apply the timezone setting to Chrome. - DNS Override: Included optional code to map Whoer.net to a custom IP via DevTools.
- Reliable IP Extraction: Replaced manual HTML parsing with a CSS selector for more stable IP retrieval.
- Cleaner Resource Management: Added
Usingstatements to properly dispose HTTP connections.
内容的提问来源于stack exchange,提问作者SampoValo

