Selenium closing the brower alone after finishing all the function - python

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

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.

Webdriver ( Selenium ) Cannot find the element

`
import requests
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from time import sleep
import os
login_page = "https://fap.fpt.edu.vn/Default.aspx"
# page = "https://fap.fpt.edu.vn/Report/ScheduleOfWeek.aspx"
email = ""
password = ""
options = Options()
options.add_argument("--window-size=1920,1080")
options.binary_location = "C:\Program Files\Google\Chrome\Application\chrome.exe"
driver = webdriver.Chrome(options = options)
driver.get(login_page)
login1 = driver.find_element("xpath","//div[#class='abcRioButtonContentWrapper']").click()
driver.find_element(By.NAME, "identifier").send_keys(email)
sleep(3)
driver.find_element(By.ID, "identifierNext").click()
sleep(2)
driver.find_element(By.NAME, "password").send_keys(password)
sleep(2)
driver.find_element(By.ID, "passwordNext").click()
sleep(999999)
`
I believe i chose the right By.NAME ( "indentifier" ) but the code still not work and return message no such element.
I tried to change the syntax, using xpath change By.NAME to By.ID but it still not work
Google login page opens in a new window. So you need to switch to this window before interacting with it. So you need to use this code (the first and the last lines are from your code and between is the part you need to add):
login1 = driver.find_element("xpath","//div[#class='abcRioButtonContentWrapper']").click()
windows = driver.window_handles
driver.switch_to.window(windows[1])
driver.find_element(By.NAME, "identifier").send_keys(email)
And after you've finished with the login you will need to switch back to the main window. To do that you can use this code:
driver.switch_to.window(windows[0])
And then work with the content of the page
No, don't use name of jsname here, because they get filled with random data.
Just find your Google account name or your email address by text and click it:
userAccount = driver.find_element_by_xpath("//*[text()='YourGoogleAccountName']")
userAccount.click()

Google chrome closes automatically after executing Python selenium code despite using the option class to keep it open

I have checked online and it was mentioned that Selenium closes the browser after running unless you use the option module or the driver.quit() or driver.close() functions but I used the option as shown in the code below but Chrome still closes after 2-3 seconds.
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
chrome_driver_path = r"C:\Development\chromedriver.exe"
serv = Service(chrome_driver_path)
driver = webdriver.Chrome(service=serv, options=chrome_options)
driver.get("https://www.google.com")

With Headless nothing happens and without Headless it collects the data perfectly in both webdriver.chrome and webdriver.firefox

The idea would be to literally do the same keyboard work:
Step 1 -> Open Web Page
Step 2 -> Ctrl + A (Select All)
Step 3 -> Ctrl + C (Copy)
Here's how I use it for Chrome:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import pyperclip
import time
option = webdriver.ChromeOptions()
option.add_argument('headless')
driver = webdriver.Chrome(options=option)
link='https://sports.staticcache.org/scoreboards/scoreboards-football/index.html?eventId=21150687'
driver.get(link)
time.sleep(10)
element=driver.find_element_by_tag_name('body')
element.send_keys(Keys.CONTROL,'a')
element.send_keys(Keys.CONTROL,'c')
driver.quit()
alltext=pyperclip.paste()
print(alltext)
Here's how I use it for Firefox:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.keys import Keys
import pyperclip
import time
option = Options()
option.headless = True
driver = webdriver.Firefox()
link='https://sports.staticcache.org/scoreboards/scoreboards-football/index.html?eventId=21150687'
driver.get(link)
time.sleep(10)
element=driver.find_element_by_tag_name('body')
element.send_keys(Keys.CONTROL,'a')
element.send_keys(Keys.CONTROL,'c')
driver.quit()
alltext=pyperclip.paste()
print(alltext)
In both options when Headless is not activated, it works perfectly, but when it is activated nothing happens and the script finishes running without anything being delivered.
Is there anything I can do to resolve this?
As you are trying to just print the content of the page, So instead of using send_keys you can try with the text
option = webdriver.ChromeOptions()
option.add_argument('headless')
driver = webdriver.Chrome("<Path of chromeDriver>",chrome_options=option)
link='https://sports.staticcache.org/scoreboards/scoreboards-
football/index.html?eventId=21150687'
driver.get(link)
time.sleep(10)
element=driver.find_element_by_tag_name('body')
print(element.text)

Get info and warnings consol logs from Chrome with Selenium from specific commands

I am trying to get logs from Chrome's console using Selenium with Python and a little bit lost because it is my first experience with it.
In general, the code works and prints logs, but I need to see some specific events, that I normally see by typing a command _newsb.getEv.getBuffer() (it is an imaginary command, I am using something similar). With Selenium, I am trying to input it like this driver.execute_script("_newsb.getEv.getBuffer()"), but I don't see events.
Here is the code:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
dc = DesiredCapabilities.CHROME
dc['goog:loggingPrefs'] = { 'browser':'ALL' }
driver = webdriver.Chrome(desired_capabilities=dc, service_args=["--verbose", "--log-path=D:\\qc1.log"])
driver.implicitly_wait(30)
driver.get('https://www.somewebsite.com/')
page = driver.find_element_by_tag_name("html")
driver.execute_script("window.scrollTo(0, 400)")
time.sleep(3)
driver.execute_script("_newsb.getEv.getBuffer()")
time.sleep(3)
driver.execute_script("window.scrollTo(0, window.scrollY + 400)")
time.sleep(3)
driver.execute_script("window.scrollTo(0, window.scrollY + 400)")
time.sleep(1)
for entry in driver.get_log('browser'):
print(entry)
driver.quit()
Could someone please point out what I am doing wrong and how do you input in console with Selenium and Python? Any advice is appreciated.
Also, how to see all logs, including info and Verbose, not just errors?
options.set_capability("goog:loggingPrefs", { # old: loggingPrefs
"browser": "ALL"})
driver = webdriver.Chrome(
options=options
)
# returns a list of all events
driver.execute_script("window.open('');")
driver.switch_to.window(driver.window_handles[1])
driver.get('http://www.google.com')
driver.execute_script("console.error('This is error')")
driver.execute_script("console.info('This is info')")
driver.execute_script("console.log('This is log')")
logs = driver.get_log("browser")
print(logs)
sleep(100000)
This prints all the 3 types of logs i am not sure if you are looking for this

Categories