Selenium切换到新URL /无法在新的URLredirect上找到元素

时间:2016-12-19 16:10:52

标签: c# selenium selenium-chromedriver

我是自动化的新手,我需要逐步找到列表中的人,然后转到他的个人资料并查看有关他的一些信息。当我在我的第一个URL上发送密钥到搜索字段时,找到我需要的人并点击工作完美。但是,当我打开man配置文件时,我无法使用selenium对此页面上的任何元素进行处理,获得此异常,这意味着他无法找到此类元素。但是,当我使用Chrome控制台和jQuery时,它可以很好地找到我的元素并使用它进行操作。

> (An unhandled exception of type
> 'OpenQA.Selenium.NoSuchElementException' occurred in WebDriver.dll
> Additional information: no such element: Unable to locate element:
> {"method":"id","selector":"contentIFrame1"})

我仍然认为webdriver正在使用我的第一页,并提出建议。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System.Windows.Forms;
using System.Threading;
using OpenQA.Selenium.Support.UI;

namespace RLC_AccCheck_1
{
  class Program
  {
    static void Main(string[] args)
    {
      using (IWebDriver driver = new ChromeDriver("c:\\Work\\Selenium"))
      {
        driver.Navigate().GoToUrl("URL")
        driver.Manage().Window.Maximize();

        Thread.Sleep(2000);

        //Catherine Burke
        //Console.Write("name=");
        string name = "Catherine Burke";//Console.ReadLine();

        IWebElement frame = driver.FindElement(By.Id("contentIFrame0"));

        IWebDriver driverFrame = driver.SwitchTo().Frame(frame);

        IWebElement search = driverFrame.FindElement(By.Id("crmGrid_findCriteria"));

        search.SendKeys(name);

        IWebElement searchImg = driverFrame.FindElement(By.Id("crmGrid_findCriteriaImg"));
        searchImg.Click();

        IWebElement row;
        try
        {
          row = driverFrame.FindElement(By.CssSelector(".ms-crm-List-Data .ms-crm-List-Row:nth-child(1)"));
        }
        catch(NoSuchElementException)
        {
          Console.WriteLine(name + " is not found");
          Console.ReadKey();
          return;
        }

        IWebElement record = row.FindElement(By.CssSelector(".ms-crm-List-DataCell:nth-child(3) a"));
        if (record.Text == name)
        {
          record.Click();
          //record.GetAttribute("href")
          Console.WriteLine(name + " is found");
        }
        else
        {
          Console.WriteLine(name + " is not found");
        }

        Thread.Sleep(3000);

        String currentURL = driver.Url;
        Console.WriteLine(currentURL + " is current URL ");

        var windowHandles = driver.WindowHandles;

        //This frame and all id/class writing below tags is not located

        IWebElement frame1 = driver.FindElement(By.Id("contentIFrame1"));
        IWebDriver driverFrame1 = driver.SwitchTo().Frame(frame1);

        Thread.Sleep(1000);

        IWebElement image = driver.FindElement(By.Id("Fax_label"));
        image.Click();

        //driver.SwitchTo().Frame(0);

        IWebElement test = driver.FindElement(By.Id("FormSecNavigationControl-Icon"));
        test.Click();

        IWebElement recordType = driver.FindElement(By.CssSelector("#ddsm_recordtype .multiSel")); 
        Console.WriteLine(record.Text);

        Console.ReadKey();
      }
    }
  }
}

1 个答案:

答案 0 :(得分:0)

您的用户个人资料页面是否使用了jQuery窗口?

可能是您的webDriver专注于与包含您的元素的窗口不同的窗口。

如果出现此问题,请参阅上一篇文章How do I interact with a popup window with Mink, Selenium 2, and Behat?