无法使用Selenium和Java单击网站上的按钮

时间:2020-06-30 18:04:11

标签: java selenium selenium-chromedriver click runtime-error

我使用硒Java代码运行测试用例。

我的网页上有一个名为“选择资产”的按钮,xpathfinder插件将其显示为//*[@id="modalButton"],我希望单击该按钮。参见下面的快照:

enter image description here

我尝试了一些代码来使单击生效,但是没有一个单击按钮。

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

对于上述Java代码,我得到以下错误。

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)

下面是显示按钮代码的网页view source

<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>

您能建议一下什么可以帮助我单击按钮吗?

1 个答案:

答案 0 :(得分:1)

尝试使用JSExecutor单击

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

在您的情况下,Webelement应该是:

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