无法使用selenium C#在网页中找到元素?

时间:2016-02-23 10:38:08

标签: c# selenium iframe

无法找到页面中的任何元素有2个iframe嵌套在selenium中吗?

首次启动时,我会在浏览器中找到所有元素但是何时转到iframe [0]我无法在浏览器中找到任何元素。

我的代码如下:

var iframes = browser.FindElementsByXPathName("//iframe[contains(@id,'container_page')]");
while (browser.FindElementsByXPathName("//span[contains(@class, 'button_Love')]").Count == 0 && iframes.Count == 0)
    Thread.Sleep(1000);
if (iframes.Count > 0)
{
    browser.GoToFrame(iframes[0]);
    Thread.Sleep(2000);
}

1 个答案:

答案 0 :(得分:0)

尝试以下代码。

   var driver = new FirefoxDriver();
        driver.Navigate().GoToUrl("you URL");
        WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(60));
        //check if there is any iframe then get that frame and switch on frame using folloiwng code.
        var frame = wait.Until(ExpectedConditions.ElementExists(By.Id("frame_id")));
        driver.SwitchTo().Frame(frame);
        var elementbutton_Love = driver.FindElements(By.ClassName("button_Love"));

        //after doing all work you can go to default content using  following code
        driver.SwitchTo().DefaultContent();

如果有任何问题,请告诉我。