I am creating a bot to register accounts on instagram and when I run my selenium script locally (Mac OS) , it works perfectly while when I run in on Ubuntu 18.4 on Azure Cloud , it fails localting selectors so practically I have tried everything from changing xpath to ID and so on.
from selenium import webdriver
from random import randint
import time
from selenium.webdriver.common.by import By
import accountInfoGenerator as account
from webdriver_manager.chrome import ChromeDriverManager
import random
import string
import os,sys
from selenium.webdriver.common.action_chains import ActionChains
from pyvirtualdisplay import Display
#!/usr/bin/env python
error = 'null'
##generator for random letters
def guess_letter():
return random.choice('abcdefghijklmnopqrstuvwxyz')
##generator for random letters
def guess_number():
return random.choice('1234567890')
#chooses PROXY AFTER EACH OTHER
def randomproxy():
file = open("./proxy.txt")
for line in file:
fields = line.split(";")
return random.choice(fields)
def insta():
print("We are now using this proxy:" + randomproxy())
while True:
chrome_option = webdriver.ChromeOptions()
chrome_option.add_argument('--proxy-server=%s' % randomproxy())
# #hrome_option.add_argument('--headless') ,service_args=['--verbose', '--log-path=/tmp/chromedriver.log']
display = Display(visible=0, size=(1024, 768))
display.start()
browser = webdriver.Chrome("./chromedriver",options=chrome_option)
action_chains = ActionChains(browser)
##checks if theres internet
def has_connection(browser):
try:
browser.find_element_by_xpath('//span[@jsselect="heading" and @jsvalues=".innerHTML:msg"]')
return False
except: return True
browser.get("http://www.instagram.com")
#if no internet then restart progras
if not has_connection(browser):
print('No Internet connection, aborted!')
browser.quit()
os.execv(sys.executable, ['python'] + sys.argv)
time.sleep(5) #time.sleep count can be changed depending on the Internet speed.
name = account.username()
#Fill the email value
email_field = browser.find_element_by_name('emailOrPhone')
action_chains.move_to_element(email_field)
email_field.send_keys(account.generatingEmail())
time.sleep(3)
print("We registered with email "+account.generatingEmail())
#Fill the fullname value
fullname_field = browser.find_element_by_name('fullName')
action_chains.move_to_element(fullname_field)
fullname_field.send_keys(account.generatingName())
time.sleep(3)
print("We registered with name" + account.generatingName())
#Fill username value
username_field = browser.find_element_by_name('username')
name2 = (name + guess_letter() + guess_number())
action_chains.move_to_element(username_field)
time.sleep(3)
username_field.send_keys(name2)
print("We registered using this username" + name2 )
#Fill password value
password_field = browser.find_element_by_name('password')
action_chains.move_to_element(password_field)
time.sleep(3)
password_field.send_keys('aa12345bb12345cc'+name) #You can determine another password here.
##clicks button and checks if button was clicked due to lags
time.sleep(6)
button = browser.find_element_by_xpath('//div[7]/div/button')
action_chains.move_to_element(button)
button.submit()
time.sleep(6)
age_button = browser.find_element_by_id('igCoreRadioButtonageRadioabove_18')
action_chains.move_to_element(age_button)
age_button.click()
time.sleep(3)
next = browser.find_element_by_xpath('/html/body/div[3]/div/div[3]/div/button')
next.click()
time.sleep(10)
print('Registering....')
##checks if error
def has_error(browser):
try:
browser.find_element_by_class_name('Ma93n')
return False
except: return True
#if error then restart
if not has_error(browser):
print('Error found! , aborted!')
browser.quit()
os.execv(sys.executable, ['python'] + sys.argv)
#if sucessfully created and connection is alive then save credentials
if has_error(browser):
f = open('../instabot/examples/secret.txt','a')
f.write( name + ':' + ('aa12345bb12345cc'+name) )
f.close()
print("Sucessfully saved")
time.sleep(10)
browser.close()
flag = True
while flag:
insta()
This is the output from ssh console
samih@app:~/instabotauto/autoaccount$ python botAccountCreate.py
We are now using this proxy:p.webshare.io:20192
We registered with email ytbygt@seznam.cz
We registered with nameBrandyn Ayana
We registered using this username.v.jkij2
Traceback (most recent call last):
File "botAccountCreate.py", line 129, in <module>
insta()
File "botAccountCreate.py", line 94, in insta
age_button = browser.find_element_by_id('igCoreRadioButtonageRadioabove_18')
File "/home/samih/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 360, in find_element_by_id
return self.find_element(by=By.ID, value=id_)
File "/home/samih/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 978, in find_element
'value': value})['value']
File "/home/samih/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/home/samih/.local/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element:{"method":"id","selector":"igCoreRadioButtonageRadioabove_18"}
(Session info: chrome=73.0.3683.86)
(Driver info: chromedriver=2.35.528139 (47ead77cb35ad2a9a83248b292151462a66cd881),platform=Linux 4.18.0-1014-azure x86_64)
What the program should is that it creates users and stores them in .txt file that is used by other program to log in to Instagram.
while on mac the console run without errors.
Checking for mac64 chromedriver:74.0.3729.6 in cache
Driver found in /Users/samihassan/.wdm/chromedriver/74.0.3729.6/mac64/chromedriver
We registered with email .skosr@seznam.cz
We registered with nameButali Zengin
We registered using this usernamefdzzicz2
Registering....
Checking for mac64 chromedriver:74.0.3729.6 in cache
Driver found in /Users/samihassan/.wdm/chromedriver/74.0.3729.6/mac64/chromedriver