尝试点击链接

时间:2017-03-26 14:30:02

标签: c# visual-studio selenium-webdriver

我正在尝试在C#中创建一个简单的自动化脚本,它将加载谷歌主页,然后在搜索框中键入内容,列出结果,然后单击“图像”链接并显示搜索项目的可能图像。

当Selenium Webdriver能够通过LinkText定位Images链接时,我能够达到这一点,但是当我想执行Click()操作时,我得到错误消息Exception抛出:WebDriver中的'OpenQA.Selenium.ElementNotVisibleException' .dll,我无法继续前进。

我将代码放在下面。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;

namespace WebDriverDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            IWebDriver driver = new FirefoxDriver();
            driver.Url = "http://www.google.com";

           var searchBox = driver.FindElement(By.Id("lst-ib"));
           searchBox.SendKeys("apple");
           searchBox.SendKeys(Keys.Enter);

           driver.Manage().Timeouts().ImplicitWait = (TimeSpan.FromSeconds(10));


            try
            {
                var imageLink = driver.FindElement(By.LinkText("Images"));
                Console.Write("Element found by a LinkText");
                imageLink.Click();
            }
            catch(Exception e)
            {
                Console.WriteLine(e);
            }
            Console.Read();

        }
    }
}

我期待您的反馈和可能的解决方案:)

致以最诚挚的问候,谢谢,

3 个答案:

答案 0 :(得分:0)

在搜索等待图片链接可点击后,您只需要暂停一下。

Driver.FindElement(By.Id("lst-ib")).SendKeys("apple\n");
new WebDriverWait(Driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementToBeClickable(By.LinkText("Images"))).Click();

此外,您可以发送\n,这是一个回车符,作为字符串的一部分。

答案 1 :(得分:0)

很抱歉很长时间,但我能够自己解决问题。而不是driver.FindElement(By.LinkText(“Images”))我使用了Xpath,向我展示了图片链接的确切位置:)

感谢您的支持!

答案 2 :(得分:-1)

我不知道为什么会这样:| 在尝试了不同的“等待类型”之后,只有1个似乎正在起作用:

Thread.sleep代码(2000);

希望现在有所帮助。我仍在寻找更好的解决方法。

相关问题