Reputation: 2825
Ok I am writing a simple code in Selenium Web Driver. What it does is:
I am using windows 8 - 64 bit and Visual Studio 2013. Browser is Firefox.
Here is the code I wrote:
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.IE;
namespace WebDriverDemo
{
class Program
{
static void Main(string[] args)
{
IWebDriver driver = new FirefoxDriver();
driver.Url = "http://google.com";
var searchBox = driver.FindElement(By.Id("gbqfq"));
searchBox.SendKeys("abc");
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromMinutes(1));
var images = driver.FindElements(By.ClassName("q qs"))[0];
images.Click();
}
}
}
But I am getting an exception on the second last line of the code. Here is the exception:
Here is the Inspect Element result:
Upvotes: 1
Views: 638
Reputation: 16201
And, the issue is compound class. Currently selenium
does not support this. You can use the cssSelector
on the other hand to avoid this issue.
.q.qs
Note .
before each class and see my answer related to this question here
Complete code as per OP's update:
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.IE;
namespace WebDriverDemo
{
class Program
{
static void Main(string[] args)
{
IWebDriver driver = new FirefoxDriver();
driver.Url = "http://google.com";
var searchBox = driver.FindElement(By.Id("gbqfq"));
searchBox.SendKeys("abc");
//The following line is missing that is mandatory.
driver.FindElement(By.Name("btnG")).Click();
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromMinutes(1));
var images = driver.FindElements(By.CssSelector(".q.qs"))[0];
images.Click();
}
}
}
Upvotes: 1
Reputation: 89
Using CSSSelector:
var images = driver.findElement(By.cssSelector(".q.qs"));
images.Click();
Using LinkText :
var images = driver.findElement(By.linkText("Images"));
images.Click();
Using Xpath :
var images = driver.findElement(By.xpath(".//*[@class='q qs' and .='Images']"));
images.Click();
Upvotes: 0
Reputation: 27486
The exception message tells you exactly what the problem is. You cannot use multiple, or "compound," class names when using By.ClassName
. A single class name cannot contain a space. If you want to use multiple class names, use By.CssSelector
.
Upvotes: 3