In Selenium WebDriver, findElement By cssSelector
is not working or not accessible.
I'm trying to press a button. There are many buttons on the page, with same id
and tags, automatically generated by the script of previous page. So I can't access it using id
or any other tag. So I've decided to go for cssSelector
.
Here's my code:
import java.util.Random;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class mrc {
public static void main(String[] args) {
System.out.flush();
//Create firefoxDriver Object.
System.setProperty("webdriver.gecko.driver", "C:\\eclipse\\geckodriver.exe");
WebDriver driver=new FirefoxDriver();
// LOGIN
driver.get("http://ec2#####-west-2.compute.amazonaws.com/login");
driver.findElement(By.xpath("/html/body/div[3]/div[1]/div[2]/div/form/div[2]/div/input")).sendKeys("admin@admin.com");
driver.findElement(By.xpath("/html/body/div[3]/div[1]/div[2]/div/form/div[3]/div/input")).sendKeys("admin");
driver.findElement(By.xpath("/html/body/div[3]/div[1]/div[2]/div/form/button[1]")).click();
// JUDGEPANEL
// Navigate to judgepanel
driver.navigate().to("http://ec2#####-west-2.compute.amazonaws.com/judgepanel");
// select event
Select drpdwn = new Select(driver.findElement(By.id("selector")));
drpdwn.selectByValue("70");
//drpdwn.selectByIndex(drpdwn.getOptions().size()-1);
driver.findElement(By.xpath("/html/body/div[3]/div[2]/div[2]/div[1]/div/div[4]/div[2]/button")).click();
// UPTO THIS PART IT WORKS FINE
// enter scores
driver.findElement(By.cssSelector("button#disablerun.btn.btn-primary")).click();
System.out.println("Button 1 Pressed");
}
}
The error is:
1525438082224 geckodriver INFO geckodriver 0.20.1
1525438082242 geckodriver INFO Listening on 127.0.0.1:39642
1525438083119 mozrunner::runner INFO Running command: "C:\\Program Files\\Mozilla Firefox\\firefox.exe" "-marionette" "-profile" "C:\\Users\\karan\\AppData\\Local\\Temp\\rust_mozprofile.fUb9UrnMUqq8"
Unable to read VR Path Registry from C:\Users\karan\AppData\Local\openvr\openvrpaths.vrpath
1525438084292 Marionette INFO Enabled via --marionette
Unable to read VR Path Registry from C:\Users\karan\AppData\Local\openvr\openvrpaths.vrpath
Unable to read VR Path Registry from C:\Users\karan\AppData\Local\openvr\openvrpaths.vrpath
Unable to read VR Path Registry from C:\Users\karan\AppData\Local\openvr\openvrpaths.vrpath
1525438086674 Marionette INFO Listening on port 63161
1525438086758 Marionette WARN TLS certificate errors will be ignored for this session
May 04, 2018 6:18:07 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Unable to read VR Path Registry from C:\Users\karan\AppData\Local\openvr\openvrpaths.vrpath
JavaScript warning: http://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.2/jquery.min.js, line 1: Using //@ to indicate sourceMappingURL pragmas is deprecated. Use //# instead
JavaScript error: http://ec2#####-west-2.compute.amazonaws.com/js/all.js, line 5831: TypeError: gapi.signin is undefined
JavaScript warning: http://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.2/jquery.min.js, line 1: Using //@ to indicate sourceMappingURL pragmas is deprecated. Use //# instead
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: button#disablerun.btn.btn-primary
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.11.0', revision: 'e59cfb3', time: '2018-03-11T20:33:08.638Z'
System info: host: 'KARAN-VAIO', ip: '*.*.*.*', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '10'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities {acceptInsecureCerts: true, browserName: firefox, browserVersion: 59.0.2, javascriptEnabled: true, moz:accessibilityChecks: false, moz:headless: false, moz:processID: 1584, moz:profile: C:\Users\karan\AppData\Loca..., moz:useNonSpecCompliantPointerOrigin: false, moz:webdriverClick: true, pageLoadStrategy: normal, platform: XP, platformName: XP, platformVersion: 10.0, rotatable: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}}
Session ID: b3b62974-ae5d-4885-956f-5ab2edf03dc9
*** Element info: {Using=css selector, value=button#disablerun.btn.btn-primary}
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.base/java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:545)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:319)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByCssSelector(RemoteWebDriver.java:413)
at org.openqa.selenium.By$ByCssSelector.findElement(By.java:430)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:311)
at mrc.main(mrc.java:51)
I've already tried xPath. It's not working too. How to resolve this error?
HTML for button is:
<div ng-if="!isLocked && !Userlock" class="row ng-scope" ng-repeat="entrance in entranceList">
<div class="col-xs-12 col-sm-12">
<div class="row">
<div class="col-xs-12 col-sm-3 ng-binding">
Roma G
</div>
<div class="col-xs-12 col-sm-3">
<!-- run1 <input type="number" placeholder="Run 1 score" ng-model=entrance.run1> -->
run1 <b><span ng-bind="entrance.run1" class="ng-binding"></span></b>
</div>
<div class="col-xs-12 col-sm-3">
<!-- run2 <input type="number" placeholder="Run 2 score" ng-model=entrance.run2> -->
run2 <b><span ng-bind="entrance.run2" class="ng-binding"></span></b>
</div>
<div class="col-xs-12 col-sm-3">
<!-- <button class="button" ng-click=saveEntraceScore(entrance)> Save</button> -->
<button id="disablerun" class="btn btn-primary" ng-click="EditEntraceScore(entrance)"> Edit
<!-- <div ng-if="isbattleTree === true" class="">
<i class="fa fa-lock"></i></div> -->
</button>
</div>
</div>
<hr>
<div class="col-xs-12 col-sm-6">
</div>
</div>
</div>