簡體   English   中英

使用 Selenium 和 Java 無法單擊網站上的按鈕

[英]Unable to click a button on a website using Selenium and Java

我使用 selenium java 代碼運行測試用例。

我的網頁有一個名為“Select Assets”的按鈕,xpathfinder 插件顯示為//*[@id="modalButton"]我希望單擊它。 請參閱下面的快照:

在此處輸入圖像描述

我嘗試了一些代碼來讓點擊工作,但他們都沒有點擊按鈕。

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>

你能建議什么可以幫助我點擊按鈕嗎?

嘗試用 JSExecutor 點擊

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

在您的情況下,Webelement 應該是:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM