简体   繁体   English

Selenium C#找不到ID或标题

[英]Selenium C# Can't find ID or Title

I manage to open a firefox browser, go to http://www.google.com/ search for "Bath Fitter". 我设法打开了Firefox浏览器,请转到http://www.google.com/搜索“ Bath Fitter”。 When i see a bunch of links, i want to in fact click on an item of the top menu provided by Google, Images. 当我看到一堆链接时,实际上我想单击Google图片提供的顶部菜单中的一个项目。 Images is located next to Map Videos News... How can i have it click on Images? 图片位于“地图视频新闻”旁边。我如何单击图片?

Below is my code: 下面是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;   
using OpenQA.Selenium.Firefox;

namespace SeleniumHelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            IWebDriver driver = null;
            try
            {
                driver = new FirefoxDriver();
                driver.Navigate().GoToUrl("http://www.google.com/");
                driver.Manage().Window.Maximize();

                IWebElement searchInput = driver.FindElement(By.Id("gbqfq"));
                searchInput.SendKeys("Bath Fitter");
                searchInput.SendKeys(Keys.Enter);

                searchInput.FindElement(By.Name("Images"));
                searchInput.Click();
                driver.Close();

            }

            catch (Exception e)
            {
                Console.WriteLine("Exception ****" + e.ToString());

            }
        }
    }
}

More specifically you can also write your selector pointing from Top Navigation. 更具体地说,您还可以编写指向“顶部导航”的选择器。 This is the XPath. 这是XPath。

.//*[@id='hdtb_msb']//a[.='Images']

try this; 尝试这个;

driver.FindElement(By.XPath(".//*[@id='hdtb_msb']//a[.='Images']"));

EDIT: Even though the selectors above were correct your code was not working because of the second page was taking too long to load. 编辑:即使上面的选择器正确,您的代码也无法正常工作,因为第二页的加载时间太长。 There you need to wait for the the element to be in ready state and an implicit wait is needed. 在那里,您需要等待元素处于就绪状态,并且需要隐式等待。 Change the code in your try block and replace with mine and try 更改try块中的代码,并替换为我的try

driver = new FirefoxDriver();
driver.Navigate().GoToUrl("http://www.google.com/");
driver.Manage().Window.Maximize();

IWebElement searchInput = driver.FindElement(By.Id("gbqfq"));
searchInput.SendKeys("Bath Fitter");
searchInput.SendKeys(Keys.Enter);

//this is the magic
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
By byImage = By.XPath(".//*[@id='top_nav']//a[.='Images']");
IWebElement imagElement =
                    new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementExists(byImage));
imagElement.Click();

Try something like this... 试试这样的东西...

IList<IWebElement> links = driver.FindElements(By.TagName("a"));
links.First(element => element.Text == "Images").Click();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM