I am having trouble finding an iframe. I want to switch to this iframe then click on an element within it.
I have tried finding the iframe using Id, Xpath, TagName, and CssSelector but my test times out while looking for the element each time.
This is the iframe as it appears in the page source:
<div xmlns="http://www.w3.org/1999/xhtml" id="dashboardView" style="display: block;">
<iframe id="dashboardViewFrame" border="0" scrolling="no" frameborder="0"
style="visibility: visible; height: 607px; width: 1280px; background-color: transparent;"
src="HtmlViewer.ashx?Dd_ContentId=6a8a44ae-2bd5-4f3c-8583-e777279ad4f2"></iframe>
</div>
<iframe xmlns="http://www.w3.org/1999/xhtml" id="dashboardViewFrame" border="0" scrolling="no"
frameborder="0" style="visibility: visible; height: 607px; width: 1280px; background-color:
transparent;" src="HtmlViewer.ashx?Dd_ContentId=6a8a44ae-2bd5-4f3c-8583-e777279ad4f2"></iframe>
Here is my current code:
public static bool IsAt
{
get
{
try
{
var dashboardiFrame = Driver.Instance.FindElement(By.Id("dashboardViewFrame"));
//todo switch to iframe
//todo find element within iframe
return true;
}
catch
{
return false;
}
}
}
Can someone please suggest a way to find the iframe and switch to it?
some times you have to sleep around 5 second till page load completely then find frame.
try this
thread.sleep(50000);
IwebElement Frame = Driver.SwitchTo().Frame("id of the frame");
//then any element inside frame should get by this line
Frame.FindElement(By.id("ID of element inside frame");
The main problem was that my test opened a new window, but my test was looking for elements on the old window. I resolved that by switching to the new page using:
Driver.Instance.SwitchTo().Window(Driver.Instance.WindowHandles.Last());
Then I could switch to the iframe also by also using SwitchTo() as shown below:
public static bool IsAt
{
get
{
try
{
Driver.Instance.SwitchTo().Window(Driver.Instance.WindowHandles.Last());
var DBViFrame = Driver.Instance.FindElement(By.Id("dashboardViewFrame"));
Driver.Instance.SwitchTo().Frame(DBViFrame);
var dataEntryButton = Driver.Instance.FindElement(By.Id("HyperlinkDataEntry"));
dataEntryButton.Click();
return true;
}
catch(Exception ex)
{
return false;
}
}
}
Related
We are developing a website using MVC 5. We would like to automatically simulate a keypress (F11) when one of the views loads. This must happen automatically on view load.
The whole purpose of this is to make the browser fullscreen.
So far we have the following code bellow which works when testing locally but when we release it live to Azure, it does not work.
[DllImport("user32.dll")]
public static extern int SetForegroundWindow(IntPtr hWnd);
[STAThread]
public ActionResult StudentView()
{
while (true)
{
Process[] processes = Process.GetProcessesByName("chrome");
foreach (Process proc in processes)
{
SetForegroundWindow(proc.MainWindowHandle);
SendKeys.SendWait("{F11}");
}
Thread.Sleep(5000);
return View();
}
}
We have also tried some of the solutions in this question but again it does not work when the website is released to live. Simulating Key Press c#
Please assist us to make the browser full screen when view loads. Thanks in advance.
I am afraid you'll need a different approach. Not all browsers will use F11 to go full screen.
Importing the user32.dll, and simulating the key-press will only work, if executed on a windows client - locally, not from azure. There are some full screen options for browsers - but I am not sure if they fit you case. Video playback components are able to request full screen; you might want to dig into that.
Otherwise, if you target a specific OS or browser, you can create a custom client side app or plugin.
As for the javascript part, you can find an example here: https://www.w3schools.com/howto/howto_js_fullscreen.asp, but I am not sure if it will fit you requirements.
Here's one of the examples:
source: https://www.w3schools.com/howto/howto_js_fullscreen.asp
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
/* Chrome, Safari and Opera syntax */
:-webkit-full-screen {
background-color: yellow;
}
/* Firefox syntax */
:-moz-full-screen {
background-color: yellow;
}
/* IE/Edge syntax */
:-ms-fullscreen {
background-color: yellow;
}
/* Standard syntax */
:fullscreen {
background-color: yellow;
}
/* Style the button */
button {
padding: 20px;
font-size: 20px;
}
</style>
</head>
<body>
<h2>Fullscreen with JavaScript</h2>
<p>Click on the "Open Fullscreen" button to open this page in fullscreen mode. Close it by either clicking the "Esc" key on your keyboard, or with the "Close Fullscreen" button.</p>
<button onclick="openFullscreen();">Open Fullscreen</button>
<button onclick="closeFullscreen();">Close Fullscreen</button>
<script>
var elem = document.documentElement;
function openFullscreen() {
if (elem.requestFullscreen) {
elem.requestFullscreen();
} else if (elem.mozRequestFullScreen) { /* Firefox */
elem.mozRequestFullScreen();
} else if (elem.webkitRequestFullscreen) { /* Chrome, Safari & Opera */
elem.webkitRequestFullscreen();
} else if (elem.msRequestFullscreen) { /* IE/Edge */
elem.msRequestFullscreen();
}
}
function closeFullscreen() {
if (document.exitFullscreen) {
document.exitFullscreen();
} else if (document.mozCancelFullScreen) {
document.mozCancelFullScreen();
} else if (document.webkitExitFullscreen) {
document.webkitExitFullscreen();
} else if (document.msExitFullscreen) {
document.msExitFullscreen();
}
}
</script>
<p>Note: Internet Explorer 10 and earlier does not support the msRequestFullscreen() method.</p>
</body>
</html>
I'm trying to generate HTML page ( C# Razor View ) that will be converted to PDF ( using wkhtmltopdf )and printed on pre-printed stationery.
My problems is, that the pre-printed stationery has a header ( easy )
and tear-off part at the bottom (4cm) which should be blank until the very last page where some additional information is printed.
The whole invoice is being generated as
<table><thead></thead><tbody></tbody></table>
So how can I set margin of X on every page except the last printed
and on that last printed page instead of margin actually print something?
Tried to use with last-child selector but that did not work.
Perhaps some other solution will be more suitable for this using .Net Core ( some linux apps can be used as well )?
You can use the CSS #page rule to specify the page margins. You can also use this to specify additional margins for left & right-hand pages (to account for binding) and for the first page but not, it would seem, for the last page!
/* Default left & right is 2cm, top & bottom margin is 4cm */
#page { margin: 4cm 2cm }
/* First page, 10 cm margin on top */
#page :first {
margin-top: 10cm;
}
/* Left pages, a wider margin on the left */
#page :left {
margin-left: 3cm;
margin-right: 2cm;
}
/* Right pages, a wider margin on the right */
#page :right {
margin-left: 2cm;
margin-right: 3cm;
}
Further reading: https://www.w3.org/TR/CSS21/page.html#page-box
Finally I got to the bottom of that.
As the top of the page should not be a problem ( can be handled the same way as well , but the table thead is another option )
My solution is:
wkhtmltopdf test.html --footer-html footer.html output.pdf
And the whole trick is withing the footer.html file:
<!DOCTYPE html>
<html>
<head>
<script>
var mainHeader = "test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>";
var secondHeader = "OOOOOOOOOOPOPOPOPOPOPOPOPOPOPOPOPOPOPOPOPOPOPOPPOPO<br>";
function selectHeader() {
var vars = {};
var x = document.location.search.substring(1).split('&');
for (var i in x) {
var z = x[i].split('=', 2);
vars[z[0]] = decodeURIComponent(z[1]);
}
if (vars["page"] == vars['topage']) {
document.getElementById('main').innerHTML = secondHeader;
} else {
document.getElementById('main').innerHTML = mainHeader;
}
if (vars["page"] == vars['frompage']) {
document.getElementById('test').innerHTML = secondHeader;
}
}
</script>
</head>
<body onload="selectHeader()">
<div style="min-height: 6cm; background-color: aqua; max-height: 6cm; overflow:visible;">
<div id="main" onload="selectHeader()">
</div>
</div>
</body>
</html>
ps.
Read somewhere that without !DOCTYPE html it might not work.
Thanks for all the help
I need an assistance, how can I select a value from a drop down list, I am programming on C# with Selenium, and I am always getting the following error:
Element should have been select but was span
My code is:
internal static void SelectDD()
{
SelectElement xxx= new SelectElement(driver.FindElement(By.XPath("/html/body/div[1]/section/div[1]/div[2]/section/div/section/div[2]/div[2]/div/div[2]/div[1]/div/div[2]/div/div/a/span[1]")));
xxx.SelectByIndex(1);
}
HTML:
<span class="select2-hidden-accessible" role="status" aria-live="polite"></span> <span class="select2-hidden-accessible" role="status" aria-live="polite"></span> <span class="select2-hidden-accessible" role="status" aria-live="polite"></span>
<div id="select2-drop-mask" class="select2-drop-mask" style="display: block;"></div>
<div id="select2-drop" class="select2-drop select2-display-none select2-drop-auto-width select2-drop-active select2-drop-above" style="left: 361.583px; top: 472.1px; bottom: auto; display: block; width: 262px;"></div>
</body>
First of all SelectElement can only be used with the element with Select tag. This is a div and SelectElement class does not apply here.
Try finding the element and just performing a click as follows:
IWebElement element = driver.FindElement(By.Id("select2-drop"));
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
js.ExecuteScript("arguments[0].click();", element);
The problem is the dropdown is not actually a drop down. Select Element can only work with element the html should be like below.
A work around would be to create a custom method which will suit the html structure you have now.
var spans = driver.FindElements({selector});
foreach(var span in spans)
{
if(span.Text == "some")
span.Click();
}
If you know the text of the DropDown, you can use method SendKeys(string text) of IWebElement
internal static void SelectDD()
{
IWebElement xxx= driver.FindElement("");
xxx.SendKeys("Text Of The Dropdown");
}
I am trying to simulate a click on a image in IE through C# using SHDocVw but have a problem. My program does not seem to find the img in the code.. Here is what I got:
SHDocVw.ShellWindows AllBrowsers = new SHDocVw.ShellWindows();
foreach (SHDocVw.InternetExplorer ieInst in AllBrowsers)
{
mshtml.IHTMLDocument2 htmlDoc = ieInst.Document as mshtml.IHTMLDocument2;
string html = htmlDoc.body.outerHTML;
foreach (mshtml.HTMLImg imgElement in htmlDoc.images)
{
if (imgElement.nameProp.ToString().Equals("icon_go.GIF"))
{
imgElement.click();
}
}
}
Here is a part of the html code im working on:
<TD align=center><INPUT title="View Detail Statistics" style="BORDER-LEFT-WIDTH: 0px; HEIGHT: 14px; BORDER-RIGHT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-TOP-WIDTH: 0px; WIDTH: 14px" src="../App_Themes/Company/Images/icon_go.GIF" type=image name=process1></TD></TR>
A problem is that the picture IS a button on the website but I dont know how to press it through the C# code.
Is there maybe another way to select the button? Like through the name "process1" instead of going for the image name?
As pointed out in your comment you are looping IMG but you should be looping INPUT:
foreach (IHTMLElement element in htmlDoc.all)
{
var input = element as IHTMLInputImage;
if (input != null && Path.GetFileName(input.src).Equals("icon_go.GIF", StringComparison.OrdinalIgnoreCase))
{
((IHTMLElement)input).click();
}
}
I am trying to highlight (around the border) element that is found in selenium webdriver using C#. I have search the net all i found was java codes, but need it in C#.
or is there any other way to do it.
thanks
There is no native way to do this, but because Selenium allows you use to execute Javascript, you can accomplish it just with a little more work:
Therefore the question becomes "how do I change an elements borders in Javascript?"
If you use jQuery it's a little bit easier, you could find the element and then set some border properties. jQuery has a neat little css property that allows you to pass in a JSON dictionary of values, it will handle setting them all for you, an example would be like:
jQuery('div.tagged > a:first').css({ "border-width" : "2px", "border-style" : "solid", "border-color" : "red" });
That would find an element, and set it's border to be solid at 2px wide with a border colour of red.
However, if you already have an IWebElement instance of the element (likely) you can take the 'finding' responsibility out of jQuery/Javascript and make it simpler again.
This would be executed something like:
var jsDriver = (IJavaScriptExecutor)driver;
var element = // some element you find;
string highlightJavascript = #"$(arguments[0]).css({ ""border-width"" : ""2px"", ""border-style"" : ""solid"", ""border-color"" : ""red"" });";
jsDriver.ExecuteScript(highlightJavascript, new object[] { element });
If you just want basic Javascript, then you could make use of the .cssText property, which allows you to give a full string of CSS styles instead of adding them individually (although I don't know how supported it is cross browser):
var jsDriver = (IJavaScriptExecutor)driver;
var element = // some element you find;
string highlightJavascript = #"arguments[0].style.cssText = ""border-width: 2px; border-style: solid; border-color: red"";";
jsDriver.ExecuteScript(highlightJavascript, new object[] { element });
(Although there are more ways, I've just gone for the most verbose to make it clearer)
C# Extension Method: Highlights and Clears in 3 seconds.
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Remote;
using System.Reactive.Linq;
public static class SeleniumUtil
{
public static void Highlight(this IWebElement context)
{
var rc = (RemoteWebElement)context;
var driver = (IJavaScriptExecutor)rc.WrappedDriver;
var script = #"arguments[0].style.cssText = ""border-width: 2px; border-style: solid; border-color: red""; ";
driver.ExecuteScript(script, rc);
Observable.Timer(new TimeSpan(0, 0, 3)).Subscribe(p =>
{
var clear = #"arguments[0].style.cssText = ""border-width: 0px; border-style: solid; border-color: red""; ";
driver.ExecuteScript(clear, rc);
});
}
}
Thanks Arran i just modified your answer..
var jsDriver = (IJavaScriptExecutor)driver;
var element = //element to be found
string highlightJavascript = #"arguments[0].style.cssText = ""border-width: 2px; border-style: solid; border-color: red"";";
jsDriver.ExecuteScript(highlightJavascript, new object[] { element });
it works perfectly...
thanks once again.
Write below JavaScript Executor code in your Class file
public void elementHighlight(WebElement element) {
for (int i = 0; i < 2; i++) {
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript(
"arguments[0].setAttribute('style', arguments[1]);",
element, "color: red; border: 5px solid red;");
js.executeScript(`enter code here`
"arguments[0].setAttribute('style', arguments[1]);",
element, "");
}
Call the above method from Selenium test case to highlight a web page element. Check out below code which shows how it is done. elementHighlight method is called with searchBox as an argument.
#Test
public void GoogleSearch() throws Exception, SQLException {
driver.findElement(By.xpath("//center/div[2]")).click();
WebElement searchBox = driver.findElement(By.xpath("//div[3]/div/input"));
elementHighlight(searchBox);
driver.findElement(By.xpath("//div[3]/div/input")).clear();
driver.findElement(By.xpath("//div[3]/div/input")).sendKeys("Test");
driver.findElement(By.xpath("//button")).click();
}
On executing the above test, Selenium test will highlight the search box on Google home page. You can reuse elementHighlight method for highlighting any elements on web page.