Opening Instagram with Selenium webdriver Python - 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.

Related

How can I run my Selenium Headerless, in Python

I am having the worlds hardest time getting my Python Code to run headerless.. here's what I've done.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
That's included at the top of my script, then I've included this.
def login(self, username, password):
self.destroyDriver()
try:
print(("Logging into => ", username))
chrome_options = webdriver.ChromeOptions()
# chrome_options.add_argument('--proxy-server=%s' % PROXY)
chrome_options.add_argument('--window-size=1420,1080')
chrome_options.add_argument('--headless')
self.driver = webdriver.Chrome()
self.driver.implicitly_wait(10)
self.driver.get("https://example.com")
time.sleep(5)
self.driver.find_element_by_class_name("sign-in").click()
time.sleep(6)
self.driver.find_element_by_name("avatarname").send_keys(username)
self.driver.find_element_by_name("password").send_keys(password)
self.driver.find_element_by_class_name("btn-primary").click()
time.sleep(4)
if "welcome/login" in self.driver.current_url:
self.destroyDriver()
return False
except:
self.destroyDriver()
return 17
return True
However, this doesn't work at all the Chrome tab still pops up. I am trying to run headerless because when the tab is minimized on my desktop the Javascript does not load properly, and causes the entire script to break forcing me to keep the tab constantly open.
you have to attach your option into your driver
self.driver = webdriver.Chrome(options=chrome_options)

Webdriver.chrome not opening chrome

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')

there is a problem in get function cant read the URL don't know why?

enter image description hereit is a simple program to test chrome driver in selenium it run but can't get the URL
from selenium import webdriver
import os
class chromeTest:
def testMethod2(self):
driverLocation = "D:\\workspace_python\\chromedriver.exe"
os.environ["webdriver.chrome.driver"] = driverLocation
driver = webdriver.Chrome(driverLocation)
driver.get("http://www.letskodeit.com")
ff = chromeTest
ff.testMethod2(0)

How can I open up a mobile version of Google in Pycharm using Selenium?

I got some code together that lets me view the mobile version of Instagram, but whenever I try to put the code into a class and run the class, it fails. I have one Class for writing my methods, and another Class for calling the methods. When I run the second class, the chromedriver will open chrome, but it won't call the instagram URL.
this is the code that works
from selenium import webdriver
mobile_emulation = { "deviceName": "iPhone X" }
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option("mobileEmulation",
mobile_emulation)
driver = webdriver.Chrome(executable_path='/Users/~~/chromedriver', chrome_options=chrome_options)
driver.get("https://instagram.com/")
I tried making a class to hold the code like this
from selenium import webdriver
mobile_emulation = {"deviceName": "iPhone X"}
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option("mobileEmulation",
mobile_emulation)
driver = webdriver.Chrome(executable_path='/Users/~~/chromedriver',
chrome_options=chrome_options)
class IGFrame:
def __init__(self, driver):
self.driver = driver
def open_ig(self):
self.driver.get("https://instagram.com/")
time.sleep(2)
and then this was the class to call the method
from version1.pages.basic_methods import *
class TestSetUp:
def __init__(self, driver):
self.driver = driver
def test_mobileViewOfIG(self):
methods = IGFrame(self.driver)
methods.open_ig()
What I was aiming for was to have the driver setup and the method to call the URL in the IGFrame class, and then I would call the method from the TestSetUp class. What am I doing wrong and what should I do?

Selenium Test Repeat/Toggle

I have a test in Selenium IDE with Flow Control which submits a form and repeats the process with label start until I stop the app.
I have exported my code into python WebDriver.
Unfortunately my test only runs once.
How can I duplicate this process on python.
class Buy(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.base_url = "https://test.mydomain.com"
def test_buy(self):
driver = self.driver
driver.get(self.base_url + "/Form")
Select(driver.find_element_by_id("DropDownOne")).select_by_visible_text("BLUE")
Select(driver.find_element_by_id("DropDownTwo")).select_by_visible_text("LIGHT")
driver.find_element_by_id("Quantity").send_keys("10")
driver.find_element_by_id("Button").click()

Categories