Ashar
Ashar

Reputation: 3065

Unable to click a button on a website using Selenium and Java

I use selenium java code to run a test case.

My webpage has a button called "Select Assets" which xpathfinder plugin shows as //*[@id="modalButton"] which I wish to click. See snapshot below:

enter image description here

I tried a couple of code to get the click working but none of them clicks the button.

driver.findElement(By.xpath("//*[@id=modalButton]")).click();
driver.findElement(By.id("modalButton")).click();

I get the below error for the above Java code.

Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id=modalButton]"}
  (Session info: chrome=75.0.3770.100)
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.141.0', revision: '2ecb7d9a', time: '2018-10-31T20:09:30'
System info: host: 'VMINITSERMAPRAP', ip: '10.9.140.15', os.name: 'Windows Server 2016', os.arch: 'x86', os.version: '10.0', java.version: '1.8.0_181'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 75.0.3770.100, chrome: {chromedriverVersion: 75.0.3770.140 (2d9f97485c7b..., userDataDir: C:\Usersxmwiis\AppData\Lo...}, goog:chromeOptions: {debuggerAddress: localhost:54846}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
Session ID: 9cf6823eed426e0cc3d457cfe146bbef
*** Element info: {Using=xpath, value=//*[@id=modalButton]}
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)

Below is the view source of the webpage showing the button code.

<div class="help-block"></div>
</div>                    </div>
                    
                    
                    <div class="row">
                        <div class="col-lg-6">
                            <h2>List of Server/Devices to be tested</h2>
                        </div>

                        <div class="col-lg-3 col-lg-offset-3">
                            <button type="button" id="modalButton" class="btn btn-primary" value="/synvm/basic/web/index.php?r=security-test-request0.000000map-asset&amp;request_type=ConfigReviewReq&amp;param=CR&amp;series_id=0" style="float:right;">Select Assets</button>                        </div>
                    </div>
                    <br>

Can you please suggest what will help me click the button?

Upvotes: 1

Views: 111

Answers (1)

Norayr Sargsyan
Norayr Sargsyan

Reputation: 1868

Try to click with JSExecutor

public void clickWithJS(WebElement element) {
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("arguments[0].click();", element);
    }

In your case Webelement should be:

WebElement element = driver.findElement(By.id("modalButton"));

Upvotes: 1

Related Questions