Why isn't Selenium chromedriver adding a profile? - python

I am trying to add a chrome profile into my program, but it is not doing anything. Can someone please help?
I am using Selenium python. When I run it seems to ignore the statements and runs in a temp profile.
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import random
import string
from selenium.webdriver.chrome.options import Options
while True:
emailstarter = random.randint(11111111111111111111,999999999999999999999)
first = random.randint(666666,888888)
last = random.randint(555,55555)
driver = webdriver.Chrome(r"C:\Program Files (x86)\chromedriver.exe") # Optional argument, if not specified will search path.
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\\Users\\user\\AppData\\Local\\Google\\Chrome\\User Data\\Default");

You have the order wrong. You should create the options first and then pass them as an argument to the webdriver.Chrome class:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\\Users\\user\\AppData\\Local\\Google\\Chrome\\User Data\\Default");
driver = webdriver.Chrome(
executable_path=r"C:\Program Files (x86)\chromedriver.exe",
options=options
)

Related

Selenium Python,default profile is not loading after using ChromeDriverManager().install()

After switching to ChromeDriverManager().install(),I am not able to use default profile.Any workaround there so that I can avoid logging in every time?
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()),....)
used many solutions found here, but still not working.
directories:
user-data-dir=C:\Users\xxx\AppData\Local\Google\Chrome\User Data
user-data-dir=C:\Users\xxx\AppData\Local\Google\Chrome\User Data\Default
To use the default profile, you have to mention the path like below:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
options = Options()
# path of the chrome profile's parent directory
options.add_argument(r"user-data-dir=C:\\Users\\User\\AppData\\Local\\Google\\Chrome\\User Data")
# name of the directory
options.add_argument("--profile-directory=Default")
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)

python selenium sendkey doesn't work in chrome

I'm trying to launch some browser (Chrome) functions by sending shortcuts
I have tried several methods but they all cannot work.
I do this by following these steps
Initialize the browser
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
options = Options()
options.add_argument("--user-data-dir="+r"path_to_user_data")
options.add_experimental_option("excludeSwitches", ["enable-logging"])
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install())
,options=options
)
Through actionchain
ActionChains(driver).key_down(Keys.CONTROL).key_down('T').key_up('T').key_up(Keys.CONTROL).perform
Through send_key
driver.find_element(by=By.XPATH,value="/html/body").send_keys(Keys.CONTROL+"T")
But it doesn't work. This is confused for me. Why it cannot work?
It is possible to open a new tab and switch to it with a single command
driver.switch_to.new_window()
To Open a blank new tab you can use the below:
driver.execute_script("window.open('');")
Switch to the new window
driver.switch_to.window(driver.window_handles[1])

Add color to Selenium Webdriver window

I would like give a color to the Selenium WebDriver window's frame. (Attaching Image)enter image description here
I tired to do it with chrome_options = Options()
and
chrome_options.add_argument("default-background-color FFFFFF00")
That's my code:
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import ElementClickInterceptedException
from selenium.common.exceptions import ElementNotInteractableException
from selenium.common.exceptions import StaleElementReferenceException
import requests
import time
# Chrome driver
s = Service('C:\Program Files (x86)\chromedriver.exe')
chrome_options = Options()
chrome_options.add_argument("default-background-color FFFFFF00")
driver = webdriver.Chrome(service=s, options=chrome_options)
driver.maximize_window()
driver.get("https://www.google.com/")
It's not working for me.
When happens is that the browser is opening with 2 tabs. The URL of the first one is the "80808000" and the second one is Google.com
enter image description here
There is any solution for it?
Thanks!
Looks like FFFFFF00 is the default background color.
Please try setting it to any other value like 80808000and see if it makes changes.
chrome_options = Options()
chrome_options.add_argument("default-background-color 80808000")
driver = webdriver.Chrome(service=s, options=chrome_options)
Then you will be able to set the color you want.
I guess that there is an issue with the syntax. I am not sure, but you can try:
chrome_options.add_argument("--default-background-color ff0000ff")
chrome_options.add_argument("--default-background-color=ff0000ff")
chrome_options.add_argument("--default-background-color:ff0000ff")
The argument "--default-background-color" is present in this list:
https://peter.sh/experiments/chromium-command-line-switches/ (referred from https://chromedriver.chromium.org/capabilities)
https://source.chromium.org/chromium/chromium/src/+/main:headless/app/headless_shell.cc;l=363?q=default-background-color&sq=&ss=chromium

I can't locate the element but after inspect element manually the code works

I get this error when I run the code :
Message: no such element: Unable to locate element: {"method":"xpath","selector":"//div[#data-test-id="birth-date__day"]/select"
If I inspect Element manually in browser the code works. I was thinking it's because of some frame but can't find it. Also I tried to first click and then try to use "Select"but still not works.
This is the code :
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
import requests
import time
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
options = webdriver.ChromeOptions()
options.add_experimental_option("useAutomationExtension", False)
options.add_experimental_option("excludeSwitches",["enable-automation"])
driver_path = 'C:\Program Files (x86)\chromedriver.exe'
driver = webdriver.Chrome(executable_path=driver_path, options=options)
url = "https://account.mail.ru/signup?from=main&rf=auth.mail.ru"
Fname = "John"
Lname = "Micheals"
driver.get(url)
time.sleep(4)
day = driver.find_element_by_xpath('//div[#data-test-id="birth-date__day"]/select')
Select(day).select_by_value("11")
Please if someone can help.

Python Selenium chrome - how to disable user input

Below is a simple code which opens a google chrome instance and then enters the text "This is some very long text".
Is there a way to disable user keyboard input while the above text is being typed in google chrome?
The goal would be that the user can not enter anything by mistake on that page while working parallely on the computer on other things.
I understand that one option would be to take the chrome instance "headless" but that is not an option for the website I want to work on.
What I have done so far:
I had a look at the selenium docs at http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.chrome.options
but I could not find any such option.
But then again, the above website does not have info on "--disable-notifications" which is something you can use with selenium chrome. So, maybe there is a chance that there is some hidden way to do what I am looking for.
Also checked https://peter.sh/experiments/chromium-command-line-switches/ but no luck
import time, datetime, sys, os
from datetime import datetime
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
CHROME_PATH = 'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe'
CHROMEDRIVER_PATH = '..\\chromedriver.exe' #location of your chromedriver.exe
WINDOW_SIZE = "1920,1080"
chrome_options = Options()
#chrome_options.add_argument("--headless")
chrome_options.add_argument("--window-size=%s" % WINDOW_SIZE)
chrome_options.add_argument("disable-gpu")
chrome_options.add_argument("disable-infobars")
chrome_options.add_argument("--disable-notifications")
chrome_options.binary_location = CHROME_PATH
browser = webdriver.Chrome(executable_path=CHROMEDRIVER_PATH,chrome_options=chrome_options)
browser.get("https://www.google.com")
elements = browser.find_elements_by_css_selector('#lst-ib')
if not elements:
print("NO SUCH ELEMENT FOUND!")
print("Fatal Error:Please perform all tasks manually")
else:
item1 = browser.find_element_by_css_selector('#lst-ib')
item1.send_keys('This is some very long text'+Keys.TAB)
Or if anyone can point me in which direction I should be doing my research, that would be great too!!
Thanks
I think this is what you wanted`
import time, datetime, sys, os
from datetime import datetime
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
CHROME_PATH = 'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe'
CHROMEDRIVER_PATH = 'C:\\python27\\chromedriver.exe' # location of your
chromedriver.exe
WINDOW_SIZE = "1920,1080"
chrome_options = Options()
# chrome_options.add_argument("--headless")
chrome_options.add_argument("--window-size=%s" % WINDOW_SIZE)
chrome_options.add_argument("disable-gpu")
chrome_options.add_argument("disable-infobars")
chrome_options.add_argument("--disable-notifications")
chrome_options.binary_location = CHROME_PATH
browser = webdriver.Chrome(executable_path=CHROMEDRIVER_PATH,
chrome_options=chrome_options)
browser.get("https://www.google.com")
elements = browser.find_elements_by_css_selector('#lst-ib')
if not elements:
print("NO SUCH ELEMENT FOUND!")
print("Fatal Error:Please perform all tasks manually")
else:
browser.execute_script('document.getElementById("lst-ib").onkeypress=function()
{return false;}')
browser.execute_script('document.getElementById("lst-ib").value = "this is some
text"')
I suggest you try the kiosk mode option once...
chrome_options = Options()
chrome_options.add_argument("--kiosk")

Categories