Select element issue - webdriver - c# - c#

I'm getting the below error while running in NUnit,
I'm finding an element and storing it in a variable, and while trying to select the element, i'm getting the error. Tried using in this way
IWebElement fromitem = WebDriver.FindElement(By.Id("from")); but same error persists.
Is there any way i can select the element?
Note : I verified the element id with firebug, there seems to be no problem with it.
The code below,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Support.Events;
using OpenQA.Selenium.Support.PageObjects;
namespace SeleniumTests
{
[TestFixture]
public class Sel
{
public static IWebDriver WebDriver;
private String baseURL;
[SetUp]
public void SetupTest()
{
WebDriver = new FirefoxDriver();
baseURL = "http://www.x-rates.com/calculator.html";
}
[TearDown]
public void TeardownTest()
{
WebDriver.Quit();
}
[Test]
public void newtest()
{
WebDriver.Navigate().GoToUrl(baseURL + "/");
var fromitem = WebDriver.FindElement(By.Id("from"));
var toitem = WebDriver.FindElement(By.Id("to"));
var fromval = new SelectElement(fromitem); //Error occurs
var toval = new SelectElement(toitem);
fromval.SelectByText("USD - US Dollar");
toval.SelectByText("INR - Indian Rupee");
WebDriver.FindElement(By.LinkText("Currency Calculator")).Click();
var curval = WebDriver.FindElement(By.CssSelector("span.ccOutputRslt")).GetAttribute("Value");
var expectedValue = 61.456825;
Thread.Sleep(900);
Assert.AreEqual(expectedValue, curval.Trim());
}
}
}

The SelectElement class can only be used with actual HTML <select> elements. In the page you provided, the element in question is an <input> element, with functionality added through CSS and JavaScript to enable it to act like a drop-down list. As such, attempting to use it with the SelectElement class will throw an exception indicating that the element is not of the correct type.
The "File does not exist" error message is a red herring. It's only there because NUnit is trying to show you the line of source code where the exception is thrown, which is a part of the WebDriver source code. The exception thrown by that line of code should be displayed somewhere within NUnit, which should contain the appropriate informational message.

Related

how slove this error c# selenium find element locator

in need scraping data from this web site i used C# using selenium When extracting data from the element, I run into a problem that it does not find id and classname and Xpath Knowing that I have extracted the parameters of the element
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
namespace Selenium_Automation
{
class Program
{
static void Main(string[] args)
{
IWebDriver driver = new ChromeDriver("chromedriver.exe");
// This will open up the URL
driver.Url = "https://www.olx.com.eg/";
//driver.FindElement(By.Name("q")).SendKeys("PHP");
//driver.FindElement(By.ClassName("gLFyf")).SendKeys("mysql");
driver.FindElement(By.XPath("//*[#id=\"body-wrapper\"]/div/header/div/div[4]/div/button/span")).Click();
driver.FindElement(By.XPath("//*[#id=\"strat-strat-dialog-545-container\"]/div/div/div/div/div[2]/div[2]/button[4]")).Click();
}
}
}

C# Automation Edge Browser - using Edge Driver - auto testing program - Failure: No matching capabilities found (SessionNotCreated)

Greetings stackoverflow community,
I am trying to compile and run the programcode from this website:
https://social.msdn.microsoft.com/Forums/en-US/7bdafd2a-be91-4f4f-a33d-6bea2f889e09/c-sample-for-automating-ms-edge-chromium-browser-using-edge-web-driver
I followed all the instructions listed in the link and set my paths were I wanted them.
The program and the edge driver starts running, but then an error appears.
"An error exeption "System.InvalidOperationException" appeared in WebDriver.dll.
Further Inforamtion: session not created: No matching capabilities found (SessionNotCreated)"
This is the code from my program, more or less copied from the link above:
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.Edge;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Support.UI;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
var anaheimService = ChromeDriverService.CreateDefaultService(#"C:\edgedriver_win64", "msedgedriver.exe");
// user need to pass the driver path here....
var anaheimOptions = new ChromeOptions
{
// user need to pass the location of new edge app here....
BinaryLocation = #"
C: \Program Files(x86)\Microsoft\Edge\Application\msedge.exe "
};
IWebDriver driver = new ChromeDriver(anaheimService, anaheimOptions); -- error appears at this line
driver.Navigate().GoToUrl("https: //google.com/");
Console.WriteLine(driver.Title.ToString());
driver.Close();
}
}
}
I would really appreciate your help!
Best Regards
Max
The article you refer to is a bit out of date. Now we don't need to use ChromeDriver to automate Edge. You can refer to the official doc about how to use WebDriver to automate Microsoft Edge.
I recommend using Selenium 4. Here I install Selenium 4.1.0 NuGet package and the sample C# code is like below:
using System;
using OpenQA.Selenium.Edge;
namespace WebDriverTest
{
class Program
{
static void Main(string[] args)
{
var options = new EdgeOptions();
options.BinaryLocation = #"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe";
var driver = new EdgeDriver(#"C:\edgedriver_win64", options);
driver.Navigate().GoToUrl("https://www.google.com");
Console.WriteLine(driver.Title.ToString());
driver.Close();
}
}
}

I facing problem open dropdown select option on click in c# using selenium

I am trying to open the Register page from my account.
The UI developer used the bootstrap code. Bootstrap developers have added the JS function on click.
and when I run this code then show error "OpenQA.Selenium.ElementClickInterceptedException has been thrown
element click intercepted: Element ... is not clickable at point (984, 50). Other element would receive the click: ...
(Session info: chrome=77.0.3865.120)"
Attached Screenshot link:
https://monosnap.com/file/1z5PYCFBHfcXtkWJWVMi4SeejUXXOf
https://monosnap.com/file/hdq3194312RCnvc6GdQXLLVqtoezNJ
This my code
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
namespace XTSeleniumtest
{
class MainClass
{
public static void Main(string[] args)
{
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("http://freshpicksdev.isrv.tech/");
driver.FindElement(By.CssSelector("div.modal-header .close")).Click();
driver.FindElement(By.XPath("//a[#id='navbarDropdown']/u")).Click();
}
}
}
**`
> strong text
`**
You can wait until the element is visible.
driver.FindElement(By.XPath("//a[#id='navbarDropdown']/u"));
If still the issue exists please post the DOM structure of your web page,it may be related to interacting with a wrong element.
Try the below code. You would need to add reference for "SeleniumExtras.WaitHelpers" from nuget
class MainClass
{
public static void Main(string[] args)
{
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("http://freshpicksdev.isrv.tech/");
driver.FindElement(By.CssSelector("div.modal-header .close")).Click();
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.IgnoreExceptionTypes(typeof(NoSuchElementException));
wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(By.XPath("//u[contains(text(),'My Account')]")));
driver.FindElement(By.XPath("//u[contains(text(),'My Account')]")).Click();
driver.FindElement(By.XPath("//a[text()='Register']")).Click();
}
}

Selenium c# find element select element exception

I'm fairly new to c# and writing a simple selenium code which will go onto www.asos.com. I then click on the country on the right hand side by finding the xpath. When I click on the country I want to change the country to 'india' and the currency to 'USD', and then select my preference.
I'm getting exception for driver.FindElement, SelectElement, SelectByValue previously when using java I wasn't getting these exceptions.
Exception I see for driver = The name driver doesn't exist in the current context
SelectElement = the type or namespace name 'SelectElement' could not be found
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;
namespace Exercise1
{
class Program
{
static void Main(string[] args)
{
IWebDriver webDriver = new ChromeDriver(#"Path to my chrome driver defined here");
webDriver.Navigate().GoToUrl("www.asos.com");
driver.FindElement(By.XPath("//*[#id="chrome - header"]/header/div[2]/div/ul/li[3]/div/button')]")).Click();
var country = driver.FindElement(By.Id("country"));
var select_country = new SelectElement(country);
select_country = SelectByValue("India");
var currency = driver.FindElement(By.Id("currency"));
var select_currency = new SelectElement(currency);
select_currency = SelectByValue("$ USD");
driver.FindElement(By.XPath("//*[#id="chrome - header"]/header/div[5]/div[2]/div/section/form/div[3]/button")).Click();
}
}
driver.FindElement(By.XPath("//*[#id="chrome - header"]/header/div[5]/div[2]/div/section/form/div[3]/button")).Click();
The erro in quotation marks, try:
driver.FindElement(By.XPath("//*[#id='chrome - header']/header/div[5]/div[2]/div/section/form/div[3]/button")).Click();
Use the value of the option in the html. Also call the select on the select element. For example:
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;
namespace Exercise1
{
class Program
{
static void Main(string[] args)
{
IWebDriver driver= new ChromeDriver(#"Path to my chrome driver defined here");
driver.Navigate().GoToUrl("www.asos.com");
driver.FindElement(By.XPath("//*[#id='chrome - header']/header/div[2]/div/ul/li[3]/div/button')]")).Click();
var country = driver.FindElement(By.Id("country"));
var select_country = new SelectElement(country);
select_country.SelectByValue("IN");
var currency = driver.FindElement(By.Id("currency"));
var select_currency = new SelectElement(currency);
select_currency.SelectByValue("2");
driver.FindElement(By.XPath("//*[#id='chrome - header']/header/div[5]/div[2]/div/section/form/div[3]/button")).Click();
}
}
You may also want to throw in some waits to give the page time to load.

A simple code in Selenium doesn't work

So, I have this simple code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.PhantomJS;
class Program
{
static void Main(string[] args)
{
IWebDriver driver = new PhantomJSDriver();
driver.Navigate().GoToUrl("http://someurl");
try
{
IWebElement search = driver.FindElement(By.XPath("//*[#id=comboInput']"));
search.SendKeys(".");
IWebElement searchtext = driver.FindElement(By.XPath("//*[#id='itext']"));
searchtext.SendKeys(".");
IWebElement searchsend = driver.FindElement(By.XPath("//*[#id='buttonArea']/center/table/tbody/tr/td[2]/a")).Click();
Console.WriteLine("Did it");
}
catch
{
Console.WriteLine("Nope");
}
}
}
I have no idea what's wrong with this code. The error is :
cannot implicitly convert type 'void' to 'OpenQA.Selenium.Iwebelement'
And the problem is with IWebDriver driver
Note : If this is a stupid question and I'm missing some kind of obvious fix, let me know. But I honestly can't figure out what's wrong with this.
The problem is with this line:
IWebElement searchsend = driver.FindElement(By.XPath("//*[#id='buttonArea']/center/table/tbody/tr/td[2]/a")).Click();
You're assigning the result to searchsend but Click 'returns' void.

Categories