【发布时间】:2015-11-21 19:47:57
【问题描述】:
我用 selenium 代码编写了一个 python 来解决new behaviour captcha。但是在完全模仿用户行为方面缺乏一些东西:代码可以定位并点击验证码,但在此之后谷歌设置了额外的图片检查
这不容易自动化。如何改进代码以在不检查图片的情况下立即解决验证码(让 google 没有提示机器人存在)?
Python 代码
from time import sleep
from random import uniform
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions as EC
# to imitate hovering
def hover(element):
hov = ActionChains(driver).move_to_element(element)
hov.perform()
# optional: adding www.hola.org proxy profile to FF (extention is installed on FF, Win 8)
ffprofile = webdriver.FirefoxProfile()
hola_file = '/Users/Igor/AppData/Roaming/Mozilla/Firefox/Profiles/7kcqxxyd.default-1429005850374/extensions/hola/hola_firefox_ext_1.9.354_www.xpi'
ffprofile.add_extension(hola_file)
# end of the optional part
driver = webdriver.Firefox(ffprofile)
url='http://tarex.ru/testdir/recaptcha/recaptcha.php'
# open new tab, also optional
driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 't')
driver.get(url)
recaptchaFrame = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.TAG_NAME ,'iframe'))
)
frameName = recaptchaFrame.get_attribute('name')
# move the driver to the iFrame...
driver.switch_to_frame(frameName)
# ************* locate CheckBox **************
CheckBox = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID ,"recaptcha-anchor"))
)
# ************* hover CheckBox ***************
rand=uniform(1.0, 1.5)
print('\n\r explicit wait for ', rand , ' seconds...')
sleep(rand)
hover(CheckBox)
# ************* click CheckBox ***************
rand=uniform(0.5, 0.7)
print('\n\r explicit wait for ', rand , 'seconds...')
sleep(rand)
# making click on CheckBox...
clickReturn= CheckBox.click()
print('\n\r after click on CheckBox... \n\r CheckBox click result: ' , clickReturn)
【问题讨论】:
标签: python selenium captcha recaptcha