Adding ChromeOptions for Selenium Webdriver C# - c#

From what I understand, the default action of when the Webdriver finds an element is to scroll such that the element is as far up the top of the page as possible. This is an issue because the website I'm working on has a header so every time I try to click on a button, it will instead click on the header. Thus, I want to change the scroll setting so that the element will be at the bottom of the page.
From reading this I was able to find what I wanted to set, however, I'm unable to set the DesiredCapabilites or ChromeOptions when I initialise the ChromeDriver. Could some provide code/steps to do this please?

You can use something like this
var chromeOptions = new ChromeOptions();
chromeOptions.AddUserProfilePreference("intl.accept_languages", "en");
chromeOptions.AddUserProfilePreference("disable-popup-blocking", "true");
var driver = new ChromeDriver(chromeOptions);
Edit-2
If the option you want to set doesn't work for you then try using actions
var elem = driver.FindElements(By.Id("your element"));
Actions action = new Actions(driver);
action.MoveToElement(elem).Click(elem).Perform();//move to list element that needs to be hovered
Edit-3
If the above also doesn't work then your next option is to use Javascript
var elem = driver.FindElements(By.Id("your element"));
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
var success = js.ExecuteScript("arguments[0].click(); return true", elem);

As far as I know you can't change everything through addarguments.
There is a list of what you can do in the Github page. but I have a better solution. you can make your own default settings and save it as a chrome profile. for example I didn't find anything to change homepage with code but this works fine for almost evwerything.
you can use this code :
options.AddArguments( #"user-data -dir=C:\Users\kian\AppData\Local\Google\Chrome\User Data");
options.AddArgument("--profile-directory=Default");
make sure you write the right path and right profile name.
to check the profile name you can go to properties.
properties
you will see the profile name.
there is a good guide for what else u can do in link.

Related

Use complex jquery selector with PuppeteerSharp

I am new to PuppeteerSharp and I have searched already on the internet for several hours and also the documentation, but I don't seem to find a solution to my problem.
Now, I have a table with some rows on my webpage (see attached picture). I need to find the row that has some text in one of the columns, and click the button left to it (the black one with an arrow).
I have got my button (at least the td) with JQuery in Firefox Dev tools like this:
$("td: Contains('41818397111')").parent().find("td[id = 'InvoiceDetails']")"
But I do not know how to use this information in PuppeteerSharp
I have tried:
ElementHandle element = await page.QuerySelectorAsync("td:Contains('41818397111')");
ElementHandle hanlde = await page.EvaluateFunctionAsync<ElementHandle>("e=> e.parent().find(\"td[id=['InvoiceDetails']\")", element);
await page.ClickAsync($("td: Contains('41818397111')").parent().find("td[id = 'InvoiceDetails']")")
but neither one works. I think I am missing some information.
Thank you
So, after many tries and a lot of searched, I ended up switching to Selenium, which seems to have better documentation and a lot of examples here.
This is the code, that worked for me:
string locator1 = "td:Contains(\'41818397111\')";
string locator2 = "td[id = \'InvoiceDetails\']";
IWebElement webElement = driver.FindElement(Selenium.WebDriver.Extensions.By.JQuerySelector(locator1).Parent().Find(locator2));
webElement.Click();

How to specify ENTER button functionality in Selenium WebDriver code using C#

For practice sake, I'm writing test cases for irctc website, there I need to enter from station place and then respective stations with that code will be displayed as bootstrap dropdown and now i have to select one among them and click enter. Unfortunately there is no enter/submit button for from and to text field, please help me to continue with this test case
Here is my code
IWebElement Fromstn = driver.FindElement(By.XPath("//*[#id='divMain']/div/app-main-page/div/div/div[1]/div[1]/div[1]/app-jp-input/div/form/div[2]/div[1]/div[1]/span/i"));
Thread.Sleep(2000);
Fromstn.SendKeys("MAQ");
Fromstn.Click();
```**OR**
Actions builder = new Actions(driver); Actions hover = builder.MoveToElement(driver.FindElement(By.XPath("//*[#id='origin']"))); hover.Build().Perform(); Thread.Sleep(2000); hover.SendKeys("MAQ"); hover.Click();
from input try the below css :
p-autocomplete#origin input
To input try the below css :
p-autocomplete#destination input
Code :
driver.FindElement(By.CssSelector("p-autocomplete#origin input")).SendKeys("MAQ");
driver.FindElement(By.CssSelector("p-autocomplete#destination input")).SendKeys("some to station");
and if you wanna do Keyboard enter then probably use it with sendkeys():
something like this :
driver.FindElement(By.CssSelector("p-autocomplete#origin input")).SendKeys("MAQ" + Keys.RETURN);
See if this works:-
driver.FindElement(By.XPath("//label[text()='From']/..//input")).SendKeys("MAQ");
//Add a wait time for the drop down value to load
Actions builder = new Actions(driver);
Actions hover = builder.MoveToElement(driver.FindElement(By.XPath(".//ul[#id='pr_id_1_list']/li"))).Click().Perform();
You can try this code. For debugging see the Fromstn object in the quick watch and see if it has returned the correct element. for debugging you can also see the element is still in the form by 'inspect element' and do a find with the Xpath given when you are on the breakpoint.
IWebElement Fromstn = driver.FindElement(By.XPath("//*[#id='divMain']/div/app-main-page/div/div/div[1]/div[1]/div[1]/app-jp-input/div/form/div[2]/div[1]/div[1]/span/i"));
Thread.Sleep(2000); //you can also try by increasing the value for testing say 10 seconds
Fromstn.Clear();
Fromstn.SendKeys("MAQ");
Fromstn.Click();

With DesiredCapabilities deprecated, how will I use SetCapability in selenium webdriver C #?

With DesiredCapabilities deprecated, how will I use SetCapability in selenium webdriver C #?
Can I use it this way?
capacidades = new ChromeOptions();
capacidades.AddAdditionalCapability(#"browserName", #"chrome");
Instead of DesiredCapabilities, you should be using a browser-specific “Options” class, just as shown in your example. However, you can only call AddAdditionalCapability for capability names that do not already have a type-safe property or method to set the capability value. In the case of the browserName capability, there is already a BrowserName property to access the value of that capability. The runtime exception you receive should have the name of the property or method to use in place of manually setting the capability with that name, but I believe there is a bug that does not properly format the exception message.
Note, however, that the BrowserName property is read-only, because since you’re using ChromeOptions, the bindings already know the browser name should be “chrome”.
Adding a simple example to the answer from JimEvans.
Simple Example to disable chrome info bars:
private IWebDriver GetChromeDriver()
{
var outPutDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
//Disable chrome info bar in order to prevent "Chrome is being run by automation software."
var chromeOption = new ChromeOptions();
chromeOption.AddArguments("disable-infobars");
return new ChromeDriver(outPutDirectory, chromeOption);
}

How to get Selenium to operate two browser windows using only one driver selenium (using c# and chromedriver)?

I am attempting to control two browser windows via selenium using c# and a single chromedriver. The reason being that I need to share session details accross browser windows.
The code that I have tried and failed with is below;
var options = new ChromeOptions();
options.AddArguments("chrome.switches", "--disable-extensions --disable-extensions-file-access-check --disable-extensions-http-throttling --disable-infobars --enable-automation ");
options.AddUserProfilePreference("credentials_enable_service", false);
options.AddUserProfilePreference("profile.password_manager_enabled", false);
options.PageLoadStrategy = PageLoadStrategy.Default;
ChromeDriverService service = ChromeDriverService.CreateDefaultService();
service.HideCommandPromptWindow = true;
var Driver = new ChromeDriver(service, options);
//THIS WILL OPEN A NEW WINDOW. BUT BECAUSE IT IS A NEW DRIVER DOES NOT WORK FOR SHARING SESSION DETAILS.
//var TestDriver = new ChromeDriver(service, options);
//TestDriver.Manage().Window.Maximize();
//THIS JUST OPENS UP A NEW TAB. NOT A NEW WINDOW (IT WOULD SEEM MOST DOCUMENTATION SUGGESTS THAT IT SHOULD)
IJavaScriptExecutor jscript = Driver as IJavaScriptExecutor;
jscript.ExecuteScript("window.open();", "google.com.au");
//TRY USING THE SEND KEYS TECHNIQUE. NOTHING HAPPENS
var test = Driver.FindElement(By.TagName("html"));
test.SendKeys(Keys.Control + "n");
test.SendKeys(Keys.Control + "t");
//TRY AGAIN USING THE SEND KEYS TECHNIQUE USING A DIFFERENT TAG. NOTHING HAPPENS
var blah = Driver.FindElements(By.TagName("body"));
blah[0].SendKeys(Keys.Control + "t");
//TRY USING ACTIONS. NOTHING HAPPENS
Actions action = new Actions(Driver);
action.SendKeys(OpenQA.Selenium.Keys.Control + "n");
action.Build().Perform();
I may resort to AutoIt to open a browser if I have to, but one more dependency is not what I need. Documentation everywhere around the web seems to suggest than all the options I tried above should work...I suspect it may be a chromedriver issue of some kind.
Any ideas on how to achieve my goal would be greatly appreciated
UPDATE.
Arnons answer below lead me to the solution. If you are in a similar situation the best thing to do is just open up the browser console (from developers tools) and experiment with javascript until you get what you want. Then just execute that. In the end executing the following code has worked for me.
IJavaScriptExecutor jscript = Driver as IJavaScriptExecutor;
jscript.ExecuteScript("window.open('https://www.bing.com.au','_blank','toolbar = 0, location = 0, menubar = 0')");
The other alternative was to use Autoit, which I also got working, much easier than I did figuring out the javascript. But one less dependency is best :)
UPDATE2.
Further complications arise with trying to control the window as an independent browser window. I believe any new window created from a parent window, has the same process id (at least my testing has indicated so), and for all intense and purpose is treated as a tab in the selinium driver. I therefore conclude that certain things are just not possible (for example relocating the child browser window on the screen).
Your first attempt using ExecuteJavaScript was very close, but In order for it to open a new window instead of new tab, you should add the following arguments: `"_blank", "toolbar=0,location=0,menubar=0" to it.
See this question for more details.
I should have read the question better, here is my solution. Ended up using this for selecting windows that popped up after clicking a button but should work with swapping between windows.
//---- Setup Handles ----
//Create a Handle to come back to window 1
string currentHandle = driver.CurrentWindowHandle;
//Creates a target handle for window 2
string popupWindowHandle = wait.Until<string>((d) =>
{
string foundHandle = null;
// Subtract out the list of known handles. In the case of a single
// popup, the newHandles list will only have one value.
List<string> newHandles = driver.WindowHandles.Except(originalHandles).ToList();
if (newHandles.Count > 0)
{
foundHandle = newHandles[0];
}
return foundHandle;
});
//Now you can use these next 2 lines to continuously swap
//Swaps to window 2
driver.SwitchTo().Window(popupWindowHandle);
// Do stuff here in second window
//Swap back to window 1
driver.SwitchTo().Window(currentHandle);
// Do stuff here in first window
You need to explicitly tell Selenium which tab you wish to interact with, which in this case would be;
driver.SwitchTo().Window(driver.WindowHandles.Last());

Switching tabs not working in Selenium Webdriver

I wrote this code in C# and it's not working for me.
Second url is still opening in the first tab, although I switched tabs and updated handle.
// open first page in first tab
string firstPageUrl = "http://google.com";
driver.Navigate().GoToUrl(firstPageUrl);
// getting handle to first tab
string firstTabHandle = driver.CurrentWindowHandle;
// open new tab
Actions action = new Actions(driver);
action.SendKeys(OpenQA.Selenium.Keys.Control + "t").Build().Perform();
string secondTabHandle = driver.CurrentWindowHandle;
// open second page in second tab
driver.SwitchTo().Window(secondTabHandle);
string secondPageUrl = "http://bbc.com";
driver.Navigate().GoToUrl(secondPageUrl); // FAILED, page is opened in first tab -.-
Thread.Sleep(2000); // slow down a bit to see tab change
// swtich to first tab
action.SendKeys(OpenQA.Selenium.Keys.Control + "1").Build().Perform();
driver.SwitchTo().Window(firstTabHandle);
Thread.Sleep(1000);
action.SendKeys(OpenQA.Selenium.Keys.Control + "2").Build().Perform();
I'm using latest Chrome driver.
Any ideas?
EDIT:
This is not duplicate. I am not opening links here, I want to navigate to URL I've provided. Also, I am using CurrentWindowHandle as suggested, but it is not working for me.
string secondTabHandle = driver.CurrentWindowHandle; is still returning the first tab, even if the second tab cover the first one. Try this
string firstTabHandle = driver.getWindowHandle();
Actions action = new Actions(driver);
action.SendKeys(OpenQA.Selenium.Keys.Control + "t").Build().Perform();
// switch to the new tab
for (String handle : driver.getWindowHandles()) {
if (!handle.equals(firstTabHandle))
{
driver.switchTo().window(handle);
}
}
// close the second tab and switch back to the first tab
driver.close();
driver.switchTo().window(firstTabHandle);
Not entirely sure if this is related to your problem (but I think it might be):
I've found, with selenium, that a better alternative to using Thread.Sleep() is using:
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(15));
where first argument is the selenium driver you're using, and the second argument is how long you want to wait before giving up (the timeout)
You can then call:
wait.Until(condition);
where condition is what you want to wait for. There is a selenium class that provides various useful conditions called ExpectedConditions, for example:
ExpectedConditions.ElementToBeClickable(By by)
which would wait for a particular clickable element to be ready to click.
In fact, I've found that any time the content of the page changes, it's a good idea to wait for the element you're using next like this.

Categories

Resources