Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot press button

I'm trying to code a bot for a game, and need some help to do it. Being a complete noob, I googled how to do it with python and started reading a bit about mechanize.

<div class="clearfix">
                    <a href="#" onclick="return Index.submit_login('server_br73');">
            <span class="world_button_active">Mundo 73</span>
        </a>
                    </div>

My problem is in logging in, where i have this raw code for now:

import requests
import requesocks
import xlrd
import socks
import socket
import mechanize
import selenium
from bs4 import BeautifulSoup

# EXCEL
file_location = "/home/luis/Dropbox/Projetos/TW/multisbr.xlsx"
wb = xlrd.open_workbook(file_location)
sheetname = wb.sheet_names ()
sh1 = wb.sheet_by_index(0)

def nickNm():
    lista = [sh1.col_values(0, x) for x in range (sh1.ncols)]
    listaNomes = lista [1]
    x < 1
    print listaNomes
def passwd():
    lista = [sh1.col_values(1, x) for x in range (sh1.ncols)]
    listaPasswd = lista [1]
    x < 1
    print listaPasswd

# TOR

def create_connection(address, timeout=None, source_address=None):
    sock = socks.socksocket()
    sock.connect(address)
    return sock

socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050)

# patch the socket module
socket.socket = socks.socksocket
socket.create_connection = create_connection

#BeautifulSoup

def get_source ():
    url = 'https://www.tribalwars.com.br'
    source_code = requests.get(url)
    plain_text = source_code.text
    soup = BeautifulSoup(plain_text, 'lxml')

# ALFA
br = mechanize.Browser ()
twbr = 'https://www.tribalwars.com.br/index.php'

def alfa ():
    br.open(link)

    br.select_form(nr=0)
    br["user"] = "something"
    br["password"] = "pword"

    result = br.submit()

    br.geturl()


nickNm()
passwd()
alfa()
like image 636
Luis Silva Avatar asked Dec 19 '15 16:12

Luis Silva


1 Answers

There is quite a lot of javascript involved when you perform different actions on a page, mechanize is not a browser and cannot execute javascript. One option to make your life easier here would be to automate a real browser. Here is an example code to log into the tribalwars using selenium and a headless PhantomJS:

from selenium import webdriver

driver = webdriver.PhantomJS()
driver.get("https://www.tribalwars.com.br/index.php")

# logging in
driver.find_element_by_id("user").send_keys("user")
driver.find_element_by_id("password").send_keys("password")
driver.find_element_by_css_selector("a.login_button").click()
like image 187
alecxe Avatar answered Nov 05 '22 11:11

alecxe