My current code below in C# opens a window then navigates to the specified URL after a button click.
protected void onboardButton_Click(object sender, EventArgs e)
{
IWebDriver driver = new ChromeDriver();
driver.FindElement(By.CssSelector("body")).SendKeys(Keys.Control + "t");
driver.Navigate().GoToUrl("http://www.google.com")
}
But the site that I am planning to navigate to has single sign-on. How can I open a new tab in my existing browser session and navigate from there? The above code does not seem to work.
Sending Keys.Control + "t" didn't work for me. I had to do it with javascript and then switch to it.
((IJavaScriptExecutor)driver).ExecuteScript("window.open();");
driver.SwitchTo().Window(driver.WindowHandles.Last());
To handle new tab you should switch to it first. Try following:
driver.FindElement(By.CssSelector("body")).SendKeys(Keys.Control + "t");
driver.SwitchTo().Window(driver.WindowHandles.Last());
driver.Navigate().GoToUrl("http://www.google.com")
Also you might need to switch back:
driver.SwitchTo().Window(driver.WindowHandles.First());
This may not works:
driver.FindElement(By.CssSelector("body")).SendKeys(Keys.Control + "t");
Alternative: Find clickable element with target blank (search for "blank" in page's surce code). This will open new tab.
Than switch between tabs (thanks #Andersson) with:
driver.SwitchTo().Window(driver.WindowHandles.Last());
driver.SwitchTo().Window(driver.WindowHandles.First());
We can simulate Ctrl + Element Click
Actions action = new Actions(_driver);
action.KeyDown(Keys.Control).MoveToElement(body).Click().Perform();
Reference:
https://www.codeproject.com/Answers/1081051/Open-link-in-New-tab-using-Selenium-Csharp-for-chr#answer3
The solution is really simple:
Driver.SwitchTo().NewWindow(WindowType.Tab);
Enjoy...
IWebDriver driver = new ChromeDriver();
Change this to:
var driver = new ChromeDriver();
I do not know why. May be the IWebDriver miss the method.
Related
After hours of fails, I am coming here. I need to scrape a dynamically generated webpage (made using Vue.JS, but I would prefer not to share the link).
I have tried multiple approaches (1, 2, 3). None of them works on this webpage.
The most promising solution was using Selenium and PhantomJS. I tried it like this and I'm not sure why it's not even working for Google:
private void button1_Click(object sender, EventArgs e) {
PhantomJSDriverService service = PhantomJSDriverService.CreateDefaultService();
service.IgnoreSslErrors = true;
service.LoadImages = false;
service.ProxyType = "none";
var driver = new PhantomJSDriver(service); // I also tried: new PhantomJSDriver();
driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(10);
driver.Url = "https://google.com";
driver.Navigate();
var source = driver.PageSource;
textBox1.AppendText(source);
}
Did not work:
I also tried with a WebBrowser Control, but the page never fully loads:
(EDIT: I found out WebBrowser just instantiates IE, and after trying to open the target website in standalone IE browser, the webpage also never loads completely, so it makes sense to see the same behaviour inside WebView. I think I am bound to Selenium&PhantomJS due to this fact.)
Surely this shouldn't be so complicated. How to do it properly?
if you need to scrape a website you can use ScrapySharp scraping framework. You can add it to a project as a nuget.
https://www.nuget.org/packages/ScrapySharp/
Install-Package ScrapySharp -Version 2.6.2
It has many useful properties to access different elements on the page.For example to access the entire HTML of the page you can use the following:
ScrapingBrowser Browser = new ScrapingBrowser();
WebPage PageResult = Browser.NavigateToPage(new Uri("http://www.example-site.com"));
HtmlNode rawHTML = PageResult.Html;
Console.WriteLine(rawHTML.InnerHtml);
Console.ReadLine();
I have the ff.code which opens a chrome browser and press "F12" to open chrome dev tools, I've been searching in google on how to press Ctrl+] but unfortunately did not find any article/info about it. Has anyone here made this stuff? thanks!
IWebDriver wdriver = new ChromeDriver();
wdriver.Navigate().GoToUrl("www.samplewebapp.com");
wdriver.Manage().Window.Maximize();
Actions action = new Actions(wdriver);
action.SendKeys(OpenQA.Selenium.Keys.F12).Perform();
Use either
Actions action=new Actions(driver);
action.SendKeys(OpenQA.Selenium.Keys.Control + "]").Build().Perform();
or
yourWebElement.SendKeys(Keys.Control + "]");
I am writing a program to run videos listed on my site for testing purpose and here what I need is to run videos in different tabs of the same browser window.
I have hundred video urls in the List videoLinks = getVideoUrls();
and now what I need is to execute these videos 5 at a time.
ChromeDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("https://www.withoutabox.com" + videoLink);
If I go the above way then for all videos I will have to create a new ChromeDriver object. I want to use single chrome browser object.
I have tried this
IWebElement body = driver.FindElement(By.TagName("body"));
body.SendKeys(Keys.Control + "t");
it only adds a new tab but not open a link there.
Please let me know how should I go around it. I have googled but couldn't find my solution so thought to ask for help.
Try this:
public void SwitchToTab(object pageId)
{
webDriver.SwitchTo().Window(pageId.ToString());
}
You can use CurrentWindowHandle to find current tab.
webDriver.CurrentWindowHandle;
For your scenario I'm using that code:
public IPageAdapter OpenNewTab(string url)
{
var windowHandles = webDriver.WindowHandles;
scriptExecutor.ExecuteScript(string.Format("window.open('{0}', '_blank');", url));
var newWindowHandles = webDriver.WindowHandles;
var openedWindowHandle = newWindowHandles.Except(windowHandles).Single();
webDriver.SwitchTo().Window(openedWindowHandle);
return new SeleniumPage(webDriver);
}
Update
Window open create new popup. By default this option can be blocked by browser settings. Disable popup blocking in your browser manually.
To check this, open js console in your browser and try to execute command window.open('http://facebook.com', '_blank');
If new window open successfully than everythng is OK.
You can also create your chrome driver with specific setting. Here is my code:
var chromeDriverService = ChromeDriverService.CreateDefaultService();
var chromeOptions = new ChromeOptions();
chromeOptions.AddUserProfilePreference("profile.default_content_settings.popups", 0);
return new ChromeDriver(chromeDriverService, chromeOptions, TimeSpan.FromSeconds(150));
Here is a simple solution for open a new tab in seleneium c#:
driver.Url = "http://www.gmail.net";
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
js.ExecuteScript("window.open();");
I'm using Selenium 2 on Windows to automate firefox. I tried to send 'Alt+Esc' to the browser to minimise it on start. However firefox keeps typing the '{%ESC}' in its address bar. What should I do? thanks.
using (driver = new FirefoxDriver(firefoxProfile))
{
driver.Keyboard.SendKeys("{%ESC}");
}
I use this to open a new window (with the keyboard input) and it works just fine
IWebDriver driver = new FirefoxDriver();
Actions action = new Actions(driver);
action.SendKeys(OpenQA.Selenium.Keys.Control + "n").Build().Perform();
I prefer:
element.SendKeys(Keys.Control + "a");
Now you can focus on the element and not only on the webdriver
Sending regular key use SendKeys("ab12#$").
Sending a modifier key (Keys.Shift, Keys.Control, or Keys.Alt) use
.[KeyUp\keyDown]([OpenQA.Selenium.Keys.Control\OpenQA.Selenium.Keys.Alt...])
And you can combine them as needed:
action.KeyDown(OpenQA.Selenium.Keys.Shift).SendKeys("ThisWillBePrintedCAPS").KeyUp(OpenQA.Selenium.Keys.Shift).SendKeys("ThisWillBePrintedRegular").Build().Perform();
How can I open a tab and go to "http://anotherwisite.com"? I tried this:
ie = new WatiN.Core.IE("https://somewebsite.com", true);
ie.TextField(WatiN.Core.Find.ByName("user")).TypeText(User.getName());
ie.TextField(WatiN.Core.Find.ByName("password")).TypeText(User.getPassword());
ie.Button(WatiN.Core.Find.Any).Click();
(the above code opens an IE and log the person in)
I can open a new Tab and open new page with Watin
Code:
var browser1 = new FireFox("http://www.omda.com");
SendKeys.SendWait("^{t}");
browser1.GoTo("http://www.omda2.com");
I use the keyboard, CTRL+T in Firefox.
There is no built-in method in WatiN to do that.
System.Windows.Forms.SendKeys.SendWait("^{t}");
Thread.Sleep(400);
//this is the new browser instance
var newTab = Browser.AttachTo<IE>(Find.ByTitle(string.Empty));