selenium - trying to populate 1st iframe on page but finds 2nd - c#

I am trying to populate first iframe on my page. It has 2 iframes on it. I am using the following code but it keeps populating the 2nd iframe for reasons unknown to me.
List<IWebElement> iframeList = Client.WebDriver.FindElementsByXPath(".//iframe");
IWebDriver overDrive = Client.WebDriver.Driver.SwitchTo().Frame(iframeList[0]);
IWebElement bodyLogin = overDrive.FindElement(By.TagName(("body")));
bodyLogin.SendKeys("My Text Message");

Related

Cannot Find Element in Selenium C#

I am trying to locate a dropdown menu button from the following website:
Inspect Element snippet of 'CRO Dashboard DropDown Menu'
My aim is to locate the dropdown button element and click it to open the menu to then continue other operations on it.
The following error occurs when trying to locate the dropdown menu button by id:
OpenQA.Selenium.NoSuchElementException: 'no such element: Unable to locate element: {"method":"css selector","selector":"#dashboardSelectorLink"}
Referring to the image, from the highlighted part I tried to use various locators like finding element by id, css selector, class names, and even xpath but still the program could not locate it so that I can click the element.
Here is some of the code I tried:
dropdown = FindElementByCSS(dropdown, ".ms-crm-ImageStrip-Dropdown_Arrow");
dropdown = FindElementByXPath(dropdown, "//a[#id='dashboardSelectorLink']/span[2]/img");
dropdown = driver.FindElement(By.CssSelector("a[id=\"dashboardSelectorLink\"]"));
dropdown = FindElementByID(dropdown, "dashboardSelectorLink");
dropdown = FindElementByXPath(dropdown, "//cssclass[#id='dashboardSelectorLink']");
dropdown = FindElementByCSS(dropdown, "cssclass[id = 'dashboardSelectorLink']");
dropdown = FindElementByID(dropdown, "dashboardSelectorLink");
dropdown = FindElementByClass(dropdown, "cssclass[id= 'dashboardSelectorLink']");
dropdown = FindElementByID(dropdown, "dashboardSelectorContainer");
The variable 'dropdown' is of IWebElement type and I am using a chrome driver for Selenium C#.
For context, the code is in the following function:
public void EnterDashboardArea()
{
IWebElement navbtn = null;
IWebElement category = null;
IWebElement dropdown = null;
navbtn = FindElementByID(navbtn, "Tab1");
ClickElement(navbtn);
category = FindElementByID(category, "MNG");
ClickElement(category);
Thread.Sleep(5000); //pausing the program so the page fully loads and all elements appear
dropdown = driver.FindElement(By.CssSelector("img[class='ms-crm-ImageStrip-Dropdown_Arrow']"));
ClickElement(dropdown);
}
In the function EnterDashboardArea(), the web elements initially are set to none where they are then filled by the functions 'FindElementByX' as they return an element value.
The functions 'FindElementByX' are created by myself, one e.g. is:
public IWebElement FindElementByID(IWebElement ele, string id) {
ele = driver.FindElement(By.Id(id));
return ele;
}
I even tried installing the selenium chrome extension in order to locate the target exactly but still no success.
What can I do please? - Thanks
Could it be that you are trying to retrieve the element before the element has been loaded into the DOM? Try to set a timeout before reading the element.
Also: is the element contained within a container that is initially hidden, before you open it? Maybe you have to click some button that shows the container and the target element within, before trying to read the element.

Selenium cannot locate Drop down located within modal

I'm trying to select a value from the drop down 'ddl_settpymtaction' but selenium cannot locate it within the modal that resides in.
CSS:
Selenium code
driver.FindElementById("btn_SettlementNew").Click();
var Action = driver.FindElementById("ddl_SettPymtAction");
var SelectElement2 = new SelectElement(Action);
SelectElement2.SelectByValue("EFT");
In selenium you should ALWAYS add some delay when performing async operations (such as a modal which loaded with animations + ajax calls)
Why? To get your page a chance to render itself and let selenium wait for the right time to inspect the page (after being rendered with the required elements)

Selenium - iframe within iframe

I am trying to find iframe within iframe using Selenium WebDriver. I have a scenario where a popup opens in a website which itself is an iframe and this in turn contains 4 richedit textboxes which are nothing but iframes. Here is how i am trying to do it. If i check the foreach loop, i get 4 iframes correctly which suggests that i am reaching right elements. What can be the reason for failure for not able to get correct tags inside iframe? Here is my code.
ReadOnlyCollection<IWebElement> topItem = driver.FindElements(By.XPath("//iframe[#src='Edit.aspx']"));
Utility.Instance.WaitUntilFrameLoaded(topItem[0]);
Thread.Sleep(2000);
ReadOnlyCollection<IWebElement> elements = Utility.Instance.WaitUnitElementsAppearsByXPath("//div[#id='ctlPanel']//table/tbody/tr[2]//iframe[#src='javascript:true;']");
Thread.Sleep(1000);
foreach(IWebElement eleFrame in elements)
{
Utility.Instance.WaitUntilFrameLoaded(eleFrame);
IWebElement body = driver.FindElement(By.TagName("body"));
body.Click();
driver.SwitchTo().DefaultContent();
Utility.Instance.WaitUntilFrameLoaded(topItem[0]);
}
However i always get the message that the element with tag name body not found. If i debug, i see html which contains a body tag. I am not sure why it does not provide me back with the element.

Stop page load during navigate url in selenium

I am using selenium tool in my C# windows application,
driver.Navigate().GoToUrl(baseURL);
when the the application executing this line the page loads takes 2mins.
During that time, the followin element has found within 10 sec.
driver.FindElement(By.Id("searchTerm"))
I used driver.FindElement(By.Id("searchTerm")).SendKeys(Keys.Escape);
But it wont work properly. The problem is after complete the page load then only the control execute next line . But I need to stop the page if the element has found.
Thanks
You could try issuing the Escape press at the top level, not on the element itself:
if (driver.FindElement(By.Id("searchTerm")) != null) {
Actions action = new Actions(driver);
action.SendKeys(Keys.Escape);
}
That seems to be the most common way of issuing a page load stop.
To stop page load at any moment you can use something like this:
IJavaScriptExecutor js = Brwsr.Instance as IJavaScriptExecutor;
js.ExecuteScript("window.stop();");

Selenium webdriver IsTextPresent (on the page more than once)

I am using selenium web driver to determine whether a string of text exists more than twice on a page, as it should only appear once.
I can use Assert.IsTrue(this.Driver.IsTextPresent("body", "string")), but I was looking for a while to see if the text was on the page more than once.
You can do this easily with FindElements:
driver.FindElements(By.XPath("//*[contains(text(),'TEXT')]")).Count;
If Count > 1, then the text is present more than once on the page.

Categories