Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding ChromeOptions for Selenium Webdriver C#

Tags:

c#

selenium

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?

like image 522
chiangy77 Avatar asked Jun 19 '26 15:06

chiangy77


1 Answers

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);
like image 131
Tarun Lalwani Avatar answered Jun 21 '26 04:06

Tarun Lalwani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!