0

Trying to find an xpath expression to use in:

wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("XPATH HERE"))).click();

The element says "Invite Users" on the page and i need to be able to click on it

I need to find the element /a[@id='inviteUsers_8ef17ba4-b739-4fb6-8198-3862ea84c381_toggle'] but the problem is the characters after "inviteUsers_" is dynamically generated

I have already tried these:

"//*[contains(.,'Invite Users')]";
"//a[contains(.,'Invite Users')]";

And these give NoSuchElement exceptions.

This is the complete XPATH:

/html/body/div[@class='col-xs-10 col-xs-offset-2 main']/fieldset[@class='form-horizontal']/div[@id='roles']/div[@id='entitlements']/div[@class='panel panel-default '][3]/div[@id='service_8ef17ba4-b739-4fb6-8198-3862ea84c381']/div[@class='panel-body']/div[@class='panel panel-default'][1]/div[@class='panel-heading']/h4[@class='panel-title']/a[@id='inviteUsers_8ef17ba4-b739-4fb6-8198-3862ea84c381_toggle'] 
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
shanwar
  • 319
  • 1
  • 2
  • 19
  • Did you find your answer? If not, please post the relevant HTML for the link you are trying to click. – JeffC Jul 11 '16 at 04:23

2 Answers2

1

You can solve it with starts-with():

//a[starts-with(@id, "inviteUsers_")]
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Still failed to find the element unfortunately :( Am i missing something from the xpath ? Maybe i need to get a specific div before trying to access the element ? – shanwar Jun 10 '16 at 15:27
  • @shanwar no, if it fails like that, it would fail with a specific `div` as well. Is this a public page and you can share a link to it? Thanks. – alecxe Jun 10 '16 at 15:29
  • Unable to do that sorry. Confidential page under development. Could it be that this element appears in sort of like a drop down area after a subsequent click of a previous element so it fails? I would think the wait (7sec before timeout) takes care of that... – shanwar Jun 10 '16 at 15:34
  • @shanwar could be all sorts of things happening, difficult to say what is it exactly. Could you post how this element looks in the HTML? Also, make sure you make all the necessary actions for that element to appear. – alecxe Jun 10 '16 at 15:42
  • Watching Selenium execute the test(with debugger) I can see that it execute until the point where it tries to find that element – shanwar Jun 10 '16 at 15:47
  • @shanwar thanks. The XPath expression itself is correct and should match the element. What happens when you use the `wait.until()` to wait for it? Do you get an error? Which one? – alecxe Jun 10 '16 at 16:02
  • This is the error i get: org.openqa.selenium.TimeoutException: Timed out after 7 seconds waiting for visibility of element located by By.xpath: //a[starts-with(@id, "inviteUsers_")] – shanwar Jun 10 '16 at 16:04
  • @shanwar just a quick check - the element is not inside an iframe, right? – alecxe Jun 10 '16 at 16:08
  • Negative. Just a inside a panel on the same page – shanwar Jun 10 '16 at 16:11
  • @shanwar alright. What happens if you replace `visibilityOfElementLocated` with `presenceOfElementLocated`? – alecxe Jun 10 '16 at 16:14
  • "org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with" Weird since I can see the element physically appear on the page before this step – shanwar Jun 10 '16 at 16:21
  • @shanwar awesome, we've got progress. So, we locate the element correctly, but it is invisible for selenium. Multiple things you can do to solve the element not visible error, please see this related topics: http://stackoverflow.com/questions/36871071/click-function-isnt-working-in-protractor-scripts/36940011#36940011, http://stackoverflow.com/a/37735942/771848, http://www.blaiseliu.com/protractor-error-element-is-not-clickable-at-point-xx-xx/. They are kind of Protractor specific, but I think you''ll be able to apply them to Java+Selenium. – alecxe Jun 10 '16 at 16:24
  • Ok as per the link I implemented the scroll into view method with a helper class. If the element is located first get location with – shanwar Jun 10 '16 at 16:37
  • This still gives a me an Element is not visible exception – shanwar Jun 10 '16 at 16:39
  • @shanwar...if you can find element but you could not `click` on it due to **element is not visible** then you should try to `click` on it using `JavaScriptExecutor` as like that :- `JavascriptExecutor executor = (JavascriptExecutor)driver; executor.executeScript("arguments[0].click()", invite);`...may be it will work – Saurabh Gaur Jun 10 '16 at 18:03
0

If this does not work try find a unique parent.

//div[contains(@id, 'service')]//a[contains(@id, 'inviteUsers')]
lauda
  • 4,153
  • 2
  • 14
  • 28