Xpath with spaces id not working

Hi there,

I have identified one link element with id=“Record Docs Latest” but now while debugging its not able to find this element and click on it with RPA.Framework.Selenium keywords.

Also, i have tried normalize-space() function in xpath as well.

Yours help on this one is appreciated.
Thanks in advance,
Amin

It is difficult to troubleshoot selectors like this :scream: You can also try with contains(). Or if this also does not work, there is option to find stable anchor element and try to use that one. If your element is on public page, maybe you can share the link? Or show element in DOM

@linkraivo I have tried contains as well - xpath://a[contains(@href,“#ts/Record%20Docs%20Latest”)]

Above is not worked, also tried @id, css options too.

@amin.patel Hi :slight_smile: So in the first message you said that you have identified the element with its @id attribute.

Have you used locator like this //a[@id="Record Docs Latest"] (assumed that element is a //a element) ?

I just tested this with very simple robot:
tasks.robot

*** Settings ***
Library  RPA.Browser.Selenium

*** Tasks ***
Minimal task
    Open Available Browser  file:////Users/teppo/repo/robot_spacelink/index.html
    Click Link    Record Docs Latest
    # These work also:
    # Click Link    //a[@id="Record Docs Latest"]
    # Click Link    //a[contains(@href, "Another File")]
    Sleep  3

index.html

<html>
    <body>
        <a id="Record Docs Latest" href="Another File.html">Another File</a>
    </body>
</html>

Another File.html

<html>
    <body>
        Victory!!!
    </body>
</html>

…and it seems to work. I was also a bit confused whether you were using href or id based locator?

@mika yes but element is a and i have used xpath://a[@id=‘Record Docs Latest’] but its not worked with Click Link keyword of RPA.Browser.Selenium !!

Is it possible that the page contains multiple elements that much that xpath? Or perhaps the element is inside an IFRAME?

Ok. As I side I recommend using Click Element keyword as that is a generic Click keyword which does not care of the element type.

What is the error message when the Click does not work ?

It might be that you are clicking too fast - the element might not be rendered yet.

I would recommended doing either

Wait Until Element Is Visible   //a[@id="Record Docs Latest"]
Click Element   //a[@id="Record Docs Latest"]

OR

Click Element When Visible //a[@id="Record Docs Latest"]

Yeah and what @Teppo said. Maybe the element is inside a frame - then you need to use Select Frame keyword (with correct frame locator) to select frame first.

I have already tried Click Element When Visible xpath://a[@id=‘Record Docs Latest’] and all options Wait Until Element Is Visible, Click Element, Click Element If Visible as well.

But all these too didnt work, so tried normalize-space(), contains() and more options.

this a element is inside section element; and i didnt see any iframe element anywhere in page !!

@Teppo I have tried all above options as well, nothing worked for me.

Below structure we had for our a element -
//section>
//ng-include>
//post-render id=“Record Docs Latest” section=“Record_Docs_Latest”>/post-render>
//div>
//a id=“Record Docs Latest” onclick=“send(this)” href=“#ts/Record%20Docs%20Latest”>See more/a>
///div>
///ng-include>
///section>

DOM of actual element

@linkraivo

Below structure we had for our a element -
//section>
//ng-include>
//post-render id=“Record Docs Latest” section=“Record_Docs_Latest”>/post-render>
//div>
//a id=“Record Docs Latest” onclick=“send(this)” href=“#ts/Record%20Docs%20Latest”>See more/a>
///div>
///ng-include>
///section>

DOM of actual element

I tested again with this HTML

<html>
    <body>
        <section>
        <ng-include>
        <post-render id="Record Docs Latest" section="Record_Docs_Latest"></post-render>
        <div>
            <!--a id="Record Docs Latest" onclick="send(this)" href="#ts/Record%20Docs%20Latest">See more</a-->
            <a id="Record Docs Latest" onclick="send(this)" href="Another File.html">See more</a>
        </div>
        </ng-include>
        </section>
    </body>
</html>

Click Link //a[@id="Record Docs Latest"] still worked. Only Click Link Record Docs Latest didn’t work as the locator matched with two elements.

@amin.patel you could test by going to the url with your browser (manually), immediately open the inspector and test the xpath //a[@id="Record Docs Latest"]. Does it find exactly one match?

Without seeing the page, the three most common problems have already been mentioned above:

  1. There is IFRAME
  2. Page has some kind of lazy loading and your target element is not there
  3. There are multiple matches

Click Link //a[@id=“Record Docs Latest”] i have tried again yesterday as suggested but did not work for me and to be correct i have used Wait Until Element Is Visible, Click Element When Visible as well.

If you

  1. Go to the url with chrome
  2. Open the Chrome’s inspector -tool
  3. Search for the xpath //a[@id=“Record Docs Latest”]

Does it find exactly 1 match?

Yes i copied Xpath from Chrome inspector-tool only using Copy → Copy Xpath option

I have also tried css:selector using Copy → Copy Selector option.
In this case, can I use Full Xpath with robocorp selenium keywords.

@amin.patel the copying does not answer to @Teppo 's question

Does it find exactly 1 match?

You can do what @Teppo suggested to do - “control +f” to activate input field on the bottom of the element tree (in the Chrome Devtools) and then copy paste your locator there — on the right side of the field it will state how many elements your locator matches to

OR run the below code (before clicking)

${count}=   Get Element Count  //a[@id="Record Docs Latest"]
Should Be Equal as Integers   
...   ${count}   1   
...   There was not just one element match. There were ${count} matches.

@mika and @Teppo we resolved this issue with help of blake from your team, it was problem with switch window and selenium is not able to find this element on redirected window correctly!!

Thanks for all your support!! Appreciate it.

1 Like