【发布时间】:2019-08-21 14:03:33
【问题描述】:
我正在尝试使用 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代码是:
公共WebElement wElement; wElement = driver.findElement(By.xpath("//button[contains(.,'创建应用程序')]")); wElement.click();这是异常消息:
org.openqa.selenium.NoSuchElementException: 没有这样的元素: 无法定位元素: {"method":"xpath","selector":"//button[contains(.,'Create Application')]"} (会话信息:chrome=76.0.3809.100) (驱动程序信息:chromedriver=72.0.3626.69 (3c16f8a135abc0d4da2dff33804db79b849a7c38),platform=Mac OS X 10.14.6 x86_64)(警告:服务器未提供任何堆栈跟踪信息) 命令持续时间或超时:0 毫秒 有关此错误的文档,请访问:http://seleniumhq.org/exceptions/no_such_element.html 构建信息:版本:'3.14.0',修订:'aacccce0',时间:'2018-08-02T20:19:58.91Z' 系统信息:主机:'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' 驱动程序信息:org.openqa.selenium.chrome.ChromeDriver 功能 {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 :正常,平台:MAC,平台名称:MAC,代理:Proxy(),可旋转:false,setWindowRect:true,takesHeapSnapshot:true,takeScreenshot:true,超时:{implicit:0,pageLoad:300000,脚本:30000},unexpectedAlertBehaviour :忽略,unhandledPromptBehavior:忽略,版本:76.0.3809.100,webStorageEnabled:true} 会话 ID:b2341899cd9b62b0169b02371aaa3018 *** 元素信息:{Using=xpath, value=//button[contains(.,'Create Application')]}【问题讨论】:
标签: java selenium-webdriver xpath css-selectors webdriverwait