Selenium:无法搜索框

时间:2014-07-14 20:55:52

标签: c# selenium selenium-webdriver selenium-ide qa

我正在尝试访问某个网站,点击“与我们联系”。然后在“询问购物者”中搜索一个术语!'搜索框。我尝试在Selenium IDE中重新创建测试但是在录制时它没有捕获搜索。我也不确定如何访问错误日志以诊断出现了什么问题。到目前为止,我有:

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

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

namespace TestApplications
{
    class Program
    {
        static void Main(string[] args)
        {
            IWebDriver driver = new ChromeDriver(@"C:\Library");
            driver.Url = "http://www1.shoppersdrugmart.ca/en/Home";

            var findContact = driver.FindElement(By.CssSelector("#Header > section.hdr-bar > div > div > menu > ul > li:nth-child(2) > a"));
            findContact.Click();

            driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));

            var findAsk = driver.FindElement(By.CssSelector("#response"));
            findAsk.SendKeys("text");

        } 
    }
}

1 个答案:

答案 0 :(得分:1)

因为在该页面中有一个iframe具有相同的ID response,而您的目标元素在此框架内具有相同的ID。

<iframe id="response" height="70" marginheight="5" src="http://shoppers.mangomoon.ca/interact/customer_interface/question_form_sdm.jsp" frameborder="0" width="470" name="response" marginwidth="5"></iframe>

因此,您需要先切换到相框。

IWebDriver driver = new ChromeDriver(@"C:\Library");
driver.Url = "http://www1.shoppersdrugmart.ca/en/Home";

var findContact = driver.FindElement(By.CssSelector("#Header > section.hdr-bar > div > div > menu > ul > li:nth-child(2) > a"));
findContact.Click();

driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));

driver.SwitchTo().Frame("response"); // add this for frame switching

var findAsk = driver.FindElement(By.CssSelector("#response"));
findAsk.SendKeys("text");