Chrome crashes if I try to open it with Python/Selenium - python

When I run the code Chrome opens the URL but after about 2 seconds it crashes. It also says on the top of the chrome window "Chrome is being controlled by automated test software"
I am running the compatible version of the chrome driver for my version of chrome.
This is my code. How can I fix the crashing?
#from config import keys
from selenium import webdriver
def order():
driver = webdriver.Chrome('./chromedriver')
driver.get('https://www.youtube.com/')
if __name__ == '__main__':
order()

as you created the driver object in the scope of order()
after the execution of the order() is done all the local variables are removed.
You must have to declare driver as the global variable
from selenium import webdriver
# declare global varible driver
driver = None
def order():
driver = webdriver.Chrome('./chromedriver')
driver.get('https://www.youtube.com/')
if __name__ == '__main__':
order()
Otherwise, you can add time.sleep() to wait for a while
import time
from selenium import webdriver
def order():
driver = webdriver.Chrome('./chromedriver')
driver.get('https://www.youtube.com/')
# will wait for 5 seconds
time.sleep(5)
if __name__ == '__main__':
order()

Related

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

Chrome does not open URL in selenium

I have downloaded the latest python version(3.8.5) and google chrome version(84) and corresponding chromedriver version(84). But the browser does not respond when running the program.
from time import sleep
from selenium import webdriver
class Bot:
def __int__(self):
self.driver = webdriver.Chrome("C:\\Windows\\chromedriver.exe")
self.driver.get("www.google.com")
my_bot = Bot()
def main():
my_bot()
if __name__ == 'main':
main()
The above is my python code
from time import sleep
from selenium import webdriver
class Bot:
def __init__(self, url):
# path to chrome driver in your local machine
driver_path = "C:\\Windows\\chromedriver.exe"
# Loading the chrome driver
self.driver = webdriver.Chrome(driver_path)
# Using the above-loaded WebDriver to open the URL in the chrome
# browser
self.driver.get(url)
def main():
# Creating a object of Bot class with URL as an input parameter
# Don't forget to use https:// else your URL won't get open
my_bot = Bot(url="https://www.google.com")
# A sleep timer of 10 sec to let webdriver load the given URL and `
# display it to you for 10 sec
sleep(10)
# Closing the bot object which we created
# After 10 sec the bot object will be closed
my_bot.close()
if __name__ == '__main__':
# Calling the main function where the Bot object is created
main()
Its rather more easy in three lines!!
from selenium import webdriver
driver = webdriver.Chrome("G:\\Downloads\\chromedriver_win32\\chromedriver.exe")
driver.get('https://www.google.com/')
And you are done!

Close all browsers with selenium when using multithreads

I m doing web scraping with selenium. And i m using multithreads library. My script opens 3 firefox browsers at the same time and scraping. After finished to scraping, i want to close all browsers, i tried many way but Browser.quit() and browser.close() closing 1 browser, other 2 browser do not close.
def get_links():
some code here...
def get_driver():
global driver
driver = getattr(threadLocal, 'driver', None)
if driver is None:
chromeOptions = webdriver.ChromeOptions()
chromeOptions.add_argument("--headless")
driver = webdriver.Firefox(executable_path)
setattr(threadLocal, 'driver', driver)
return driver
def get_title(thisdict):
import datetime
driver = get_driver()
driver.get(thisdict["url"])
time.sleep(5)
driver.execute_script("window.scrollTo(0, document.body.scrollHeight)")
if __name__ == '__main__':
ThreadPool(3).map(get_title, get_links())
driver.close() #or driver.quit()
you have to use the self.selenium.stop() function. The quit()basically calls driver.dispose method which in turn closes all the browser windows. close() closes the browser window on which the focus is set.
I solved the problem with the code below. After Multithread finished all scraping, I m calling closeBrowsers function. And the function kills all open firefox browsers.
import os
def closeBrowsers():
os.system("taskkill /im firefox.exe /f")
if __name__ == '__main__':
ThreadPool(2).map(get_title, get_links())
closeBrowsers()

PhantomJS Selenium site does not load

I am trying to create an app which monitors a webpage using phantomjs and selenium but I have found an issue with a certain url as seen in the code.
from selenium import webdriver
SITE = "http://www.adidas.com/"
def main():
print("Building Driver")
driver = webdriver.PhantomJS()
driver.set_window_size(1024, 768)
print("Driver Created")
print("Navigating to: "+SITE)
driver.get(SITE)
print("Site loaded")
print("Saving Screenshot")
driver.save_screenshot("screen.png")
print("Fetching Current URL")
print(driver.current_url)
print("Exiting")
driver.quit()
if __name__ == '__main__':
main()
The program never gets past the line driver.get(SITE) How can I make it so that the website will load?
It appears that this is an error in PhantomJS. I would try using either the firefox or the chrome driver instead.
from selenium import webdriver
SITE = "http://www.adidas.de"
def main():
print("Building Driver")
browser = webdriver.Chrome(*path to chrome driver*)
print("Driver Created")
print("Navigating to: "+SITE)
browser.get(SITE)
print("Site loaded")
browser.quit()
if __name__ == '__main__':
main()
Creating a headless application would also be possible if that's what you wanted.

Opening multiple URLs with Selenium (Python)

Windows 10 x64 / Python 2.7 / Selenium
I am trying to build a tool to scrape my ticket queue for unassigned tickets, open them and look for keywords, then do other things. But for now, I can't seem to figure out getting the code to open more than the first URL. I keep getting a StaleElementReferenceException error; and I do not understand why.
I'm building on top of examples I have found. This may not even be a good way to go about this. I am open to a new direction as well.
The goal for this will be to have something scrape the queue every X and when certain keywords are found offer me a prompt to assign it. It needs to run on its own while I am doing other tasks so it can not interfere with my keystrokes.
from selenium import webdriver
import selenium.webdriver.support.ui as ui
from selenium.webdriver.common.keys import Keys
from time import sleep
from selenium.webdriver.support.ui import WebDriverWait
import unittest
class LoginTest (unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.get("https://TICKETS.COMPANY.COM/TEAM/MINE")
def test_Login(self):
driver = self.driver
table = "UNASSIGNED-TICKETS"
# Select the UNASSIGNED-TICKETS tab
selectUnassignedTab = WebDriverWait(driver, 10).until(lambda driver: driver.find_element_by_link_text('Unassigned'))
selectUnassignedTab.click()
# Grab all of the Ticket URLs
unlockedTickets = WebDriverWait(driver, 10).until(lambda driver: driver.find_elements_by_xpath("//table[#id='unlocked-tickets']/tbody/tr[#role='row']/td[#class='nowrap']/a[#href]"))
counter = 1
dictURLs={}
for ticket in unlockedTickets:
ticketUrl = ticket.get_attribute('href')
# Troubleshooting: Make sure URLs are grabbed.
print ticket.get_attribute('href')
# Stuff them in a dict
dictURLs["string{0}".format(counter)]=ticketUrl
# Open each ticket (NOT WORKING)
# driver.get(ticketUrl) <--- Causes the Stale Element Error
if counter == 1:
# Wait for the User and Pass fields to load. Then assign them.
emailFieldElement = WebDriverWait(driver, 10).until(lambda driver: driver.find_element_by_name('username'))
passFieldElement = WebDriverWait(driver, 10).until(lambda driver: driver.find_element_by_name('password'))
# Log in information.
emailFieldElement.clear()
emailFieldElement.send_keys("USERNAME-HERE")
passFieldElement.clear()
passFieldElement.send_keys("PASSWORD-HERE")
passFieldElement.submit()
counter = counter + 1
def tearDown(self):
sleep(15)
self.driver.quit()
if __name__ == '__main__':
unittest.main()
I used this code to open multiple windows and it worked like a charm
from selenium import webdriver
import threading
import time
def test_logic():
driver = webdriver.Firefox()
url = 'https://www.google.co.in'
driver.get(url)
# Implement your test logic
time.sleep(2)
driver.quit()
N = 5 # Number of browsers to spawn
thread_list = list()
# Start test
for i in range(N):
t = threading.Thread(name='Test {}'.format(i), target=test_logic)
t.start()
time.sleep(1)
print t.name + ' started!'
thread_list.append(t)
# Wait for all thre<ads to complete
for thread in thread_list:
thread.join()
print 'Test completed!'

Categories