Sriram
Sriram

Reputation: 449

XPATH and CSS locators throws error for Chrome in webdriver

I am facing issues in locating element in chrome. XPath of the element looks this way:

//*[@id="signin"] (right click on the element and copy xpath)

CSS for the same:

$$("div[id='signin']")

Now I am able to locate the element in chrome when I hover over them manually. But when I try to implement the same as code it throws error.

driver.findElement(By.cssSelector("$$("div[id='signin']")).click(); - CSS
driver.findElement(By.xpath("//*[@id="signin"]")).click(); - XPATH

It throws remove argument to match 'xpath(String)' Help!!

Code :

public static WebDriver driver;


@BeforeClass
public static void start()
{
    File file = new File("D:/new/chromedriver.exe");
    System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
    driver = new ChromeDriver();
    driver.get("http://abcd.com");
}


@AfterClass
public static void close()
{
    driver.close();
}


@Test
public static void test()

    {
    driver.findElement(By.name("UserId")).sendKeys("100");
    driver.findElement(By.name("Password")).sendKeys("100");
    driver.findElement(By.xpath("//*[@id='signin']")).click();


    }

}

@NOte : the URL here is dummy. in the real time am using a proper URL. It throws the below error

FAILED: test org.openqa.selenium.NoSuchElementException: The element could not be found (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 89 milliseconds For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html Build info: version: '2.32.0', revision: '6c40c18', time: '2013-04-09 17:23:22' System info: os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0'

Upvotes: 1

Views: 2971

Answers (3)

Andrian Durlestean
Andrian Durlestean

Reputation: 1730

driver.findElement(By.cssSelector("div#signin")).click(); - CSS

driver.findElement(By.xpath("//*[@id='signin']")).click(); - Xpath

driver.findElement(By.id("signin")).click(); - Id

Upvotes: 0

Amol
Amol

Reputation: 1

From the CSS you mentioned it seems that it is div element that you trying to find out.Following should work seamlessly for this.

driver.findElement(By.xpath("//div[@id='signin']")).click();

Upvotes: 0

HemChe
HemChe

Reputation: 2337

Try enclosing the signin in single quotes instead of double quotes as shown below.

driver.findElement(By.xpath("//*[@id='signin']")).click(); - XPATH

Upvotes: 2

Related Questions