Reputation: 43
Cheers, I can not execute my selenium python script with the default chrome profile (aka the one I am connected to with email), The code works just fine in guest mode (The profile that selenium defaults to when running the script) but It refuses to even execute when adding the following line :
options.add_argument("user-data-dir=C:\\Users\\Faycal\\AppData\\Local\\Google\\Chrome\\User Data")
After running the code I am prompted with the following browser error :
There are no errors in the terminal and the script does not even run, I tried adding \ at the end of the path, I tried changing the profile by adding another chromedriver option but nothing seems to work. Hopefully someone can help, thanks alot and have a nice one.
Edit 1: Here is the code I am trying to run :
import undetected_chromedriver as uc
import time
from selenium.webdriver import ActionChains, Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.color import Color
if __name__ == '__main__':
PATH = "C:\Program Files (x86)\chromedriver.exe"
url = "https://portail.if-algerie.com/login"
user_data_dir = r'C:\Users\Faycal\AppData\Local\Google\Chrome\User Data'
options = uc.ChromeOptions()
options.add_argument("--start-maximized")
options.add_argument('--disable-blink-features=AutomationControlled')
options.add_argument("--disable-plugins-discovery")
options.add_argument('--user-data-dir='+user_data_dir)
options.add_argument('--profile-directory=Default')
driver = uc.Chrome(options=options)
while(True):
try:
driver.get("https://portail.if-algerie.com/login")
break
except:
driver.refresh()
continue
driver.implicitly_wait(15)
email = driver.find_element(by=By.NAME, value="email")
password = driver.find_element(by=By.NAME, value="password")
login_button = driver.find_element(by=By.ID, value="login")
email.send_keys("mail")
password.send_keys("pwd")
time.sleep(2)
login_button.click()
time.sleep(2)
link = driver.find_element(by=By.LINK_TEXT, value="Tests et Examens")
link.click()
register_link = driver.find_element(by=By.LINK_TEXT, value="S'inscrire")
register_link.click()
time.sleep(2)```
Upvotes: 2
Views: 2994
Reputation: 4198
You have to add two arguments in order to achieve the desired result. The first is the one you already added, and the second is the "profile-directory".
# The r means that the string is to be treated as a raw string,
# which means all escape codes will be ignored.
user_data_dir = r'C:\Users\Faycal\AppData\Local\Google\Chrome\User Data'
# User's data path
options.add_argument('--user-data-dir='+user_data_dir)
# Profile directory
options.add_argument('--profile-directory=Default')
As for the script of your first edit, the problem is that you used a while
loop, which is a bad practice in this case. You should use the WebDriverWait
class of Selenium
instead, and the conditions of the expected_conditions
.
Some examples of such conditions are the following.
presence_of_element_located:
An expectation for checking that an element is present on the DOM of a page. This does not necessarily mean that the element is visible. locator - used to find the element returns the WebElement once it is located.
element_to_be_clickable:
An expectation for checking an element is visible and enabled such that you can click it.
Element is either a locator (text) or an WebElement.
With that being said, a login function should look something like this:
import undetected_chromedriver as uc
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.common.exceptions import TimeoutException
import time
def login():
try:
timeout = 20 # Number of seconds before timing out.
user_data_dir = r'C:\Users\Faycal\AppData\Local\Google\Chrome\User Data'
options = uc.ChromeOptions()
options.add_argument("--start-maximized")
options.add_argument('--disable-blink-features=AutomationControlled')
options.add_argument("--disable-plugins-discovery")
options.add_argument('--user-data-dir=' + user_data_dir)
options.add_argument('--profile-directory=Default')
driver = uc.Chrome(options=options)
driver.get(r'https://portail.if-algerie.com/login')
email = WebDriverWait(driver, timeout).until(EC.presence_of_element_located((By.NAME, "email")))
email.send_keys("mail")
password = WebDriverWait(driver, timeout).until(EC.presence_of_element_located((By.NAME, "password")))
password.send_keys("pwd")
login_button = WebDriverWait(driver, timeout).until(EC.element_to_be_clickable((By.ID, "login")))
login_button.click()
time.sleep(50)
except TimeoutException as e:
print(e)
if __name__ == '__main__':
login()
In the above snippet, Selenium WebDriver
waits up to 20 seconds before throwing a TimeoutException
unless it finds the element to return within the above time. WebDriverWait
, by default, calls the ExpectedCondition
every 500 milliseconds until it returns successfully.
Upvotes: 4