Selenium/Python/Chrome: Browser Chrome can`t create directory 'Selenium' - python-3.x

Application that normally works on the PC under this User (Administrator) suddenly begins to launch Windows alarm-window: "Browser Google Chrome can't execute read and write operations in directory 'selenium'."
(But after 'Cancel' this alarm - app works Ok - it get the page in Chrome, and handles it normally).
But the app opens different pages in the new Chrome-window many-many times, and i can`t to sit and press 'Cancel' all the time )))
Aditionaly, this code normally work on enother PC in the remote workplace.
Code:
```python
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
...
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=selenium")
options.add_argument("--remote-debugging-port=9222")
driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)
try:
driver.get(url)
...
```

Related

Trying to close portable browser via selenium

I am trying to close portable browser via selenium
I passed --remote-debugging-port=9222 because if I do not pass it then the program is stuck in object creation of webdriver.Chrome(). It will open the portable browser but not load the URL.
But after the URL is open I want to close the browser but driver.quit() it is not working for me. I have tried some other methods for close the browser but they do not work as well.
I want to close the specific instance of the browser which is opened by this program not other opened instances of the browser.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.binary_location = 'C:/Portable/GoogleChromePortable/GoogleChromePortable.exe'
chrome_options.add_argument("--remote-debugging-port=9222")
chrome_options.add_argument("--incognito")
chrome_options.add_argument("--profile-directory=Person 1")
driver = webdriver.Chrome(options=chrome_options,executable_path='C:/Portabl/chromedriver_win32/chromedriver.exe')
url = "https://www.google.com/"
driver.get(url)
driver.quit()
I am using:
selenium 3.141.0, windows 10, python 3.8.0, portable chrome version 93.0.4577.63 (32-bit)
Your this statement
I passed --remote-debugging-port=9222 because if I do not pass it then
the program is stuck in object creation of webdriver.Chrome()
is not correct. --remote-debugging-port=9222 looks like a port number where your application is deployed and you have used chrome option to send them to browser object.
driver.quit()
this typically should have worked, what is the error when it did not work ?
also, for closing a single instance you could do
driver.close()
see if that helps.

Selenium using Chrome browser - Timeout during HTTP basic authentication [Python]

As a part of automation, I am trying to login to a locally hosted website using selenium. The page serves a HTTP Basic authentication popup and I use the below code to send the credentials. However, upon using a debugger and executing the code step-wise, I deciphered that a TimeOut exception occurs repeatedly (at line marked with a comment beside it).
Details
I tried on Chrome browser and its corresponding chrome WebDriver for all versions from 79.0 till the latest 84.0, but this exception seems to occur in all the cases.
OS - Windows Server W2k12 VM. [Tried on Windows 10 as well]
Python version 3.8
Code
import time
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument('--ignore-ssl-errors')
driver = webdriver.Chrome(chrome_options=options)
driver.maximize_window()
driver.get(url)
wait = WebDriverWait(driver, 10)
alert = wait.until(EC.alert_is_present()) # This line causes the time out exception
alert = driver.switch_to.alert
alert.send_keys('domain\username' + Keys.TAB + 'password')
alert.accept()
time.sleep(5)
Note:
Due to a bug in the internal page, I cannot send the credentials through the URL using the https://username:password#ipaddress:port format, so this has forced me to resort to making the above selenium method as my only choice.
The corresponding code for firefox works well (for the same target internal website)
Probable Hunch
I wonder, if I am missing any packages on the newly created VM which is crucial for chrome WebDriver to work. For example, Firefox Gecko driver required Visual studio redistributables in order to work. Does chrome WebDriver require any such equivalent packages?
I don't believe that the Basic Auth popup is exposed as an "alert" in ChromeDriver, so AFAIK your only option is https://username:password#ipaddress:port. Interesting that you say that you can program to the popup in Firefox.
Until Chrome 78, the auth popup would display and block the test script, and you had to "manually" enter the credentials (or use a more general "desktop window manipulation" API), but I don't think that works anymore.
What worked here finally was to discard using the Selenium way of using send_keys() approach but use other packages to do the job. The following two snippets worked.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import keyboard
​
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument('--ignore-ssl-errors')
driver = webdriver.Chrome(r"path_to_chromedriver.exe", options=options)
driver.maximize_window()
driver.get("<replace with url>")
​
keyboard.write(r"<replace with username>")
keyboard.press_and_release("tab")
keyboard.write("<replace with password>")
keyboard.press_and_release("tab")
keyboard.press_and_release("enter")
Or this one (pip install pywin32 before)
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import win32com.client
​
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument('--ignore-ssl-errors')
driver = webdriver.Chrome(r"path_to_chromedriver.exe", options=options)
driver.maximize_window()
driver.get("<replace with url>")
​
shell = win32com.client.Dispatch("WScript.Shell")
shell.SendKeys(r"<replace with username>")
shell.SendKeys("{TAB}")
shell.SendKeys("<replace with password>")
shell.SendKeys("{TAB}")

extract and save whatsapp session from chrome browser local storage and then use it on another device

To avoid mark as duplicate then I put some source where I've read :
Python and Selenium - Reboot program and reuse same browser session
Python and Selenium - Reuse a whatsapp web session
Currently I want to make python selenium application so that it could send my message to my customer. On those 2 articles on above say if possible to use local storage to keep logged in into web.whatsapp.com after scan qr-code once.
this is my code :
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time
import sys
from selenium.webdriver.chrome.options import Options
chromepath = r'E:\chromedriver\chromedriver.exe'
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\\Users\\user\\AppData\\Local\\Google\\Chrome\\User Data")
driver = webdriver.Chrome(executable_path=chromepath, chrome_options=options)
This code worked well, so I can open web.whatsapp.com with login at once, and then use it to send message.
Q : Is possible to save whatsapp session from chrome local storage to database eg : mysql or etc so that I can use it to make whatsapp web keep logged in although with different device / computer.
In other word is possible to change the value of :
user-data-dir=C:\\Users\\user\\AppData\\Local\\Google\\Chrome\\User Data
with the value of web.whatsapp.com session that stored in my database?
Note : if not possible, please tell me another solution so that I still can use it to access whatsapp account in another device without copy user data folder and then move to new device.
Thanks
if using linux or mac go to your terminal and type :
chromium --remote-debugging-port=9250
where chromium is the browser and 9250 is a port number which you can choose and then in your selenium file while making a selenium object
opts=Options()
opts.add_experimental_option("debuggerAddress","localhost:9250")
driver=webdriver.Chrome(options=opts)
and this shall open your browser with every websites logged in.

WhatsApp without QR code scanning everytime through Selenium WebDriver

Is there any whatsapp or webdriver API to access WhatsApp web without scanning QR code everytime while accessing it using selenium and chrome webdriver in python?
This is What you need. This code Read QR and store it in cookies
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
jokes = ["You don't need a parachute to go skydiving. You need a parachute to go skydiving twice.",
"This is Test Message."]
options = Options()
options.add_argument("--user-data-dir=chrome-data")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome('/usr/local/bin/chromedriver', options=options)
driver.maximize_window()
driver.get('https://web.whatsapp.com') # Already authenticated
time.sleep(20)
##################### Provide Recepient Name Here ###############################
driver.find_element_by_xpath("//*[#title='MyJakartaNumber']").click()
for joke in jokes:
driver.find_element_by_xpath('//*[#id="main"]/footer/div[1]/div[2]/div/div[2]').send_keys(joke)
driver.find_element_by_xpath('//*[#id="main"]/footer/div[1]/div[3]/button/span').click()
time.sleep(10)
time.sleep(30)
driver.close()
Your "WhatsApp" and "QR Code" don't tell anything to me, however if you're testing an application which requires an extra action to sign in I don't think you will be able to perform it using Selenium as it's browser automation framework.
Web applications identify users via Cookies - special HTTP Headers containing client-side information. When you start a web browser via Selenium bindings - it kicks off a clean browser session which is not authenticated in "WhatsApp" (whatever it is)
The solutions would be in:
Authenticate in WhatsApp manually, store your browser profile somewhere and start Selenium by pointing it to the earlier storied profile folder
Authenticate in WhatsApp manually, store your browser cookies and use WebDriver.add_cookie() function to read the stored cookies into the current clean session
You can use "pywhatkit". pywhatkit is used to send messages using whatssapp web. Run:
pip install pywhatkit
and you are good to go.

How to use Chrome Webdriver cache for Whatsapp Web?

I created a script to send messages in an automated way through the WhatsApp Web.
However, I am not able to save the cache, containing the QR Code of Authentication, to the Chrome Web Driver through Selenium.
Each time the browser is opened to send a new message, reading the QR Code is requested again.
I'm using Ubuntu and Google Chrome on version 74.0.3729.157
What I've tried so far:
options = Options()
options.add_argument("--disable-extensions")
options.add_argument('--ignore-certificate-errors')
options.add_argument('--user-data-dir=./User_Data')
options.add_argument("--test-type")
driver = webdriver.Chrome(options=options, executable_path="/usr/bin/chromedriver")
driver.get(url)
time.sleep(10)
python_button = driver.find_element_by_id("action-button")
python_button.click()
time.sleep(30)
python_button2 = driver.find_element_by_css_selector("span[data-icon='send']")
python_button2.click()
Content of User_Data folder:
What's missing so I do not have to scan the QR code every time?

Resources