簡體   English   中英

如何使用Selenium WebDriver和C#在innerHTML中查詢iFrame內的元素?

[英]How do I query innerHTML for elements that are inside of an iFrame using Selenium WebDriver and C#?

我正在測試一個網站,並希望傳遞文檔名稱,然后搜索具有該名稱的ID,然后讓Selenium WebDriver單擊該特定元素以創建文檔。 問題在於,在iframe中有一個單獨的“ Assessments.aspx”頁面可以處理文檔的創建。 iframe沒有ID。

在此處輸入圖片說明

用於創建文檔的元素位於此iframe的<div>中。 我的目標是查詢此iframe內的innerHTML,以找到傳遞到方法中的匹配文檔名稱,然后將該ID返回給WebDriver,以便它可以單擊相應的元素以創建文檔。

在此處輸入圖片說明

internal bool CreateDocument(IWebDriver driver, string documentName, ref DataObject masterData)
    {
        try
        {
            var js = driver as IJavaScriptExecutor;
            if (js == null) return false;

            // verify that we are on the documents page
            if (driver.PageSource.Contains("<div id=\"tabManagementFullPage\">")) Console.WriteLine("We are on the documents page...");

            // get inside the iframe
            driver.FindElement(By.Id("//iframe[@src='Assessments.aspx']"));
            driver.SwitchTo().Frame(0);

            // use jquery to find the document then return it to the webdriver and click the id of the desired document
            const string script1 = "var found = $(\"td:contains('Start of Car')\").attr('id'); $('#results').html(found);";
            var element = (string)js.ExecuteScript(script1);
            var x = driver.FindElement(By.Id(element));
            x.Click();
            return true;
        }
        catch (Exception exception)
        {
            masterData.Logger.Log(Loglevel.Error, "Boom:{0}", exception.Message);
        }
        return false;
    }

如何找到不帶ID的iframe? 如何將javascript函數的結果返回到C#中的變量?

您可以傳遞Frame()元素。

IWebElement iframe = driver.FindElement(By.Xpath("//iframe[@src='Assessments.aspx']"));
driver.SwitchTo().Frame(iframe);

至於javascript部分,您需要返回一個值:

const string script1 = "return $(\"td:contains('Start of Car')\").attr('id'); $('#results').html(found);";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM