How to use Safari Browser in Selenium Webdriver using Python bindings - python

I am not able to launch safari browser from Selenium web driver. I am using Python bindings.
Could not find any reference that provided detailed steps for Python.
I am aware there is a related question on stackoverflow, but it doesn't answer for python -
python selenium webdriver safari driver
Followed the Documentation on http://code.google.com/p/selenium/wiki/SafariDriver , downloaded and installed safari developer certificate. This doc has code for Java and not for python bindings.
Below is my code(I am using desired capabilities) Not sure what am I missing here -
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium import webdriver
browser = webdriver.Remote(desired_capabilities=webdriver.DesiredCapabilities.SAFARI)
browser.get('http://www.google.com')
browser.close()

From the looks of it, you're trying to open a webpage with a webbrowser. Why not use "webbrowser"? It's easy to get the webbrowser have safari as default:
import webbrowser
browser = webbrowser.get('safari')
browser.open("http://www.google.com/")
As I can't selenium set up, I'm not sure if this is what you want. Who knows, it could be an alternative?

Related

Selenium with brave a chromium based browser(Version 1.33.106 Chromium: 96.0.4664.110 (Official Build) (64-bit)

I'm a beginner level python programmer,
I am currently working on a browser automater using selenium,
but currently i'am using brave version 96.0.4664.45
and my chrome driver is'nt working properly, whereas geckodriver is working fine with firfox
error here---> Errors with selenium library in python path and all correct with my side
Pls help me as soon as possible
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")
References
You can find a couple of relevant detailed discussions in:
How to use Brave web browser with python, selenium and chromedriver?
Use Selenium with Brave Browser pass service object written in python
How to initiate Brave browser using Selenium and Python on Windows
I suggest to try webdriver_manager
https://github.com/SergeyPirogov/webdriver_manager.
It helps to setup path to chromedriver.
For Chrome:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())
For Chromium:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.utils import ChromeType
driver = webdriver.Chrome(ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).install())
I suppose some of this will work for Brave.
Also, take a look at this example:
https://gist.github.com/passivebot/91d726bafc1f08eb475dd8384a3f21db

Python Selenium Detach Option Not Working

I want to write a Python script using Selenium and Chrome where Selenium won't close the Chrome browser when the script finishes. From doing a bunch of googling, it looks like the standard solution is to use the detach option. But when I run the following script:
import selenium
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
driver = webdriver.Chrome(options=chrome_options)
driver.get("https://www.google.com/")
It opens up Chrome, goes to Google's homepage, and then closes the browser. It's not throwing any errors.
Any idea why it's not working? I'm using the latest version of Google Chrome on Windows 10, and I've got the latest version of the selenium module installed. I couldn't find anything online that said the experimental detach option no longer existed. And I double checked the API, and it looks like it's the right syntax.
I discovered another way to go: start Chrome in remote debugging mode, then connect to it. That way, not only does the browser stay open, but you can also use your existing Chrome profile so you can take advantage of any sites your cookies allow you to access without having to log in every time you run the script.
Here's what you need to do if you're on Windows 10:
Start Google Chrome up remotely, pointed towards your existing user profile and the port you want to use:
cd "C:\Program Files (x86)\Google\Chrome\Application"
chrome.exe -remote-debugging-port=9014 --user-data-dir="%LOCALAPPDATA%\Google\Chrome\User Data"
In your python script, connect to the local port that this version of Chrome is running on:
import selenium
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", "localhost:9014")
driver = webdriver.Chrome(options=chrome_options)
driver.get("https://github.com/")
This code worked perfectly for me using : selenium-wire , hope it works for you
from seleniumwire import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
driver = webdriver.Chrome(ChromeDriverManager().install(),options=chrome_options)
driver.get("https://www.google.com/")

Can I use selenium webdriver on a non-gui server? (Python)

I want to use the selenium webdriver on a linux server running Ubuntu. How can I use selenium on said server without opening a browser, as it doesn't have one? I'm using Selenium with Python and Gecko(Firefox), but I'll be willing to switch to JavaScript or Chrome if I have to.

Selenium - Cant interact with web driver

im trying to load a specific chrome profile using Python selenium webdriver, but i cant interact with the driver after assigning the chrome profile. it opens the chrome profile that i wanted, but from there - nothing. i cant do any action. for example - im trying to open Microsoft.com:
This works:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('http://www.microsoft.com')
But this doesnt work at all:
from selenium import webdriver
import getpass
username = getpass.getuser()
options = webdriver.ChromeOptions()
options.add_argument('user-data-dir=C:/Users/'+username+'/AppData/Local/Google/Chrome/User Data/')
driver = webdriver.Chrome(executable_path='C:/Users/'+username+'/Documents/selProject/chromedriver.exe', chrome_options=options)
driver.get('http://www.microsoft.com')
The above code opens chrome, but doesnt go to microsoft.com or any other action.
thanks for reading!
This is likely because you have Chrome open already with that user signed in.
In order to use chrome with that profile, while also running the script you'll need to separate the directories where the profiles are pulled from. That is, move (or copy) the Default profile to another directory that you call to within the user-data-dir argument.
You do following things and check whether it works.
Upgrade python bindings using
pip install -U selenium
for chrome browser download the latest chrome driver "ChromeDriver 2.45"
form http://chromedriver.chromium.org/downloads and write the code
from selenium import webdriver
driver=webdriver.Chrome("Path of the Chromedriver" + "chromedriver.exe" )
driver.get('http://www.microsoft.com')

Python Selenium Firefox Accessibility issue

I tried selenium in Python to open a Google page, however, instead of loading the page, the Firefox browser keeps telling me the Accessibility issue. I followed the instruction to change the settings but can not find it!
Besides, I found a solution to a similar problem - Webdriver.get(url) open Firefox but not the URL, but after I linked geckodriver, the Accessibility problem is till there.
Could someone please help? Tens of Thanks in advance!!!!!
from selenium import webdriver
driver = webdriver.Firefox(executable_path=r'path\geckodriver.exe')
driver.get('http://www.google/com')
Screenshot - Accessibility issue
Screenshot - Firefox instruction
Screenshot - Can not find the setting

Categories