Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

click() on css Selector not working in Selenium webdriver

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;
}
like image 802
user1769790 Avatar asked Oct 23 '12 23:10

user1769790


People also ask

Why click is not working in Selenium?

We can list the most common reasons for click problems as being one of the following: Wrong web element locations. The existence of a web element that obscures the web element that we want to click. The Selenium WebDriver works much faster than the response of the application.

How do you click on an element using CSS selector in Selenium?

Type “css=input[type='submit']” (locator value) in Selenium IDE. Click on the Find Button. The “Sign in” button will be highlighted, verifying the locator value. Attribute: Used to create the CSS Selector.

What is click () method in Selenium?

How to use the Selenium Click command. To put it in simple words, the click command emulates a click operation for a link, button, checkbox or radio button. In Selenium Webdriver, execute click after finding an element. In SeleniumIDE, the recorder will do the identifying, and the command is simply click.

What is limitation of CSS selector in Selenium?

One of the major drawbacks of the CSS selectors in Selenium is traversing the DOM is not possible with CSS as that of XPath. We can navigate from child to parent and parent to child using XPath. But we can only navigate from parent to child by CSS Selectors in Selenium.


2 Answers

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.

like image 120
eugene.polschikov Avatar answered Oct 12 '22 06:10

eugene.polschikov


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

like image 20
Jotish Avatar answered Oct 12 '22 06:10

Jotish