Forum Discussion

halovski's avatar
halovski
Contributor
8 months ago
Solved

How do I locate an element by its ID in html? The equivalent of getElementByID in JavaScript

  I have found the ID of the tr parent, with a regex. Now I need to go down to the td class = c3 child and click the image inside it. How can I do that when I have its ID? Query selector and Find didnt work for me. Going down 2 children by their ID is the issue.

  • Using ID,

    will highlight the first row

7 Replies

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    Here's an example based on the following website https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_tr,

    function Test()
    {
        var page = Aliases.browser.Page("https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_tr").FindElement("#iframeResult")
        // Highlight Month
        var element = page.FindElement("/html/body/table/tbody/tr[1]/th[1]");
        Sys.HighlightObject(element);
        // Highlight $80
        var element = page.FindElement("/html/body/table/tbody/tr[3]/td[2]");
        Sys.HighlightObject(element);
    }

    It uses XPath to find the element and then highlights it.

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    Another example, to highlight the cell using XPath base on the class name

     

    • halovski's avatar
      halovski
      Contributor

      I am currently at 
      let tr = Aliases.browser.pagePostbank.FindElement("td[@id='m_img_Edit_5be1669f1f644960a895de91534a50f8']")

       

      Its xpath is /html/body/form/div[3]/control/div/div/div[2]/div/div/div/div[1]/div/table/tbody/tr[15]/td[1]/div/a

       

      I don't know if I have to use the full path or not. Missclicked the solution button sorry about that

  • Can you please just give me an example with this ID:

    id="m_img_Edit_5be1669f1f644960a895de91534a50f8"

     

    I tried to write the ID in a few ways and none of them worked. What specifically would I have to write in the FindElement for it to get located?

     

    let img = Aliases.browser.FindElement("id...")

    • halovski's avatar
      halovski
      Contributor

      Thank you so much, my problem was the xpath //tr[@id=.

       

      So I don't start another thread, how can I put my variable inside the xpath like I can do in javascript.

       

      ("//tr[@id='My variable here']")

      I tried ${} ("//tr[@id='${My variable here}']") like in javascript and that didn't help

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    For example,

    var myVar = "m_img_Edit_5be1669f1f644960a895de91534a50f8"
    var xpathStr = "//*[@id='" + myVar + "']";
    var element = page.FindElement(xpathStr);