Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
799 views
in Technique[技术] by (71.8m points)

button - 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;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...