51Testing软件测试论坛

标题: selenium怎样获取动态图片验证码..急 [打印本页]

作者: 喜欢猫的白菜    时间: 2012-4-20 11:00
标题: selenium怎样获取动态图片验证码..急
本帖最后由 愚人 于 2012-4-20 15:20 编辑

selenium怎样获取动态图片验证码?
在点开页面后,点用户名和密码后需要图片验证码?
我自动转换为C#后不知道要怎么写来验证验证码了
源码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Selenium;
using System.Threading;

namespace TestFor2012420
{
    public partial class Form1 : Form
    {

        private ISelenium selenium;
        private StringBuilder verificationErrors;
        
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://mydiy.pconline.com.cn/");
            selenium.Start();
            verificationErrors = new StringBuilder();
            selenium.Open("/");
            selenium.Click("xpath=(//a[contains(text(),'选用')])[6]");
            selenium.Click("//span[@type='1']");
            selenium.Type("xpath=(//input[@name='product_query'])[2]", "华硕");
            selenium.Click("xpath=(//input[@name='search'])[2]");
            Thread.Sleep(3000);
            selenium.Click("link=下一页");
            Thread.Sleep(3000);
            selenium.Click("xpath=(//a[contains(text(),'选用')])[2]");
            Thread.Sleep(3000);
            selenium.Click("//span[@type='2']");
            Thread.Sleep(3000);
            selenium.Click("xpath=(//a[contains(text(),'选用')])[3]");
            Thread.Sleep(3000);
            selenium.Type("id=num_2", "2");
            Thread.Sleep(3000);
            selenium.Click("//span[@type='3']");
            Thread.Sleep(3000);
            selenium.Click("link=2");
            Thread.Sleep(3000);
            selenium.Click("link=3");
            Thread.Sleep(3000);
            selenium.Click("link=选用");
            Thread.Sleep(3000);
            selenium.Click("//span[@type='4']");
            Thread.Sleep(3000);
            selenium.Click("link=下一页");
            Thread.Sleep(3000);
            selenium.Click("xpath=(//a[contains(text(),'选用')])[5]");
            Thread.Sleep(3000);
            selenium.Click("//span[@type='5']");
            Thread.Sleep(3000);
            selenium.Click("link=2");
            Thread.Sleep(3000);
            selenium.Click("xpath=(//a[contains(text(),'选用')])[5]");
            Thread.Sleep(3000);
            selenium.Click("//span[@type='6']");
            Thread.Sleep(3000);
            selenium.Click("xpath=(//a[contains(text(),'选用')])[7]");
            Thread.Sleep(3000);
            selenium.Click("//span[@type='7']");
            Thread.Sleep(3000);
            selenium.Click("xpath=(//a[contains(text(),'选用')])[7]");
            Thread.Sleep(3000);
            selenium.Click("//span[@type='8']");
            Thread.Sleep(3000);
            selenium.Click("link=下一页");
            Thread.Sleep(5000);
            selenium.Click("link=下一页");
            Thread.Sleep(3000);
            selenium.Click("xpath=(//a[contains(text(),'选用')])[4]");
            Thread.Sleep(3000);
            selenium.Click("//span[@type='9']");
            Thread.Sleep(3000);
            selenium.Click("xpath=(//a[contains(text(),'选用')])[4]");
            Thread.Sleep(3000);
            selenium.Click("//span[@type='10']");
            Thread.Sleep(3000);
            selenium.Click("link=下一页");
            Thread.Sleep(3000);
            selenium.Click("link=下一页");
            Thread.Sleep(3000);
            selenium.Click("xpath=(//a[contains(text(),'选用')])[4]");
            Thread.Sleep(3000);
            selenium.Click("//span[@type='11']");
            Thread.Sleep(3000);
            selenium.Click("link=下一页");
            Thread.Sleep(3000);
            selenium.Click("link=下一页");
            Thread.Sleep(3000);
            selenium.Click("xpath=(//a[contains(text(),'选用')])[4]");
            Thread.Sleep(3000);
            selenium.Click("//span[@type='12']");
            Thread.Sleep(3000);
            selenium.Click("//span[@type='15']");
            Thread.Sleep(3000);
            selenium.Click("xpath=(//a[contains(text(),'选用')])[2]");
            Thread.Sleep(3000);
            selenium.Select("name=DiyType", "label=专业设计方案");
            Thread.Sleep(3000);
            selenium.Type("id=DiyTitle", "测试");
            Thread.Sleep(3000);
            selenium.Type("id=DiyComment", "此程序用于测试");
        }


    }

}
该怎么解决呢?说详细一点。。谢谢大家了
作者: 愚人    时间: 2012-4-20 15:21
求助贴子请不要设置成付费主题,谢谢
作者: ryugun    时间: 2012-4-20 16:51
验证码问题。。。
最简单的办法就是和开发商量,取消验证码,或做个万能验证码。。

至于别的方法。。同等大神支招。。
另外。。几乎每执行一句代码,等3秒。。。这效率是不是该考虑下了。。
作者: 六月天    时间: 2012-4-20 17:40
用了太多的clickandwait了……
一般是要求开发做个api接口,你可以直接调用,获取当前的验证码。要不然就让开发直接把验证码注释掉。这些事情应该在需求分析阶段就作为可测性需求向开发提出来,现在才注意这个问题,被动了不是。
作者: 楠族开心果    时间: 2012-4-20 18:08
回复 2# 愚人


    估计又一个认为是给别人钱的 哈哈
作者: 喜欢猫的白菜    时间: 2012-4-22 11:20
回复 3# ryugun


每一句加上等待是必须得。你要考虑网页加载时间,如果网页加载没有完毕的话。会出问题的。不信你可以试试
作者: 喜欢猫的白菜    时间: 2012-4-22 11:21
回复 4# 六月天


    确实很被动
作者: dian106106    时间: 2012-5-25 23:04
没有万能的验证码, 知道开发的code也不错, 回调了. 如果是第三方的, 那估计没什么辄了
作者: ryugun    时间: 2012-5-26 17:50
回复 6# 喜欢猫的白菜
等待页面加载是必须的。。但目前你这种写法很不好。。
我记得selenium里封装有对应的api,名称忘记了,也可以自己简单写一个函数

点击某个按钮,检查需要出现的东西是否出现,如果没出现再等待1(n)秒。。然后循环再检查,再等待。。。直到超出我们预置的时间范围。。。

这样的方式会好的多。。
作者: bob123654    时间: 2012-12-14 10:59
楼主好牛X 求助帖还要收费?桑不起啊
作者: fengerapple    时间: 2013-1-25 17:52
Thank you very much for sharing!The good man!The good life of peace!
作者: 猫星人    时间: 2015-4-2 19:14
不可能啊,要停用吧




欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2