Selenium - Cant interact with web driver - python

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')

Related

How can I open websites with selenium/webdriver?

import time
from selenium import webdriver
driver = webdriver.Chrome('C:\Program Files\Google\Chrome\Application\chrome.exe')
driver.get('https://www.facebook.com')
time.sleep(5)
driver.quit()
error code: Executable_path has been deprecated, please pass in a service object.
The code above begins to open a Google Chrome tab but does not pick a user, and it will stop where Google Chrome shows all your users. I've tried using a specific profiles path but I've gotten various errors. If someone is able to resolve this issue I would appreciate it, I would like to open the Chrome tab as a guest.
It looks like your question has two parts. You are trying to figure out the webdriver and the user profile path. Allow me to answer both of these questions for you.
In the latest version of Selenium the executable_path parameter has been deprecated. Service objects containing the executable path are now required. There are two options for this.
Service objects
Option #1: Use your executable path
Append this import to your code:
from selenium.webdriver.chrome.service import Service
Then, include the service object as such:
driver = webdriver.Chrome(service=Service("C:\Program Files\Google\Chrome\Application\chrome.exe"))
Option #2: Let web driver manager handle it
This is great for when the driver becomes outdated. No need to redownload the driver.
First, go to the project directory in your terminal. If you are using PyCharm, there is no need to traverse to the directory, as you are already in the project directory.
Use pip to install web driver manager:
pip install webdriver_manager
Now, there is no need to enter an executable path:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get("https://www.facebook.com")
Selecting a user profile
This is fairly simple. First, go to chrome and enter chrome://version/ into the URL address bar. You will see the profile path. It will look like this C:\Users\yourprofile\AppData\Local\Google\Chrome\User Data\Default.
Then, include the following chrome options as such:
options = webdriver.ChromeOptions()
options.add_argument(r"--user-data-dir=C:\Users\yourprofile\AppData\Local\Google\Chrome\User Data")
options.add_argument(r"--profile-directory=Default")
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
This works for me.
The service is the path to the chrome driver you can download.
The chrome driver can be downloaded here: https://chromedriver.chromium.org/downloads
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
s = Service('/Users/macbook/PycharmProjects/chromedriver')
browser = webdriver.Chrome(service=s)
browser.get('https://www.facebook.com')
time.sleep(5)
browser.quit()
[1]: https://chromedriver.chromium.org/downloads

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/")

Python / Selenium - "chromedriver" executable needs to be in a PATH

I was trying to do something with a gecko driver ( Selenium in this case ) and I can't get my chrome driver to work. Knowing that I am on Mac Sierra, and that my script is in the same folder as my chrome driver, here is my code
from selenium import webdriver
import time
url = "https://accounts.google.com/signup/v2/webcreateaccount?flowName=GlifWebSignIn&flowEntry=SignUp"
driver = webdriver.Chrome("chromedriver")
driver.get(url)
Thanks for the help
You can test if it actually is in the PATH, Open your command prompt go to the location of your chromedriver and hit enter. you will get below message:
C:\>
chromedriver.exe
Starting ChromeDriver 76.0.3809.68 (420c9498db8ce8fcd190a954d51297672c1515d5-ref
s/branch-heads/3809#{#864}) on port 9515
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent
access by malicious code.
Then you need to setup PATH like below before you start your test:
driver = webdriver.Chrome('/path/to/chromedriver')
Try this:
from selenium import webdriver
driver= webdriver.Chrome(executable_path="C:/users/usr/Desktop/chromedriver.exe")
url = 'your website'
driver.get(url)
make sure you are using this slash (/) in your file path to the chrome driver (as shown above)

Selenium does not use my custom profile in Chrome

I am using selenium with chrome, but it does not use the profile that I specified, but starts with a new profile instead. Here is the python code:
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--user-data-dir=/home/username/.config/google-chrome/Profile 1')
driver = webdriver.Chrome(chrome_options=chrome_options)
The browser opens but it does not use my profile. The version of selenium is 3.5.0, the version of chrome driver is 2.31, the version of chrome is 60.0.3112.113, and I am on Ubuntu 16.04
To find path to your chrome profile data you need to type chrome://version/ into address bar . For ex. mine is displayed as
C:\Users\pc\AppData\Local\Google\Chrome\User Data\Default,
to use it in the script I had to exclude
\Default\
so we end up with only
C:\Users\pc\AppData\Local\Google\Chrome\User Data.
Also if you want to have separate profile just for selenium: replace the path with any other path and if it doesn't exist on start up chrome will create new profile and directory for it.
Hope this helps. Thanks.

Connecting to google using python stays blank

I am learning python and I am running into an issue
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('http://www.google.com')
my firefox launches but it does not go on google.com like it is supposed to be but rather stays as a blank page.
I would recommend using chrome instead because your script does work.
so... http://chromedriver.storage.googleapis.com/2.9/chromedriver_win32.zip Download that and extract it to your python27/scripts folder located in the root directory in the C:\\ Drive and then run this script.
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('http://www.facebook.com')
And It should connect you to facebook.com Hope I helped. :)

Categories