I'm trying to open a Firefox browser with undetected_chromedriver.
But only getting a default Firefox browser instead of getting the url I provided.
What did I miss or do wrong?
Here is the code I made so far.
import undetected_chromedriver as uc
import time
if __name__ == '__main__':
driver_path = "C:/Users/jay/Desktop/py/geckodriver.exe"
Firefox_path = "C:/Program Files/Mozilla Firefox/firefox.exe"
option = uc.ChromeOptions()
option.binary_location = Firefox_path
driver = uc.Chrome(executable_path=driver_path, options=option)
driver.get('https://google.com')
time.sleep(10)
I'd appreciate it if you could help me with this.
undetected_chromedriver is ONLY for chromedriver.
It modifies values directly inside binary file chromedrive.exe and it doesn't know how to modify values inside file geckodriver.exe.
See also repo GitHub - undetected-chromedriver.
There is:
"Works ... on .... Chromium based browsers".
"Automatically downloads the driver binary and patches it."
It means it automatically uses chromedrive.exe and it can't even use geckodriver.exe. And chromedrive.exe doesn't know how to communicate with Firefox - so it can't open page.
You can use it only with browsers which use engine Chromium - like Brave and maybe Opera, Microsoft Edge (but I didn't test it).
Related
When I run this code, the page opens and closes. I can't reach the page. Everything is in the latest version. I am a Windows user.
I was trying to open Instagram page with this code.
enter image description here
I tried to open the instagram site with this code and the site was closed as soon as it was opened.
Im not familiar with selenium but I know for a fact that
get() just gets the HTML code of the website and does not display it
You have to add the below Chrome Option:
options.add_experimental_option("detach", True)
Full code:
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_experimental_option("detach", True)
driver = webdriver.Chrome(service=Service(chrom_driver_pat), options=options)
driver.get(<url>)
Next time, don't post the image, post the code and explain your issue clearly.
First of all, please, do not use images when your want a code review or help, it is more easier to help with raw code.
Selenium now uses Service method to run webdriver.
To run what you want, you need to fix some parts of your code, like this:
from selenium import webdriver
# in my case, i'm using selenium==4.5.0, for this version, is necessary to
# use selenium.webdriver.chrome.service to use driver path
from selenium.webdriver.chrome.service import Service as ChromeService
# For Windows path, use r"C:\..." when you will use \
# Remember to set the executable file too
chrome_driver_path = r"C:\pythondriver\chromedriver\chromedriver.exe"
# you can use "universal" path too, like linux, using / instead of \
# chrome_driver_path = "C:/pythondriver/chromedriver/chromedriver.exe"
# instead using:
# webdriver.Chrome()
# driver = webdriver.Chrome(chrome_driver_path)
# use this:
chrome_service = ChromeService(chrome_driver_path)
driver = webdriver.Chrome(service=chrome_service)
url = "https://instagram.com"
driver.get(url)
I tested using the configurations below and worked for me:
Chromedriver - 108.0.5359.71
Chrome - 108.0.5359.125 - 64 bits
If you need more help using Chrome Service, try look the Selenium Docs
i am trying to control my browser using python, what I need is I give commands in terminal that should work on the browser like opening and searching for something(like scorling the bowser) and closing the browser
currently I am done with opening the browser and closing
yes, you can by using python selenium driver.
This is simple code to test. Also don't forget to download selenium driver.
https://chromedriver.chromium.org/downloads
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome('./chromedriver')
driver.get("https://www.python.org")
print(driver.title)
search_bar = driver.find_element_by_name("q")
search_bar.clear()
search_bar.send_keys("getting started with python")
search_bar.send_keys(Keys.RETURN)
print(driver.current_url)
driver.close()
Read docs in link
This is achievable (at least in POSIX systems like Linux or BSD) by using pipes.
https://www.codegrepper.com/code-examples/python/python+selenium+brave+browser
I see this example to use brave browser on windows. Is it supposed to work on Catalina as well by just replacing driver_path and brave_path?
Also, Chromedriver is only for Chrome. How to determine which version of chromedriver should be used for brave browser?
https://chromedriver.chromium.org
from selenium import webdriver
driver_path = "C:/Users/username/PycharmProjects/chromedriver.exe"
brave_path = "C:/Program Files (x86)/BraveSoftware/Brave-Browser/Application/brave.exe"
option = webdriver.ChromeOptions()
option.binary_location = brave_path
# option.add_argument("--incognito") OPTIONAL
# option.add_argument("--headless") OPTIONAL
# Create new Instance of Chrome
browser = webdriver.Chrome(executable_path=driver_path, chrome_options=option)
browser.get("https://www.google.es")
Prerequisites:
Your chromedriver version should match your Brave Browser web driver version.
To make sure it does:
Check ChromeDriver version with brew info chromedriver. Along the lines of the output it should read chromedriver: 89.0.4389.23 (latest version as of writing this post)
Open Brave Browser, in menu bar click Brave -> About Brave. Along the lines it should read Version 1.22.71 Chromium: 89.0.4389.114 (Official Build) (x86_64) (again, latest as of writing this post)
These two should match, however, i am not entirely sure to which degree, since, as you can see here, last entries (.23 and .114) don't match, yet this works perfectly fine on my machine (macOS Big Sur 11.2.3) I don't think macOS version should really matter, but i still mentioned it for the sake of completeness.
Finally run the following code (replace paths with ones on your machine if they are different):
from selenium import webdriver
driverPath = '/usr/local/Caskroom/chromedriver/89.0.4389.23/chromedriver'
binaryPath = '/Applications/Brave Browser.app/Contents/MacOS/Brave Browser'
options = webdriver.ChromeOptions()
options.binary_location = binaryPath
browser = webdriver.Chrome(executable_path=driverPath, chrome_options=options)
browser.get("https://www.google.es")
If you have never used chromedriver before, after runnning the code you should see a macOS prompt saying that chromedriver is from unknown developer or was downloaded from the internet, smth like that. Close that prompt (It's important that you do this before moving on). Then go to System Preferences -> Security & Privacy -> press the lock icon and unlock it and then approve chromedriver to run on your machine. Run the above code again, a new macOS prompt will appear saying smth about unknown developer again, this time you can just click Open. Brave Browser window should pop up at this point. At least it did on my machine.
P.S. I apologise for possibly going into too much detail, but sometimes i get really frustrated with answers which skip parts which are considered to be obvious
For the next person looking, this is the most current way of using Brave with Selenium on Mac:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
driverPath = "/Applications/chromedriver" # Path to ChromeDriver
service = Service(driverPath)
options = webdriver.ChromeOptions()
options.binary_location = "/Applications/Brave Browser.app/Contents/MacOS/Brave Browser" # Path to Brave Browser (this is the default)
driver = webdriver.Chrome(service=service, options=options)
# From here its Selenium as usual, example:
driver.get("https://google.com")
print(driver.title)
driver.close()
I would like to log into a website and download a file. I'm using selenium and the chromedriver. Would like to know if there is a better way. It currently opens up a chrome browser window and sends the info. I don't want to see the browser window opened up and the data being sent. Just want to send it and return the data into a variable.
from selenium import webdriver
driver = webdriver.Chrome()
def site_login(URL,ID_username,ID_password,ID_submit,name,pas):
driver.get(URL)
driver.find_element_by_id(ID_username).send_keys(name)
driver.find_element_by_id(ID_password).send_keys(pas)
driver.find_element_by_id(ID_submit).click()
URL = "www.mywebsite.com/login"
ID_username = "name"
ID_password = "password"
ID_submit = "submit"
name = "myemail#mail.com"
pas = "mypassword"
resp=site_login(URL,ID_username,ID_password,ID_submit,name,pas)
You can run chrome in headless mode. In which case, the chrome UI won't show up and still performing the task you were doing. Some article I found on this https://intoli.com/blog/running-selenium-with-headless-chrome/. Hope this helps.
First option: If you are able to change the driver, you can use phantom-js as driver. That was a headless browser and you can use it with selenium.
Second option: If the site are not dynamic (easily called it SPA) or you are able to trace packet (which can be done in chrome dev tools), you can directly use request with the help of beautifulsoup if you need to get some data on the page.
Just add this two lines
chrome_options = Options()
chrome_options.add_argument("--headless")
This should make chrome run in the background.
Is it possible to connect selenium to the browser I use normally instead a driver? For normal browsing I am using chrome with several plugins - add block plus, flashblock and several more. I want to try to load a site using this specific configuration. How can I do that?
p.s - I dont want to connect only to an open browser like in this question :
How to connect to an already open browser?
I dont care if I spawn the process using a driver. I just want the full browser configuration - cookies,plugins,fonts etc.
Thanks
First, you need to download the ChromeDriver, then either put the path to the executeable to the PATH environment variable, or pass the path in the executable_path argument:
from selenium import webdriver
driver = webdriver.Chrome(executable_path='/path/to/executeable/chrome/driver')
In order to load extensions, you would need to set ChromeOptions:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = webdriver.ChromeOptions()
options.add_extension('Adblock-Plus_v1.4.1.crx')
driver = webdriver.Chrome(chrome_options=options)
You can also save the chrome user profile you have and load it to the ChromeDriver:
options = webdriver.ChromeOptions()
options.add_argument('--user-data-dir=/path/to/my/profile')
driver = webdriver.Chrome(chrome_options=options)
See also:
Running Selenium WebDriver using Python with extensions (.crx files)
ChromeDriver capabilities/options