Firefox URL not working using Selenium - c#

The following is basically what i did. It is exactly what the example said but it does not go to the URL when I run it.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
namespace TestApp.Selenium_Basics
{
class TestExercise01
{
public void TestExercise01Run()
{
IWebDriver driver = new FirefoxDriver();
driver.Url = #"http://www.facebook.com";
}
}
}

The line below will not work:
driver.Url = #"http://www.facebook.com";
Try this instead:
driver.Navigate().GoToUrl(#"http://www.facebook.com");

Related

Google API Client returning 404 not fund

I am trying to get the best searches from google, using keyword, and GoogleAPI, but It is always returning "System.Net.WebException: 'The remote server returned an error: (404) Not Found.'"
Am I doing anything wrong, or is it that GoogleAPI is outdated? Last time I checked and downloaded the .dll file was from 2010...
code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.IO;
using System.Security.Cryptography;
using System.Text.RegularExpressions;
using System.Threading;
using System.Diagnostics;
using System.Collections;
using Microsoft.Win32;
using System.Web;
using System.Net.Mail;
using System.Net.Mime;
using System.ComponentModel;
using System.Net;
using System.Windows;
using Google.Apis.Services;
using Google.Apis.CustomSearchAPI.v1;
//Ignore most using statements, It was imported from my previous code.
namespace TYZDsag
{
internal class Program
{
static void Main(string[] args)
{
var client = new Google.API.Search.GwebSearchClient("https://www.google.com/");
var results = client.Search("uhuhuh", 12);
foreach (var webResult in results)
{
Console.WriteLine("{0}, {1}, {2}", webResult.Title, webResult.Url, webResult.Content);
//listBox1.Items.Add(webResult.ToString());
}
}
}
}
That's not how you initialize a service object using an api key.
using System;
using System.Threading.Tasks;
using Google.Apis.CustomSearchAPI.v1;
using Google.Apis.Services;
namespace customsearch
{
class Program
{
static async Task Main(string[] args)
{
Console.WriteLine("Hello World!");
var service = new CustomSearchAPIService(new BaseClientService.Initializer()
{
ApiKey = "XXX",
ApplicationName = "xyz",
});
var result = await service.Cse.List().ExecuteAsync();
}
}
}
Method: cse.list
How to create an api key

addScriptToEvaluateOnNewDocument Selenium (Does not work)

I need to execute addScriptToEvaluateOnNewDocument method of Google Headless Chrome in C# .Net. I basically need to execute Javascript before any other javascript is executed on the documents. Here is my code:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Interactions;
namespace User
{
class Program
{
//public static IWebDriver driver = new ChromeDriver();
public static IWebDriver driver;
public static double magnification = 2;
static void Main(string[] args)
{
ChromeOptions options = new ChromeOptions();
//options.AddArgument("Page.addScriptToEvaluateOnNewDocument");
options.AddArgument("--window-size=1920,1080");
options.AddUserProfilePreference("Page.addScriptToEvaluateOnNewDocument", "alert('INJECTED SCRIPT')");
driver = new ChromeDriver(options);
var action = new OpenQA.Selenium.Interactions.Actions(driver);
System.Threading.Thread.Sleep(1000);
driver.Navigate().GoToUrl("https://google.com");
System.Threading.Thread.Sleep(10000);
}
}
}
But the alert isn't fired (i have tried other JavaScript code besides alert - nothing seems to work)
Try this.After initialize the driver.Hope this will work.
driver.ExecuteJavaScript("alert(0);")
OR
driver.Scripts().ExecuteScript("alert(0);")
I have only done this in Python but it should work the same way.
The chromedriver should expose a function named execute_cdp_cmd()
You can use this to run JavaScript before the page JavaScript is run. See the python example below. Note that you should add the code shown below after creating the driver but before you get the web page.
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
"source": """
alert(0);
"""
})
I don't have a C# example, but this might help.
using OpenQA.Selenium.DevTools;
using OpenQA.Selenium.DevTools.V91.Page;
using OpenQA.Selenium.Support.UI;
using RandomNameGeneratorLibrary;
using Network = OpenQA.Selenium.DevTools.V91.Network;
using DevToolsSessionDomains = OpenQA.Selenium.DevTools.V91.DevToolsSessionDomains;
IDevTools devTools = driver as IDevTools;
session = devTools.GetDevToolsSession();
var domains = session.GetVersionSpecificDomains<DevToolsSessionDomains>();
domains.Page.AddScriptToEvaluateOnNewDocument(new AddScriptToEvaluateOnNewDocumentCommandSettings()
{
Source = jscode,
});

Selecting image error using selenium c#

I'm running a basic c# test below. My test is essentially going to go onto www.asos.com then search for a specific item. When the search results are loaded I want to click on the first item that's returned. I tried to use CSS selector to click on the first imagine but I get an exception. Invalid selector.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.Interactions;
using System.Threading;
namespace Exercise1
{
class Exercise_2
{
static void Main(string[] args)
{
IWebDriver webDriver = new ChromeDriver();
webDriver.Navigate().GoToUrl("http://www.asos.com/men/");
webDriver.Manage().Window.Maximize();
webDriver.FindElement(By.XPath(".//input[#data-testid='search-input']")).SendKeys("Polo Ralph Lauren Oxford Shirt In Regular Fit Blue");
webDriver.FindElement(By.XPath(".//button[#data-testid='search-button-inline']")).Click();
//*[#id="product-6153807"]/a/div[1]/img Xpath of the image to be clicked on
webDriver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
webDriver.FindElement(By.CssSelector("6153807")).Click();
}
}
}
use the following css selector.
article img
in code,
webDriver.FindElement(By.CssSelector("article img")).Click();

c# - geckodriver.exe selenium 3.8 not running nunit 3.9

I try to run Nunit_selenium automation in updated firefox version,
vs 13,
selenium webdriver 3.8.0
NUnit3TestAdapter.3.9.0
Selenium.Support.3.8.0
Selenium.WebDriverBackedSelenium.3.8.0
Firefox launched but driver url link not updated
issue :in firefox notrun_image
please find the below code :
.
.
.
.
.
.
.
/
namespace SanityTesting
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework.Constraints;
using NUnit.Framework;
using NUnit.Core.Extensions;
using System.Configuration;
using System.IO;
using System.Reflection;
using OpenQA.Selenium.Interactions;
using OpenQA.Selenium;
using Syncfusion.UnitTesting.Framework;
using OpenQA.Selenium.Remote;
using System.Threading;
using OpenQA.Selenium.Support.UI;
using System.Collections;
using OpenQA.Selenium.Firefox;
using System.Diagnostics;
using System.Globalization;
using System.Drawing;
using SanityTesting.AccordionFluent;
using System.Windows.Forms;
[TestFixture("Firefox")]
[TestDirectory("..\\..\\Accordion\\")]
public class EmberJS_Accordion : NUnitUtil
{
string activeBrowser;
public EmberJS_Accordion(string browser)
{
FirefoxDriverService service = FirefoxDriverService.CreateDefaultService(#"C:\Drivers");
service.FirefoxBinaryPath = #"C:\Program Files (x86)\Mozilla Firefox\firefox.exe";
System.Environment.SetEnvironmentVariable("webdriver.gecko.driver", "C:\\Drivers\\geckodriver.exe");
FirefoxDriver driver = new FirefoxDriver(service); // FirefoxDriver driver = new FirefoxDriver();
//driver = GetWebDriverForBrowser(browser);
//FirefoxProfile profile = new FirefoxProfile();
//profile.SetPreference("browser.startup.page", 0); // Empty start page
//profile.SetPreference("browser.startup.homepage_override.mstone", "ignore"); // Suppress the "What's new" page
// return new FirefoxDriver(profile);
activeBrowser = browser;
}
[TestFixtureTearDown]
public void quitDriver()
{
driver.Quit();
}
public void TakeAndCompareScreenshotByLocator(By Locator, string fileName)
{
IWebElement contents = driver.FindElementAfterClickable(Locator);
TakeAndCompareScreenshot(contents, fileName);
}
[Test, TestCaseSource(typeof(AccordionModel), "Sanity_Accordion")]
[Category("Sanity")]
[Component(Component.Accordion)]
public void EmberJS_Accordion_Sanity(string url, int time)
{
AutomationHelpers.GotoPage(driver, url);
driver.Navigate().Refresh();
driver.WaitForElementClickable(By.CssSelector(".e-acrdn"), time);
Thread.Sleep(200);
IWebElement contents = driver.FindElement(By.ClassName("cols-sample-area"));
TakeAndCompareScreenshot(contents);
}
}
change the following line of code,
FirefoxDriver driver = new FirefoxDriver(service);
to
driver = new FirefoxDriver(service);
I have doubt, is your NUnitUtil class have driver member variable. if yes, then above code may work.

LinkFinder.find fails to work in webcrawler app

Wrote code as the start to a web crawler that scrapes links from webpage.
Following the instructions from this page:
http://www.dotnetperls.com/scraping-html
I seem to get an error that LinkFinder cannot be found?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Diagnostics;
namespace WebCrawler
{
class Program
{
static void Main(string[] args)
{
WebClient url = new WebClient();
String initialLink = url.DownloadString("http://www.FAKEADDRESS.org.uk/");
for (LinkItem i in LinkFinder.find(initialLink))
{
System.Diagnostics.Debug.WriteLine(initialLink);
}
}
}
}
LinkFinder is a class that is included in the code at that URL you provided. Make sure you also copy that class into your project in some way (a file by itself, in another file, whatever).

Categories

Resources