I am trying to test how to use proxies using selenium in python
Here's the code I am trying
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
PROXY = "164.68.123.119:9300"
opts = Options()
#opts.add_argument('--proxy-server=%s' % PROXY)
driver = webdriver.Chrome(executable_path="D:/Webdrivers/chromedriver.exe", options=opts)
print(type(driver))
driver.get("https://www.google.com")
myPageTitle=driver.title
print(myPageTitle)
assert "Google" in myPageTitle
time.sleep(10)
print(driver.quit)
driver.quit()
When running the code without relying on the proxy, I got error logs like that
DevTools listening on ws://127.0.0.1:1056/devtools/browser/3bd3f85f-6d2c-4273-87be-a2e108c07626
<class 'selenium.webdriver.chrome.webdriver.WebDriver'>
Google
[10116:7976:1029/180608.508:ERROR:chrome_browser_main_extra_parts_metrics.cc(230)] crbug.com/1216328: Checking Bluetooth availability started. Please report if there is no report that this ends.
[10116:7976:1029/180608.509:ERROR:chrome_browser_main_extra_parts_metrics.cc(233)] crbug.com/1216328: Checking Bluetooth availability ended.
[10116:10692:1029/180608.527:ERROR:device_event_log_impl.cc(214)] [18:06:08.528] Bluetooth: bluetooth_adapter_winrt.cc:1073 Getting Default Adapter failed.
[10116:7976:1029/180608.527:ERROR:chrome_browser_main_extra_parts_metrics.cc(236)] crbug.com/1216328: Checking default browser status started. Please report if there is no report that this ends.
[10116:7976:1029/180608.561:ERROR:chrome_browser_main_extra_parts_metrics.cc(240)] crbug.com/1216328: Checking default browser status ended.
[10116:10692:1029/180608.589:ERROR:usb_descriptors.cc(114)] Failed to parse configuration descriptor.
[10116:10692:1029/180608.725:ERROR:usb_descriptors.cc(100)] Failed to read all configuration descriptors. Expected 1, got 0.
[10116:10692:1029/180608.725:ERROR:device_event_log_impl.cc(214)] [18:06:08.726] USB: usb_device_win.cc:93 Failed to read descriptors from \\?\usb#vid_0000&pid_3825#5&50adea6&0&1#{a5dcbf10-6530-11d2-901f-00c04fb951ed}.
<bound method WebDriver.quit of <selenium.webdriver.chrome.webdriver.WebDriver (session="9dde593ef8cb9492d05b49ea42fc0d9f")>>
Any idea how to fix such errors?
To initiate Chrome browser using a proxy you can try the following solution:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
PROXY = "164.68.123.119:9300"
options = Options()
options.add_argument('--proxy-server={}'.format(PROXY))
options.add_argument("start-maximized")
driver = webdriver.Chrome(executable_path=r'C:\WebDriver\ChromeDriver\chromedriver.exe', options=options)
print(type(driver))
driver.get("https://www.google.com")
myPageTitle=driver.title
print(myPageTitle)
assert "Google" in myPageTitle
time.sleep(10)
driver.quit()
PS: chrome_browser_main_extra_parts_metrics.cc, device_event_log_impl.cc, usb_descriptors.cc, etc are the result of a generic bug due to Chrome/ChromeDriver compatibility which you can ignore as of now. For details check Parametrized tests are flaky due to a timeout in recording expensive metrics on startup.
Reference
You can find a relevant discussions in:
How to rotate Selenium webrowser IP address
Related
I have this script that was able to connect to a browser that is run by this command:
"c:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe" --remote-debugging-port=9222. For some reason, it stopped working.
from selenium import webdriver
from selenium.webdriver.edge.options import Options
edge_options = Options()
edge_options.add_experimental_option('debuggerAddress', '127.0.0.1:9222')
driver = webdriver.Edge(options=edge_options)
driver.get(youtube.com)
print(driver.title)
Part of the error message is this:
Message: unknown error: cannot connect to microsoft edge at 127.0.0.1:9222
from chrome not reachable
Two questions about this message:
Could I just connect to the IP address of the "real" browser?
Why is there written chrome instead of chromium?
This is a full error message it makes me swallow.
https://i.stack.imgur.com/mGL4d.png
I am trying to open a website on chrome via selenium with the following code:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
s=Service('C:/Users/Morteza/Documents/Dev/chromedriver.exe')
browser = webdriver.Chrome(service=s)
url='https://www.google.com'
browser.get(url)
link to problem: https://share.cleanshot.com/p1qu5y
This is not an issue or crash. After the specified actions are completed successfully, selenium closes the web browser. This program works fine.
Use the following code with a while True block
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
This isn't any crash or an error as such. Selenium automatically closes the client i.e. the Chrome Browser instance after executing the last line of your code. However this practice may accumulate undeleted/zombie chromedriver instances within your system.
Ideally, you always need to invoke driver.quit() within tearDown(){} method to close & destroy the WebDriver and Web Client instances gracefully at the end of your tests.
the chromedrive path in your code, i guess your platform is windows.
but the picture you post revease that your machine is mac.
pay attention to the chromedriver file PATH and your PC platform, make sure they suit to each other.
or you could put chromedriver in system PATH variable, so you do not need to specify the chromedrive path in your code
#TLDR I want to use brave browser with selenium written in python but can't find any current solutions that work.
This code works
from selenium import webdriver
option = webdriver.ChromeOptions()
option.binary_location = r'C:\Program Files\BraveSoftware\Brave-
Browser\Application\brave.exe'
driver = webdriver.Chrome(executable_path=r'C:\WebDrivers\chromedriver.exe',
options=option)
driver.get("https://www.google.com")
driver.quit()
but executable_path is deprecated:
C:\Users\USER\PycharmProjects\pythonProject\sol2.py:5:
DeprecationWarning: executable_path has been deprecated, please pass in a Service object
driver = webdriver.Chrome(executable_path=r'C:\WebDrivers\chromedriver.exe', options=option)
Found this on youtube: https://www.youtube.com/watch?v=VMzmVFA-Gps
# import statements
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
# Declare variables and setup services
driverService = Service('C:/webdrivers/chromedriver.exe')
# 1. Passes the chromedriver path to the service object
# 2. stores the service object in the s variable
driver = webdriver.Chrome(service=driverService)
# 1. Passes service object driverSerice into the webdriver.Chrome
# 2. Stores object in driver variable
# Body (actually doing stuff)
driver.maximize_window() # maximizes the browser window
driver.get("https://www.google.com") # navigates to google.com
myPageTitle = driver.title
# gets the title of the web page stores in myPageTitle
print(myPageTitle) # prints myPageTitle to Console
assert "Google" in myPageTitle
# checks myPageTitle to ensure it contains Google
# clean up
driver.quit() # closes the browser
When I run this code I get:
selenium.common.exceptions.WebDriverException: Message: unknown error: cannot find Chrome binary
This code works as long as you allow Google Chrome onto your PC. I don't want Chrome on my PC.
The problem is that I can't figure out how to get selenium to use brave instead of Chrome.
As of this writing I am using the following:
Windows 11 Home
Selenium v4.0.0
Python v3.10
ChromeDriver 95.0.4638.69
Brave Browser Version 1.31.91 Chromium: 95.0.4638.69 (Official Build) (64-bit)
Can some one please explain how to make this work with the current (read nondeprecated) code on brave browser? Thanks for your time.
To initiate a brave browsing context you need to:
Use the binary_location attribute to point to the brave binary location.
Use the chromedriver executable to initiate the brave browser.
Code block:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
option = webdriver.ChromeOptions()
option.binary_location = r'C:\Program Files (x86)\BraveSoftware\Brave-Browser\Application\brave.exe'
driverService = Service('C:/Users/.../chromedriver.exe')
driver = webdriver.Chrome(service=driverService, options=option)
driver.get("https://www.google.com")
Note: The DeprecationWarning: executable_path has been deprecated is a harmless warning message which doesn't affects your test execution and you can still ignore it.
References
You can find a couple of relevant detailed discussions in:
How to use Brave web browser with python, selenium and chromedriver?
How to initiate Brave browser using Selenium and Python on Windows
you have to set your path to brave binary.
options.setBinary("Path to brave.exe");
Go through this website:
https://mundrisoft.com/tech-bytes/how-to-execute-selenium-script-in-brave-browser/
from selenium import webdriver
driver = webdriver.Chrome("C:\Chromedriver\chromedriver.exe")
driver.get('https://www.tokopedia.com/search?st=product&q=ps%205&navsource=home')
Whenever I try run the code above I get the following errors :
DevTools listening on ws://127.0.0.1:62443/devtools/browser/4cc18177-e5fa-4ed8-b260-13c1670788e2
[3140:8180:0617/184053.599:ERROR:device_event_log_impl.cc(214)] [18:40:53.599] USB: usb_device_handle_win.cc:1058 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)
[3140:8180:0617/184053.608:ERROR:device_event_log_impl.cc(214)] [18:40:53.608] Bluetooth: bluetooth_adapter_winrt.cc:1072 Getting Default Adapter failed.
and the Chrome browser that should stay open closes itself. What is the cause and how do I fix it ? thanks!
Works for me after adding chrome_options
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
driver = webdriver.Chrome("/path/to/chromedriver", chrome_options=chrome_options)
driver.get('https://www.tokopedia.com/search?st=product&q=ps%205&navsource=home')
I am using a script daily. It's a headless chrome that just checks a site every 5 minutes and suddenly devmode turned on and i can't seem to turn it off. This is my script:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('headless')
driver = webdriver.Chrome + 'E:\Chrome Downloads\chromedriver_win32\chromedriver.exe', chrome_options=options)
And the output is:
[0926/111600.894:ERROR:devtools_http_handler.cc(786)]
DevTools listening on 127.0.0.1:12107
[0926/111601.685:ERROR:browser_gpu_channel_host_factory.cc(103)] Failed to launch GPU process.
It also spews out the F12 developer console info everytime it connects to a new site. :c
I managed to fix it finally :D
options.add_argument('--log-level=3')
That was all it took.
I was running into the same issue and adding the log-level argument only did not work for me.
On Windows you obviously need to add the following option to your ChromeOptions as well:
options.add_experimental_option('excludeSwitches', ['enable-logging'])
As described here: https://bugs.chromium.org/p/chromedriver/issues/detail?id=2907#c3