【问题标题】:Selenium switching to new URL / Can't find elements on new URLredirectSelenium 切换到新 URL/在新 URLredirect 上找不到元素
【发布时间】:2017-05-04 17:58:30
【问题描述】:

我是自动化方面的新手,我需要逐步在列表中找到人,然后转到他的个人资料并查看有关他的一些信息。当我在我的第一个 URL 上工作时,将密钥发送到搜索字段,找到我需要的人并单击完美工作。但是,当我打开家伙个人资料时,我无法使用 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();
      }
    }
  }
}

【问题讨论】:

  • 新网址仍在同一窗口中

标签: c# selenium selenium-chromedriver


【解决方案1】:

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

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

如果是这个问题,看之前的帖子How do I interact with a popup window with Mink, Selenium 2, and Behat?

【讨论】:

  • 新网址仍在同一窗口中
  • 唯一想到的其他想法是您的 selenium 脚本试图在页面完成加载之前在页面上查找元素。一个令人讨厌的解决方法是在重定向后告诉您的脚本休眠(xx),但您实际上可以编写一个等待响应的函数。希望您能找到解决方案。
猜你喜欢
  • 1970-01-01
  • 2014-09-26
  • 1970-01-01
  • 1970-01-01
  • 2020-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多