0

I have the following code:

API_KEY = 'YOUR_API_KEY'

proxy_options = {
    'proxy': {
        'http': f'http://scraperapi:{API_KEY}@proxy-server.scraperapi.com:8001',
        'https': f'http://scraperapi:{API_KEY}@proxy-server.scraperapi.com:8001',
        'no_proxy': 'localhost,127.0.0.1'
    }
}

driver = webdriver.Chrome(seleniumwire_options=proxy_options)
wait = WebDriverWait(driver, 20)
driver.get(url)

# Rest of the code

How can I print the IP every time driver.get(url) is runned?

LJG
  • 601
  • 1
  • 7
  • 15

1 Answers1

0

A simpler approach would be to wrapup the initialization of ChromeDriver initiated Browsing Context within a for loop as follows:

API_KEY = 'YOUR_API_KEY'

proxy_options = {
    'proxy': {
    'http': f'http://scraperapi:{API_KEY}@proxy-server.scraperapi.com:8001',
    'https': f'http://scraperapi:{API_KEY}@proxy-server.scraperapi.com:8001',
    'no_proxy': 'localhost,127.0.0.1'
    }
}

my_url_list = ['https://www.google.com/', 'https://stackoverflow.com/']

for url in my_url_list:
    try:
        driver = webdriver.Chrome(seleniumwire_options=proxy_options)
        wait = WebDriverWait(driver, 20)
        driver.get(url)
        print(driver.current_url)
    except:
        continue
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • But this prints the current URL, I would need the current IP. – LJG Mar 25 '22 at 12:45
  • Were does the IP address shows up? In the url? – undetected Selenium Mar 25 '22 at 12:46
  • It prints `https://www.google.com/` and `https://stackoverflow.com/`. – LJG Mar 25 '22 at 12:49
  • Yes, we are printing the `current_url`, hence `https://www.google.com/` and `https://stackoverflow.com/` is printed. Where does the **IP address** gets reflected which you want to print? – undetected Selenium Mar 25 '22 at 12:56
  • I see that the Google page is open in a different language, so I guess the request is made via a different IP. But I didn't understand how to see it. – LJG Mar 25 '22 at 14:46
  • @LJG If you can update me where _`IP address`_ information is getting reflected, I can suggest you the way to extract the required info. – undetected Selenium Mar 25 '22 at 14:58
  • I can't understand what you mean, sorry. I thought there was a method to print the IP address used by the proxy in the Python console. – LJG Mar 25 '22 at 15:08
  • I solved using `'https://api.ipify.org/?format=json'` twice as URL. – LJG Mar 25 '22 at 15:26