11

HTML

<input class="button" type="button" onclick="$.reload('results')" value="Search">

I don't have an id or name for this . Hence am writing

FirefoxDriver driver = new FirefoxDriver();
driver.get("http://....");
driver.findElement(By.cssSelector("input[value=Search]")).click();

But click() is not happening.

Tried driver.findElement(By.cssSelector(".button[value=Search]")).click();

Tried value='Search' (single quotes).

these Selectors are working in

.button[value=Search] {
padding: 10px;
}
input[value=Search] {
padding: 10px;
}
6
  • Looks ok. Are you sure the driver.get() loads the correct page ? Are you using any JS Frameworks ? Maybe you have to wait for some ajax calls to finish. Commented Oct 24, 2012 at 7:05
  • Does it find it and just not click it? Throw an exceptions? Commented Oct 24, 2012 at 8:34
  • i have luck with driver.get() loading my page. Commented Oct 24, 2012 at 15:51
  • @Arran am stuck with that question; am not sure if it is finding my 'Search' button Commented Oct 24, 2012 at 15:51
  • If using Firefox, use the IDE with your selector and verifies that the IDE actually sees something. Commented Oct 24, 2012 at 16:03

4 Answers 4

2

i would inject piece of js to be confident in resolving this issue:

first of all locate element using DOM (verify in firebug): locating

public void jsClick(){

        JavascriptExecutor js = (JavascriptExecutor) driver;
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("document.getElementsByTagName('button')[0].click();");
        js.executeScript(stringBuilder.toString());
    }
jsClick();

from the retrospective of your element it be like:

....
stringBuilder.append("document.getElementsByTagName('input')[0].click();");
....

Please, note: document.getElementsByTagName('input') returns you an array of DOM elements. And indexing it properly e.g. document.getElementsByTagName('input')[0], document.getElementsByTagName('input')1, document.getElementsByTagName('input')[2].... ,etc you will be able to locate your element.

Hope this helps you. Regards.

Sign up to request clarification or add additional context in comments.

7 Comments

This helped to find the element, but click() not happening. driver not executing the click(); is it because i have my onclick event to this input?
well, try to locate your error step by step. open your page in firefox, open firebug, open console in firebug and simply try to execute following piece of code in console: document.getElementsByTagName('button')[0].click() and see what happens
javascript 'click' approach is robust and always works for me. If nothing happens - you should check carefully that you've found web element properly
'Run' on document.getElementsByTagName('button')[5].click(); working absolutely fine. I just have this public void jsClick(){ JavascriptExecutor js = (JavascriptExecutor) driver; StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append("document.getElementsByTagName('button')[0].click();"); js.executeScript(stringBuilder.toString()); } jsClick();
nice) so as you can see in this public jsClick you mention improper arg order. change 'document.getElementsByTagName('button')[0]' to 'document.getElementsByTagName('button')[5]'. So as I can see it be: public void jsClick(){ JavascriptExecutor js = (JavascriptExecutor) driver; StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append("document.getElementsByTagName('button')[5].click();"); js.executeScript(stringBuilder.toString()); } jsClick();
|
1

Please use the below code.

driver.findElement(By.cssSelector("input[value=\"Search\"]")).click();

It works for me. And make sure that the name is "Search", coz it is case sensitive.

Thanks

Comments

0

Are you sure that using this CSS-selector (input[value=Search]) on your page you have only one result?

Comments

0

single quotes are missing in your code, the [value=Search] should be replaced with [value='Search'].

first you have to check if the selector u are using will work or not..

If you are using chrome or FF,you can follow these steps,

  1. go to the page where button (to be clicked) is present,

  2. open web console and type in the following and click enter..

    $("input[value='Search']")

or

$("input[value='Search'][type='button']")

or

$("input[value='Search'][type='button'].button")

you will get a list of elements which can be accessed using this selector, if that list contains only one element (button that you want to click), then this selector is valid for your use..otherwise u'l have to try some other selector..

If any of the above selector is valid,u'l have to change your code accordingly..

driver.findElement(By.cssSelector("input[value='Search'][type='button'].button")).click();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.