gpt4 book ai didi

java - 使用 selenium webdriver 选择按钮时出现问题

转载 作者:太空宇宙 更新时间:2023-11-04 09:27:07 26 4
gpt4 key购买 nike

我正在尝试使用 seleninum webdriver 和 java 的不同方法来选择特定按钮,但不幸的是,没有任何效果。

当我使用 Selenium IDE 进行测试时,它可以正常工作。例如,我复制了相同的 xpath,但是当我尝试在我的 java 应用程序中测试时,没有任何效果。我尝试使用不同的方式,By.cssSelector 和 By.path。

这是我的 html:

<section class="fd-section"><fd-action-bar><div class="fd-action-bar"><fd-action-bar-header class="fd-action-bar__header"><fd-action-bar-title><h1 class="fd-action-bar__title"> Applications </h1></fd-action-bar-title></fd-action-bar-header><fd-action-bar-actions class="fd-action-bar__actions"><y-list-search _nghost-c4="" hidden=""><!----><!----><div _ngcontent-c4="" clickoutsideevents="click,mousedown" excludebeforeclick="true" class="ng-star-inserted"><!----><button _ngcontent-c4="" fd-button="" class="fd-button xyz-icon--search fd-button--light ng-star-inserted"></button><!----></div></y-list-search><y-list-filter _nghost-c5="" hidden=""><!----></y-list-filter><!----><button class="open-create-namespace-modal fd-button xyz-icon--add ng-star-inserted" fd-button=""> Create Application </button></fd-action-bar-actions></div></fd-action-bar></section>

我需要选择带有文本“创建应用程序”的按钮。

当我使用 Selenium IDE 创建测试时,此按钮的 xpath 是:

//按钮[contains(.,'创建应用程序')]

基本上,我的java代码是:

public WebElement wElement;wElement = driver.findElement(By.xpath("//button[contains(.,' Create Application')]"));wElement.click();

这是异常消息:

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//button[contains(.,' Create Application')]"}  (Session info: chrome=76.0.3809.100)  (Driver info: chromedriver=72.0.3626.69 (3c16f8a135abc0d4da2dff33804db79b849a7c38),platform=Mac OS X 10.14.6 x86_64) (WARNING: The server did not provide any stacktrace information)Command duration or timeout: 0 millisecondsFor documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.htmlBuild info: version: '3.14.0', revision: 'aacccce0', time: '2018-08-02T20:19:58.91Z'System info: host: 'C02WW0BZHTD8', ip: 'fe80:0:0:0:8f6:17e1:1a28:1e23%en0', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.14.6', java.version: '1.8.0_171'Driver info: org.openqa.selenium.chrome.ChromeDriverCapabilities {acceptInsecureCerts: false, acceptSslCerts: false, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 72.0.3626.69 (3c16f8a135abc..., userDataDir: /var/folders/2r/99nyn7t16cz...}, cssSelectorsEnabled: true, databaseEnabled: false, goog:chromeOptions: {debuggerAddress: localhost:60374}, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: MAC, platformName: MAC, proxy: Proxy(), rotatable: false, setWindowRect: true, takesHeapSnapshot: true, takesScreenshot: true, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unexpectedAlertBehaviour: ignore, unhandledPromptBehavior: ignore, version: 76.0.3809.100, webStorageEnabled: true}Session ID: b2341899cd9b62b0169b02371aaa3018*** Element info: {Using=xpath, value=//button[contains(.,' Create Application')]}

最佳答案

执行这段代码时,按钮是否已加载到页面中?

{implicit: 0, pageLoad: 300000, script: 30000},表明驱动程序不会隐式等待查找任何元素。即如果该元素不可用,它将立即抛出异常。

尝试driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);,然后再尝试查找按钮。

还可以尝试使用以下定位器 -

  1. //按钮[contains(.,'创建应用程序')]
  2. //按钮[contains(text(),'创建应用程序')]

如果以上方法均无效,请提供一个 URL(如果是公开的)

还要检查按钮是否在框架内。

关于java - 使用 selenium webdriver 选择按钮时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57543844/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com