Webdriver.chrome not opening chrome - python

I'm working on an automation script and I'm trying to open a url in chrome, I have installed and imported selenium and downloaded the chrome driver and moved it to /usr/local/bin.
But when I try and run the script, the console is blank and then about a second later it displays 'Process finished with exit code 0' as if nothing happened. Below is my current code:
from selenium import webdriver
class Script():
def __init__(self):
self.driver = webdriver.Chrome(executable_path=r'/usr/local/bin/chromedriver')
def login(self):
self.driver.get('https://facebook.com')

The path is wrong here. It should be like below
webdriver.Chrome(executable_path=r'/usr/local/bin/chromedriver.exe')
Need to add .exe extension in the executable_path.
Also, make sure you are using correct Version of chrome driver for Google Chrome.

You are on linux system. You don't need the raw i.e. r switch. Your effective line of code will be:
self.driver = webdriver.Chrome(executable_path='/usr/local/bin/chromedriver')

Does this work?
from config import keys
from selenium import webdriver
def order():
driver = webdriver.Chrome(executable_path='/usr/local/bin/chromedriver')
driver.get('https://facebook.com')
print("here")
if __name__== '__main__':
order()
An exit code 0 means that ran without error. If an error occurs it would provide a non-zero argument. I would add a
from selenium import webdriver
class Script():
def __init__(self):
self.driver = webdriver.Chrome(executable_path='/usr/local/bin/chromedriver')
def login(self):
self.driver.get('https://facebook.com')
print ('Opened facebook')
This should return with "Opened facebook" then 'Process finished with exit code 0'. I built something similar that will log a user into Facebook.
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
class Script():
def __init__(self):
self.driver = webdriver.Chrome(executable_path='/usr/local/bin/chromedriver')
def login(self):
self.driver.get('https://facebook.com')
print ('Opened facebook')
self.driver.implicitly_wait(30)
self.driver.get(k['product_url'])
print ('Opened facebook')
username_box = self.driver.find_element_by_id('email')
username_box.send_keys('EMAIL ADDRESS')
print ('Email Id entered')
password_box = self.driver.find_element_by_id('pass')
password_box.send_keys('password')
print ('Password entered')
login_box = self.driver.find_element_by_id('loginbutton')
login_box.click()
print('Logged In')

Related

Facing this error in Pytest on selenium python

import time
import self as self
from pytest import mark
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from setuptools import setup
#mark.execute
class First_Tests:
def test_first(self, setup):
driver = setup['driver']
browser = setup['browser']
driver.get("https://shuftipro.com/")
driver.maximize_window()
def header_test(self, setup):
driver = setup['driver']
# Click on solution in header
solution = driver.find_element(By.ID, "menu-item-72751")
solution.click()
if driver.current_url == "https://shuftipro.com/solutions/":
print("land on solution page.")
else:
print("land on wrong page.")
obj = First_Tests()
obj.test_first(self, setup)
obj.header_test(self, setup)
If I remove the "self" from parameter and run the program it showing me error that, test_first() takes 1 positional arguments but 3 were given
one of easier way would be to use webdriver from selenium
driver = webdriver.Chrome()
and remove
driver = setup['driver']
browser = setup['browser']
and then get rid of setup as parameter.
The final code will look something like this:
from pytest import mark
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
#mark.execute
class First_Tests:
def test_first(self):
driver.get("https://shuftipro.com/")
driver.maximize_window()
def header_test(self):
# Click on solution in header
solution = driver.find_element(By.ID, "menu-item-72751")
solution.click()
if driver.current_url == "https://shuftipro.com/solutions/":
print("land on solution page.")
else:
print("land on wrong page.")
obj = First_Tests()
obj.test_first()
obj.header_test()

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!

Opening Instagram with Selenium webdriver Python

I want to log in to Instagram with selenium webdriber. I wrote a code but It always opens just google chrome page, without going to Instagram page. I have tried to change to .sleep() time but It is always the same.
from selenium import webdriver
import time
class InstagramBot:
def __init__(self, username, password):
self.username = username
self.password = password
self.driver = webdriver.Chrome(executable_path = "C:\Program Files (x86)\xxx\xxx\xxx\chrome.exe")
def closeBrowser(self):
self.driver.close()
def login(self):
driver = self.driver
driver.get("https://www.instagram.com/")
time.sleep(5)
IGBot = InstagramBot("xxx", "yyy")
IGBot.login()
Also I have tried with \chrome.exe and with \chrome, and with "www.instagram.com".
After 5 seconds, I get this error:
selenium.common.exceptions.WebDriverException: Message: Service C:\Program Files (x86)\xxx\xxx\xxxx\chrome.exe unexpectedly exited. Status code was: 0
This line
self.driver = webdriver.Chrome(executable_path = "C:\Program Files (x86)\xxx\xxx\xxx\chrome.exe")
should be
self.driver = webdriver.Chrome(executable_path = "C:\complete\path\to\chromedriver.exe")
Note not to confuse "chrome.exe" with "chromedriver.exe" in Selenium context.

Python class to open and close website using Selenium

Trying to setup Chrome for the open / close a website. Now i can open it, But failed to close it.
Can anyone tell me why? Many thanks!
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
class chromeSetup():
def __init__(self):
self.chrome_path = r'C:\XXXXX\chromedriver.exe'
def searchWeb(self, url="https://www.google.com.hk/"):
driver = webdriver.Chrome(self.chrome_path)
driver.get(url)
def close(self):
self.driver.close()
You are not making driver an instance attribute. Change searchWeb method like this:
def searchWeb(self, url="https://www.google.com.hk/"):
self.driver = webdriver.Chrome(self.chrome_path)
self.driver.get(url)

selenium script not working anymore

I used to have a selenium script code in Python for a firefox website that worked fine.After a while I updated Firefox (48) and selenium 2.9.1.1, python is 3.5.0
the code,
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re, os
class Jqueryx(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.base_url = "http://127.0.0.1:8080/"
self.verificationErrors = []
self.accept_next_alert = True
self.path_upload_quest = r'C:\\Users\jl\Documents\DONNEES\DONNEES_LIMONDE'
self.path_upload_ref = r'C:\\Users\jl\Documents\DONNEES\DONNEES_LIMONDE\ref'
def test_jqueryx(self):
driver = self.driver
driver.get(self.base_url + "/lim/home.php")
If I run the script now i ve got this message:
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' >executable needs to be in PATH.
So I download this geckodriver thing and try to add it to the python but nothing works yet,
I try to add this in the script;
os.environ["PATH"] += r'C:\Users\jl\geckodriver'
without success or adding a .pth file in the site-package folder but no change too...
What can I do to get this script back on track ?
thx
Copy the GeckoDriver to the folder where you have your code, and then change the following in your code
self.driver = webdriver.Firefox("path/to/your/current/folder")
This should work fine
you can do one of the following:
first method:
place the driver in the PATH environment variable and than run the script
second method (Add to path during the test):
add the driver during the test by running
os.environ["PATH"] += r'/path/to/dir/where/your/driver/is'
in your case:
os.environ["PATH"] += r'C:\Users\jl'
this code must be executed before calling
browser = webdriver.Firefox()
third method (full path to driver):
browser = webdriver.Firefox(executable_path=r'/full/path/to/driver')
in your case:
browser = webdriver.Firefox(executable_path=r'C:\Users\jl\geckodriver')

Categories