I am testing a website and want to pass in the name of a document and then search for the id that has that name and then have the Selenium WebDriver click on that particular element to create a document. The problem is that there is a separate "Assessments.aspx" page inside of an iframe that handles the creation of the documents. The iframe does not have an id.
The elements that are used to create the documents are located in a <div>
inside of this iframe. My goal is to query the innerHTML inside of this iframe to find the matching document name passed into the method and then return that id to the WebDriver so it can click on the corresponding element in order to create the document.
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;
}
How do I find the iframe without the id? How do I return the result of a javascript function to a variable in C#?
You can pass Frame()
an element.
IWebElement iframe = driver.FindElement(By.Xpath("//iframe[@src='Assessments.aspx']"));
driver.SwitchTo().Frame(iframe);
As to the javascript portion, you need to return a value:
const string script1 = "return $(\"td:contains('Start of Car')\").attr('id'); $('#results').html(found);";
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.