无法按下按钮

时间:2015-12-19 16:36:50

标签: python automation mechanize mechanize-python

我试图为游戏编写机器人代码,并需要一些帮助来完成它。作为一个完整的菜鸟,我用谷歌搜索了如何用python进行操作并开始阅读有关机械化的内容。

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

我的问题是登录,我现在有这个原始代码:

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

2 个答案:

答案 0 :(得分:11)

当您对网页mechanize is not a browser and cannot execute javascript执行不同的操作时,会涉及大量的JavaScript。让您的生活更轻松的一个选择是自动化真正的浏览器。以下是使用selenium和无头tribalwars登录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()

答案 1 :(得分:0)

如果这是基于浏览器的尝试,请从firefox的iMacros插件运行自动化