I attempting to send a Ctrl + 0 to set IE 9 to 100%.
I have tried the following snippets of code:
IWebElement query = Driver.FindElement(By.TagName("html"));
query.SendKeys("^0");
query.SendKeys(Keys.Control + Keys.NumberPad0);
I have also tried the actions object code:
Actions action = new Actions(Driver);
action.KeyDown(Keys.Control).SendKeys("0").KeyUp(Keys.Control).Perform();
I have read that in Java, WebDriver supports Keys.chord()
But I cannot find an implementation of this in C#.
Any ideas would be helpful.
The <html> tag will never be "visible" to the driver, thus can't have keys sent to it. The following code works for me:
IWebElement element = driver.FindElement(By.TagName("body"));
element.SendKeys(Keys.Control + "0");
However, with the most recent versions of the IEDriverServer.exe, you will not be able to get this far unless you set a special option for the driver to disable detection of the zoom setting. If your zoom level is not set to 100%, the InternetExplorerDriver constructor will throw an exception unless you instantiate your driver with something like:
InternetExplorerOptions options = new InternetExplorerOptions();
options.IgnoreZoomLevel = true;
IWebDriver driver = new InternetExplorerDriver(options);
Related
The same method used in the test automation project I wrote in c # does not work in internet explorer 11 even though the movement method I use is chrome, firefox and edge. It does not give any errors, but the next action is fail
log.Debug("fare " + by + " üzeriine dogru haraket ediyor, webelement label ");
IWebElement element = GetElement(by);
Actions Actions = new Actions(Driver);
WaitElementToClickable(Driver, by, 5);
Actions.MoveToElement(element);
Actions.Perform();
WaitElementToClickable(Driver, by, 5);
I spent a long time trying to get actions to work across all browsers, and for IE I found the following helped.
Selenium webdriver v2.29.0 (https://github.com/SeleniumHQ/selenium/blob/master/java/CHANGELOG) added:
IEDriver supports "requireWindowFocus" desired capability. When
using this and native events, the IE driver will demand focus and
user interactions will use SendInput() for simulating user
interactions. Note that this will mean you MUST NOT use the
machine running IE for anything else as the tests are running.
When I set the IEDriver I use:
InternetExplorerOptions options = new InternetExplorerOptions();
options.requireWindowFocus();
webDriver = new InternetExplorerDriver(options);
And all my move to and click events work fine. I'm using IE11.125-11.309 and Selenium (java bindings) 3.7.1.
How can I reset Chrome's zoom level to Default(100%) using Selenium WebDriver? It's really easy to do using keyboard keys "Ctrl + 0".
However, when I try to emulate this using WebDriver, I can't get anything to work. Here is what I tried without success:
[TestMethod]
public void ZoomToDefaultLevelWithChrome()
{
var driver = new ChromeDriver();
driver.Navigate().GoToUrl("https://www.ultimateqa.com");
var actions = new Actions(driver);
var browser = (IJavaScriptExecutor)driver;
var html = driver.FindElement(By.TagName("html"));
//None of these work
actions.SendKeys(Keys.Control).SendKeys(Keys.Add).Perform();
actions.KeyDown(Keys.Control).SendKeys(Keys.NumberPad0).Perform();
//actions.KeyDown(Keys.Control).SendKeys(Keys.NumberPad0).Perform();
//actions.KeyDown(Keys.Control).SendKeys(html, "0").Perform();
actions.KeyUp(Keys.Control);
actions.KeyDown(Keys.Control).Perform();
actions.SendKeys(html, "0").Perform();
actions.SendKeys(html, Keys.NumberPad0).Perform();
actions.SendKeys(Keys.NumberPad0).Perform();
browser.ExecuteScript("document.body.style.zoom = '1.0'");
browser.ExecuteScript("document.body.style.transform='scale(1.0)';");
browser.ExecuteScript("document.body.style.zoom='100%';");
}
The first 2 solutions from here don't work in C#: Selenium webdriver zoom in/out page content
Also, this doesn't work in Chrome even though it might in other browsers: selenium vba code to zoom out webpage to 60%
Can you try the below. This should work.
((IJavaScriptExecutor)driver).executeScript("document.body.style.zoom='100%';");
I have also encountered this problem, a work around I found was good was setting the the resolution capability
capability.SetCapability("resolution", 1920x1080);
C#, FierFox, Selenium v3.8
My purpose is movement cursor emulation in browser, using IMouse.MouseMove. Google searching gives me something like that, but I always receive System.NullReferenceException.How I should to use IMouse interface?
Simple console app:
FirefoxDriverService service = FirefoxDriverService.CreateDefaultService(Directory.GetCurrentDirectory() + "\\bin\\");
service.FirefoxBinaryPath = #"C:\Program Files (x86)\Mozilla Firefox\firefox.exe";
IWebDriver FF = new FirefoxDriver(service);
FF.Navigate().GoToUrl("https://www.google.ru/");
IWebElement element = FF.FindElement(By.Name("btnK"));
Actions act = new Actions(FF);
act.MoveToElement(element).Perform(); // It's OK
ILocatable hoverItem = (ILocatable)element;
IMouse mouse = ((IHasInputDevices)FF).Mouse;
mouse.MouseMove(hoverItem.Coordinates, 100, 100); // System.NullReferenceException
FF.Quit();
The short answer is that you don't do that in the .NET bindings. The IKeyboard and IMouse interfaces aren't intended to be used by users' code. This is despite the fact that they're public, and do have documentation. What you're running into is that those interfaces will not work for drivers that implement the W3C WebDriver specification. At the moment, that list is Firefox and IE (when using version 3.5 or later of the IE driver), but that list is going to grow to include Chrome and Edge soon, and eventually Safari too. The appropriate way to simulate mouse movement with the .NET bindings is through using the Actions class.
I'm going to explain this the best I can.
I run tests using the 3 major browsers, firefox, chrome and IE.
I have line where I select data from a drop down menu. Here is an example of what I use.
new SelectElement(CPC_Main.driver.FindElement(By.XPath("//select[#id='orgVdc']"))).SelectByText("Selenium_vDC");
This will select my element orgVdc and select the text value by Selenium_vDC. This will work perfectly for Firefox however it hangs and timesout for Chrome.
From what I can tell the issue is related to the default value of that element.
For example if default value of the element is "Test_vDC" selenium will successfully change it "Selenum_vDC" for all browsers.
However if the default value was "Selenium_vDC" already then Chrome will hang on trying to select that same value.
I hope that explains this enough, in a nut shell Chrome does not like matching default values.
I had encountered issue like this in the past. I used to get exceptions like "The element is no longer attached to the DOM". The issues(stale reference exceptions) like these needs a solid exception handling to make sure Automation execution is not affected.
You can try the below code. You can modify the below code as per your code set up.
public void SetDropDownValue(string Xpath, string value)
{
var element = FindElement(By.Xpath("Xpath"));
var selectElement = new SelectElement(element);
//check whether the option is selectable or not
var wait = new WebDriverWait(this.driver, this.testCaseConfiguration.WaitTime);
wait.Until(ExpectedConditions.TextToBePresentInElement(selectElement , option));
try
{
selectElement.SelectByText(value);
}
catch (StaleElementReferenceException)
{
element = FindElement(By.Xpath("Xpath"));
selectElement = new SelectElement(element);
selectElement.SelectByText(value);
}
}
class Program
{
static void Main(string[] args)
{
IWebDriver driver = new ChromeDriver();
//Dev environment
driver.Url = /*URL*/;
var trialGroupName = driver.FindElement(By.Name("TrialGroupName"));
trialGroupName.SendKeys(/*trial group name */);
var userName = driver.FindElement(By.Name("UserName"));
userName.SendKeys(/*username*/);
var password = driver.FindElement(By.Name("UserPassword"));
password.SendKeys(/*password*/);
var loginButton = driver.FindElement(By.Id("Ok"));
loginButton.Click();
//Find and click on Browse tab
var browseTab = driver.FindElement(By.Id("17"));
browseTab.Click();
}
}
OpenQA.Selenium.NoSuchElementException: 'no such element: Unable to locate element: {"method":"id","selector":"17"}
(Session info: chrome=58.0.3029.110)
(Driver info: chromedriver=2.30.477700 (0057494ad8732195794a7b32078424f92a5fce41),platform=Windows NT 6.1.7601 SP1 x86_64)'
Here's the inspect information:
<td>
<a id="17" class="defaultMenu initMenu" href="javascript:onMenuHref(17)" notran="">Browse</a>
</td>
I've also tried running it with the XPath (both the short version from Chrome and the long version from FireBug) and I receive the same error. I've tried adding in an implicit wait and a Thread.Sleep() but still received the same error. Also tried starting on a different field and tabbing to it and then it gave me the same exception but on the new field. I also tried playing around with cssSelector this morning and couldn't get that to work either. I did try to do an explicit wait but I'll be honest, I'm still trying to digest and understand it as waits are a new concept and I don't fully understand it, so I don't think I'm doing it right.
I'm a beginner, and I'm currently just trying to write the script to get it to the actual page that needs testing. I've been teaching myself some coding and selenium as I'm the only QA person at my company right now and I really think we could benefit from automation. This portion of the page isn't actually programmed by us it's done by a third party who hosts the system we use so I'm limited to what I see in the inspect.
(I commented out the user login information and URL because it is for internal use only and the page can't be reached outside of our systems anyways. Also I'm pretty sure I'm not allowed to do so by my employer).
Any help on this would be greatly appreciated, or any additional resources I can use. I've been using google mostly so there may be other pages you are aware of that could help me that I haven't found yet.
The login button takes you to a new page, which you need to wait for it to load. Try using an explicit wait to wait for the tab to be clickable. The timeout is 10 seconds in the following example, so modify that as needed.
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement browseTab = wait.Until(ExpectedConditions.ElementToBeClickable(By.Id("17")));