Can title of browser be changed in selenium c#? - c#

In Selenium using Webdriver and C# how we can change the browser title?
Using javascript and jQuery as follow:
document.title='XXX'
or
$('title')[0].text='XXX'
Have no effect although we can change the title using Web developers tool console.
Is there any restriction in changing browser title in Selenium?
UPDATE:
Problem roots: Using JavaScriptExecutor that has been initialized with the driver on a window which had been closed.

As said in this answer,
you can run javascript code from selenium.
Your code will be like this:
WebDriver driver; // assume assigned elsewhere
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
string title = (string)js.ExecuteScript("document.title = 'hello'");
And it will change browser title.
Edit
Here full working code:
ChromeOptions options = new ChromeOptions();
options.AddArguments("--start-maximized");
var driver = new ChromeDriver(options);
driver.Navigate().GoToUrl("http://www.google.com");
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
string title = (string)js.ExecuteScript("document.title = 'hello'");
And here result:

Related

How to open 2 different links in two tabs using a singe Selenium c# Webdriver/ChromeDriver?

Following is my code
IWebDriver driver = new ChromeDriver();
driver.Url = "https://www.google.com/";
driver.Url = "https://login.yahoo.com/";
I want that both the links should be opened in different tabs of same browser window
How to achieve this?
TIA
Try this:
((IJavaScriptExecutor)driver).ExecuteScript("window.open();");
instead of
IWebElement body = driver.FindElement(By.TagName("body"));
body.SendKeys(Keys.Control + "t");
If you need to continue working on the first window you need to follow steps described here just remember to change Ctrl + t with the JavaScript above.
You should be able to do that as follows:
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL+"t");
driver.get("https://login.yahoo.com/");
Good luck
Andreas

Click on HTML elements with Scrapy (WebScraping)

I'm doing a program in c # using scrapySharp or HtmlAgilityPack. But I have the disadvantage of that part of the information that I need, to appear when I click on an HTML element (Button, link ).
In some forums it was commented that when using Selenium you could manipulate the html elements, so I tried the following
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
// Defines the interface with the Chrome browser
IWebDriver driver = new ChromeDriver ();
// Auxiliary to store the label element in href
Element IWebElement;
// Go to the website
driver.Url = url;
// Click on the download button
driver.FindElement (By.Id ("Download button")). Click ();
but being a web automation test, it opens a browser and the website to perform the selection process (clicks), so it is not of my use, since I have to perform the inspection on several websites internally.
Although I can continue using Selenium, I am looking for ways to avoid using the browser and instead click without it.
Does anyone know how to achieve the click of the link or button, without the need to open a browser for web scraping?
Hope this would be helpful to anyone who has the same requirements.
If you want to avoid opening the browser, you could use below settings in the ChromeDriver.
// settings for avoid opening browser
var options = new ChromeOptions();
options.AddArgument("headless");
var service = ChromeDriverService.CreateDefaultService();
service.HideCommandPromptWindow = true;
// url to access and scrape
var url = "https://example.com";
using (var driver = new ChromeDriver(service, options))
{
// access the url
driver.Navigate().GoToUrl(url);
// Click on the download button - copied from your code above
driver.FindElement (By.Id ("Download button")). Click ();
}
In addition to above below links also, you may find useful,
can-selenium-webdriver-open-browser-windows-silently-in-background
running-webdriver-without-opening-actual-browser-window

C# Mute Audio Selenium Chrome Browser

Like my title says, I am looking for a way to mute any audio on Selenium Chrome Web Driver.
I found this for python(example below) and was wondering if there was something similar for C#?
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--mute-audio")
Try this:
var options = new ChromeOptions();
options.AddArgument("mute-audio");
var driver = new ChromeDriver(options);

bypassing javascript to get page source

1)I am building an app for downloading all my pictures from my instagram profile.
2)I don't want to use instagram api so i searched and i find Selenium Webdriver and i add this package to my project
3)I am using c# to build android app (Xamarin) check below Code.
string url = "https://www.instagram.com/" + _theUsername;
IWebDriver driver = new FirefoxDriver();
driver.Url = url;
string source = driver.PageSource;
4)By Using above code i am getting below error :-
OpenQA.Selenium.DriverServiceNotFoundException: The geckodriver file does not exist in the current directory or in a directory on the PATH environment variable
5)Is it possible to use this in xamarin? i need to find a way to do this
any help will be greatly appreciated
I am not familiar with Selenium, but getting the page source for a url is easy with HttpClient:
string url = "https://www.instagram.com/" + _theUsername;
using (var httpClient = new HttpClient()){
var response = await httpClient.GetStringAsync(url);
Debug.WriteLine(response);
}
and response will have your page source. And if you do not want to show the web page then there is no need for a WebView anyway.

Accessing JQuery Auto complete with Selenium Webdriver

I am trying to get the HTML of a JQuery autocomplete after a word is typed in. To do this I decided to try selenium webdriver.
I had a read of the documentation and I think I got couple of issue.
1) I'ts not finding the search suggestions although I am pretty sure I am selecting it right
2) I don't know how to get the HTML to a string from an IWebElement.
Here is my code that doesn't work:
IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl("https://site.xxx/");
IWebElement query = driver.FindElement(By.Name("stext"));
query.SendKeys("iphone");
// Everything up to this point works, I put a wait in here just incase it takes a few seconds to load
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(2));
// This seems to come back empty but in developer tools for chrome I can see it
IWebElement results = driver.FindElement(By.ClassName("ac_results"));
Can anyone see where I am going wrong?
On your result call add .text(); to end to get the text value of the element.
Java syntax.
String val = driver.findElement(By.ClassName("ac_results")).text();
Or
WebElement we driver.findElement(By.ClassName("ac_results"));
String val = we.text();
I'd try the javascriptexecutor, see below: (C# version)
IWebDriver driver;
IJavaScriptExecutor jse = driver as IJavaScriptExecutor;
string body = (string)jse.ExecuteScript("return document.body");

Categories

Resources