1

I am trying of different ways to select a specific button using seleninum webdriver with java but unfortunately, nothing is working.

When I tested using the Selenium IDE is working. I copied the same xpath, for example, but when I try to test in my java application nothing is working. I tried using different ways, By.cssSelector and By.path.

This is my 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>

I need to select the button with the text " Create Application ".

When I created a test using Selenium IDE the xpath for this button is:

//button[contains(.,' Create Application')]

Basically, my java code is:

public WebElement wElement;

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

wElement.click();

This is the exception message:

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')]}
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Marcelo
  • 13
  • 4

3 Answers3

0

When this piece of code is being executed, is the button loaded into the page already?

{implicit: 0, pageLoad: 300000, script: 30000}, suggests that the driver will not implicitly wait for finding any elements. i.e. if the element is not available, it will throw the exception immediately.

Try driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS); before you attempt to find the button.

Also try using below locators -

  1. //button[contains(.,'Create Application')]
  2. //button[contains(text(),'Create Application')]

If none of the above works, please can you provide a URL (if it is public)

Also check whether the button is inside a frame.

jo_fletcher
  • 23
  • 1
  • 11
  • Hello Sachin, thanks a lot for your reply! Yes, the button is available when the page is loaded. Anyway, I added "driver.manage()..." and I followed your tips and unfortunately is not working yet. This is very strange because I am following the same process with other buttons and this is working, just with this specific button is not working. – Marcelo Aug 18 '19 at 11:30
0

You can use JavascriptExecutor interface in this case and try to click the button.

WebElement element = driver.findElement(By.xpath("//button[contains(text(),'Create Application')]"));
JavascriptExecutor executor = (JavascriptExecutor)driver; 
executor.executeScript("arguments[0].click();", element);

Also give a try with WebDriverWait

new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(text(),'Create Application')]"))).click();
  • I tried using both cases but unfortunately still not working. I tried with two different syntaxes to By.xpath: 1. //button[contains(.,' Create Application')] and 2. //button[contains(text(),' Create Application ')] This is very strange because when I execute my test using Selenium IDE, the button is clicked with the syntax 2. When I use the same syntax inside my java source code doesn't work. – Marcelo Aug 18 '19 at 20:50
  • I received this exception: org.openqa.selenium.TimeoutException: Expected condition failed: waiting for element to be clickable: By.xpath: //button[contains(.,' Create Application')] (tried for 20 second(s) with 500 milliseconds interval) – Marcelo Aug 18 '19 at 20:50
0

The desired element is an Angular element so to locate the element you have to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:

  • 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();
    

Additional Considerations

Ensure that:

Here you can find a detailed discussion on NoSuchElementException, Selenium unable to locate element

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Is working now! :) To solve I needed to use this because the button was inside an iframe. WebElement iFrame= driver.findElement(By.tagName("iframe")); driver.switchTo().frame(iFrame); – Marcelo Aug 19 '19 at 17:23
  • @Marcelo The HTML you provided was missing the iframe tag. Even the existing HTML didn't sound like being within an iframe. Hence it was tough to guess. Glad your question was resolved. If this answer have catered to your question you can accept the answer by clicking on the hollow check mark beside the answer which is just below the votedown arrow so the check mark turns green. – undetected Selenium Aug 19 '19 at 18:01