Automating Facebook using Selenium Webdriver - python

driver = webdriver.Chrome('chromedriver')
driver.get('https://www.facebook.com/')
print("opened facebook")
I am using this code to open Facebook and the page opens.
driver.find_element(By.NAME, "email").send_keys("xxx")
sleep(1)
driver.find_element(By.NAME, "pass").send_keys("xxx")
sleep(1)
driver.find_element(By.NAME, "login").click()
sleep(1)
Then log in to my account. After successful login, my chrome window closes in a few seconds.
Can someone tell me why?
Full Code:
import time
import os
import wget
import shutil
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
try:
usr=""
pwd=""
driver = webdriver.Chrome('chromedriver')
driver.get('https://www.facebook.com/')
print ("Opened facebook")
driver.find_element(By.NAME, "email").send_keys(usr)
print ("Email Id entered")
sleep(1)
driver.find_element(By.NAME, "pass").send_keys(pwd)
print ("Password entered")
driver.find_element(By.NAME,"login").click()
sleep(100)
except Exception as e:
print("The error raised is: ", e)

The program will exit after executing code. Add below statements to keep program running:
time.sleep(300) #300 seconds i.e. 5 minutes
# close the browser window
driver.quit()

This will fix your problem
from selenium.webdriver.chrome.options import Options
# Stop Selenium from closing browser automatically
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
# Chrome driver to run chrome
driver = webdriver.Chrome(options=chrome_options)

Related

how to procses Cloudflare Turnstile in web driver with selenuim

i try to use selenuim module to get for some website unsuccsesfull.
this is what i try:
undetected_chromedriver
add user profile
use proxy.
when i open it with webdriver the Cloudflare not allow me to process.
when i open it with normal chrom its works fine.
any idias?
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from datetime import datetime
import time
from undetected_chromedriver import Chrome
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
print('Hey Elior, im on = ', current_time)
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\\Users\\Owner\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 12")
driver = Chrome(chrome_options=options)
driver.get("https://www.example.com")
def login():
driver.get("site")
# identify username, password and signin elements
time.sleep(1)
driver.find_element(By.NAME, "username").click()
driver.find_element(By.NAME, "username").send_keys('')
time.sleep(1)
driver.find_element(By.NAME, "password").click()
driver.find_element(By.NAME, "password").send_keys('')
time.sleep(1)
# press on login button
driver.find_element(By.ID, "login-submit").click()
driver.maximize_window()
driver.execute_script("console.clear()") # clean the console
time.sleep(3)
You could try using Selenium-Profiles.
Note that headless almost never works undetected.

python selenium checking for element in chrome doesn't work as expected

This works just fine in Firefox. When I use Chrome, once the page fully loads it doesn't print "element loaded", and it doesn't go to timeout either. It just waits forever.
I've tried using visibility_of_element_located instead of presence_of_element_located but it makes no difference. I've tried all_elements too. Any advice?
from selenium import webdriver
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.common.exceptions import TimeoutException
from selenium.webdriver.common.keys import Keys
browser = webdriver.Chrome()
browser.get("http://url.com")
timeout = 10
email = ("email#gmail.com")
try:
email_form_wait = WebDriverWait(browser, timeout).until(EC.presence_of_element_located((By.XPATH, '//*[#id="username"]')))
print ("element loaded")
email_form = browser.find_element(By.XPATH, '//*[#id="username"]')
email_form.send_keys(email, Keys.ENTER)
except TimeoutException:
print ("Loading took too much time!")
Update
For debugging reasons, I've been trying this
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time
browser = webdriver.Chrome()
browser.get("http://url.com")
time.sleep(5)
print("end wait")
This works as expected on Chrome, but when I add
email_form = browser.find_element(By.XPATH, '//*[#id="username"]')
print("element found")
at the end of the code, it doesn't even print "end wait", it's stuck waiting forever and then it prints "end wait" and returns an error only after I force close the browser.
Site Isolation is a security feature in Chrome that offers additional protection against some types of security bugs. It uses Chrome's sandbox to make it harder for untrustworthy websites to access or steal information from your accounts on other websites.
Try opening chrome driver with the following options supplied.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
options = webdriver.ChromeOptions()
options.add_argument("--disable-site-isolation-trials")
chrome_path = r"C:\Users\hpoddar\Desktop\Tools\chromedriver_win32\chromedriver.exe"
s = Service(chrome_path)
driver = webdriver.Chrome(service=s, chrome_options=options)
driver.get(url)
username = driver.find_element(By.XPATH, '//input[#id="username"]')
username.send_keys('email#gmail.com')
submit = driver.find_element(By.CSS_SELECTOR, '.ca56ae105.c03eb9739.c026ac3bb.ca0d6234f._button-login-id')
submit.click()
password = driver.find_element(By.XPATH, '//input[#id="password"]')
password.send_keys('password')
continueButton = driver.find_element(By.CSS_SELECTOR, '.ca56ae105.c03eb9739.c026ac3bb.ca0d6234f._button-login-password')
continueButton.click()

Python using selenium webdriver to fill a form, when submitting the form, chrome is saying no internet connection is available

I am using Selenium webdriver to fill out a form, the fields are good, but when I do a .submit using the login button, or do a .submit after the password field, the chrome browser will say "Unable to connect to internet. Please check your internet connection."
the internet is definitely up since I am able to open other websites on another browser (including a new session of chrome).
This is my code:
import time
import selenium
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = webdriver.ChromeOptions();
chrome_options.add_experimental_option("excludeSwitches", ['enable-automation']);
options = Options()
# options.add_argument('start-maximumized')
# options.add_argument('disable-infobars')
chrome_options.add_argument('no-sandbox')
PATH = "c:\scripts\chromedriver.exe"
# driver = webdriver.Chrome(executable_path=PATH)
driver = webdriver.Chrome(chrome_options=chrome_options,
executable_path='c:\scripts\chromedriver.exe')
driver.get('https://stupidwebsite.org')
time.sleep(1)
input_username = driver.find_element_by_id('username')
time.sleep(1)
input_username.send_keys("myself#email.com")
input_password = driver.find_element_by_id('password')
input_password.send_keys("mypassword#123")
input_password.submit()
# esubmit=driver.find_element_by_xpath
('/html/body/div/div[1]/div[2]/div/div/form/div[2]/div/button')
# esubmit = driver.find_element_by_name('MuiButton-label')
# esubmit.submit()
time.sleep(2)
# driver.quit()
I changed it a bit, and tried out google, and it works:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
#set chromodriver.exe path
driver = webdriver.Chrome(executable_path="C:\scripts\chromedriver.exe")
driver.implicitly_wait(0.5)
#launch URL
driver.get("https://www.google.com/")
#identify search box
m = driver.find_element_by_name("q")
#enter search text
m.send_keys("Tutorialspoint")
time.sleep(0.2)
#perform Google search with Keys.ENTER
m.send_keys(Keys.ENTER)
Please help me out. Thank you.

How to scroll in Instagram following window using Selenium and Python?

How would I scroll in the following window on Instagram using Selenium and Python? I've tried everything I've found and none of them work.
Here's what I have to get me to the following window:
from selenium import webdriver
browser = webdriver.Firefox(executable_path = '/usr/local/bin/geckodriver')
browser.implicitly_wait(5)
browser.get('https://www.instagram.com/')
sleep(2)
username_input = browser.find_element_by_css_selector("input[name='username']")
password_input = browser.find_element_by_css_selector("input[name='password']")
username_input.send_keys("Enter Username Here")
password_input.send_keys("Enter Password Here")
login_button = browser.find_element_by_xpath("//button[#type='submit']")
login_button.click()
sleep(5)
browser.find_element_by_xpath('/html/body/div[1]/section/main/div/div/div/section/div/button').click()
sleep(2)
browser.find_element_by_css_selector('button.aOOlW:nth-child(2)').click()
browser.get('https://www.instagram.com/instagram/')
sleep(2)
browser.find_element_by_css_selector('li.Y8-fY:nth-child(3) > a:nth-child(1) > div:nth-child(1)').click()
sleep(2)
follower_number =int( driver.find_elements_by_xpath('//span [#class="g47SY "]')[2].text)
i=0
while(i<follower_number):
element = driver.find_elements_by_xpath("//div[#role='dialog']//ul//li")
driver.execute_script("arguments[0].scrollIntoView(true);",element[i])```
have a look at this Question. i tried that and worked.
my code
from selenium.webdriver.common.action_chains import ActionChains
import time
import re
from webdriver_manager.chrome import ChromeDriverManager
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.maximize_window()
driver.get("https://www.instagram.com/")
time.sleep(5)
my_email=driver.find_element_by_xpath('//*[#id="loginForm"]/div/div[1]/div/label/input')
my_email.send_keys("")
my_password=driver.find_element_by_xpath('//*[#id="loginForm"]/div/div[2]/div/label/input')
my_password.send_keys("")
time.sleep(5)
login=driver.find_element_by_xpath('//*[#id="loginForm"]/div/div[3]')
login.click()
time.sleep(5)
driver.get("https://www.instagram.com/instagram/following/")
driver.find_element_by_css_selector('li.Y8-fY:nth-child(3) > a:nth-child(1) > div:nth-child(1)').click()
time.sleep(5)
# this is not perfect but it will get your work done
# getting the number of followers
follower_number =int( driver.find_elements_by_xpath('//span [#class="g47SY "]')[2].text)
i = 0
# looping for the exact number of followers ...
while(i<follower_number):
# as the website is dyanamic so updating the follwers list and also the webelement
element = driver.find_elements_by_xpath("//div[#role='dialog']//ul//li")
# executing scroll into view script to view the element and thats gonna load the next element(follower ) ..ultimately your scrolling achived
driver.execute_script("arguments[0].scrollIntoView(true);",element[i])
time.sleep(2)
print(i)
i=i+1

Selenium closing the brower alone after finishing all the function

I'm pretty new in programming so I might be an easy question, but I don't understand why the browsers opened by Selenium closes at the end of the code.
from lib2to3.pgen2 import driver
from selenium import webdriver
def Online_PLatform():
Driver = webdriver.Chrome()
Driver.get('https://elearningmarikina.ph/')
Gmail = Driver.find_element_by_xpath('/html/body/div[1]/div/div/div[1]/div[2]/div/div/form/div[1]/input')
Gmail.send_keys('958rectin#depedmarikina.com')
Pass = Driver.find_element_by_xpath('/html/body/div[1]/div/div/div[1]/div[2]/div/div/form/div[2]/input')
Pass.send_keys('33112')
Button = Driver.find_element_by_xpath('/html/body/div[1]/div/div/div[1]/div[2]/div/div/form/div[3]/button')
Button.click()
You can use 2 approaches in order to keep you driver open.
1.
Add the 'detach' option to your driver settings:
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
Simply add a delay at the end of your test code (less elegant approach but more simple)
from lib2to3.pgen2 import driver
from selenium import webdriver
import time
def Online_PLatform():
Driver = webdriver.Chrome()
Driver.get('https://elearningmarikina.ph/')
Gmail = Driver.find_element_by_xpath('/html/body/div[1]/div/div/div[1]/div[2]/div/div/form/div[1]/input')
Gmail.send_keys('958rectin#depedmarikina.com')
Pass = Driver.find_element_by_xpath('/html/body/div[1]/div/div/div[1]/div[2]/div/div/form/div[2]/input')
Pass.send_keys('33112')
Button = Driver.find_element_by_xpath('/html/body/div[1]/div/div/div[1]/div[2]/div/div/form/div[3]/button')
Button.click()
time.sleep(50)
This is because after the all functions, The code stops running and that's why selenium exits.
You can use the time module to delay.
import time
from lib2to3.pgen2 import driver
from selenium import webdriver
def Online_PLatform():
Driver = webdriver.Chrome()
Driver.get('https://elearningmarikina.ph/')
Gmail = Driver.find_element_by_xpath('/html/body/div[1]/div/div/div[1]/div[2]/div/div/form/div[1]/input')
Gmail.send_keys('958rectin#depedmarikina.com')
Pass = Driver.find_element_by_xpath('/html/body/div[1]/div/div/div[1]/div[2]/div/div/form/div[2]/input')
Pass.send_keys('33112')
Button = Driver.find_element_by_xpath('/html/body/div[1]/div/div/div[1]/div[2]/div/div/form/div[3]/button')
Button.click()
time.sleep(50) #---> 50 second delay

Categories