我正在尝试使用带有Java的seleninum webdriver选择特定按钮的不同方法,但不幸的是,没有任何效果。
当我使用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为:
// button [包含(。,'创建应用程序')]
基本上,我的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 milliseconds For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html Build 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.ChromeDriver Capabilities {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')]}
答案 0 :(得分:0)
执行这段代码时,按钮是否已加载到页面中?
{implicit: 0, pageLoad: 300000, script: 30000}
,建议驱动程序不会隐式等待查找任何元素。也就是说,如果该元素不可用,它将立即引发异常。
在尝试找到按钮之前,先尝试driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
。
也尝试使用以下定位符-
//button[contains(.,'Create Application')]
//button[contains(text(),'Create Application')]
如果以上方法均无效,请提供一个URL(如果它是公开的)
还要检查按钮是否在框架内。
答案 1 :(得分:0)
在这种情况下,您可以使用JavascriptExecutor界面,然后尝试单击按钮。
WebElement element = driver.findElement(By.xpath("//button[contains(text(),'Create Application')]"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
还可以尝试使用WebDriverWait
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(text(),'Create Application')]"))).click();
答案 2 :(得分:0)
期望的元素是Angular元素,因此要定位必须为elementToBeClickable()
引入 WebDriverWait 的元素,可以使用以下任何一个{{3} }:
cssSelector
:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("fd-action-bar-actions.fd-action-bar__actions button.open-create-namespace-modal.fd-button.xyz-icon--add.ng-star-inserted"))).click();
xpath
:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@class='open-create-namespace-modal fd-button xyz-icon--add ng-star-inserted' and contains(., 'Create Application')]"))).click();
确保:
在这里您可以找到有关ChromeDriver v76.0 release notes的详细讨论