2

I am trying to write a webdriver C# code that checks for elements in a site. However, the site is a single page website with an embedded form. The form contents changes when you click Next. So I am having a hard time figuring out how to find elements in the next form. I have had this work several times with a non single page. I know the issue is that my xpath is not finding the element I am looking for because it is inside a form

This is my xpath:

private By labelPageHeader = By.XPath("//form[1]/div[4]/label[text()='Welcome to the second form']");
public bool IsNewForm()
{
    return Elements.CheckElementIsVisible(_driver, labelPageHeader );
}

When I do that I get error:

Could not load page within the designated timeout period

That usually works when I am not working in a single page site.

Here is a snippet of the HTML. I am trying to hit the label inside id="alligator" inside the form:

<form name="form1" method="post" action="./form.aspx?AspxAutoDetectCookieSupport=1" onsubmit="javascript:return WebForm_OnSubmit();" id="form1">
    <div id="Step3" class="step">
        <div id="alligator" class="input">

            <label for="alligator">Welcome to the second form
                <abbr title="This field is required">*</abbr></label>                                  

        </div>
        <input type="hidden" class="link" value="Step4" />
    </div>
    <!-- step3 ends -->
  • What is your question? – Alexey R. Oct 26 '17 at 8:16
2

Rather than trying with an xpath like form[1]/div[4], try to be more specific like

//form[@id='form1']//div[@id='alligator']/label

Searching with the exact text in the element can be your personal preference.

And the xpath can be used as:

private By labelPageHeader = By.XPath("//form[@id='form1']//div[@id='alligator']/label");

public bool IsNewForm()
{
    return Elements.CheckElementIsVisible(_driver, labelPageHeader );
}
  • Thanks a lot @the_coder! I tried the xpath you provided and I noticed two things, when I run the test with or without debugger, it fails but when I put a breakpoint in the "IsNewForm" method and use f11 to step into it, the element gets hit and the test pass. One other thing I think may be the issue is that the site contains javascripts. Is there a different way to check if Element is visible using javascript? – Geezus Oct 27 '17 at 13:03
  • If the element you want to find has an id attribute you can you the following mentioned here: stackoverflow.com/questions/10886190/… – the_coder Oct 30 '17 at 4:25
  • I will like to mention, when I switch the driver to Chrome or even Firefox, that particular step passed. so the issue is only occurring when I run my test on IE – Geezus Oct 30 '17 at 22:36
0

The problem it seems is that you are using XPath. You should use id.

From the snippet of HTML code you provided I see that the form does not have its own id. I suggest you give the form a id.

Then do your thing manually (that is do what you want to automate manually) until you go to the next page. Then you inspect the element you are trying to reach using the browser (e.g. on firefox right click on element and choose inspect element). Some HTML code will be displayed. This is the code that your browser "sees". From this code note the id given for that element it could be in the form of 'formId:elementID'. Use this ID to point to the element.

This should work.

  • Thanks a lot @ITguy! I gave the form ID and ran some test, still nothing is getting hit. I even tried to hit just the form ID on the top and it still fails. One other thing I think may be the issue is that the site contains javascripts. Is there a different way to check if Element is visible using javascript? – Geezus Oct 27 '17 at 13:07
  • After you give the form id, run through the steps (that you want to automate) manually. After each click/page load/Next Click, right click on the element and choose inspect element. Then in this html code that you get look for id of the element (It does not matter if you are using javascript or not you will always see an id). In case there is no id given in your program's code itself, the browser will generate a temporary ID itself (using this temporary in your code however will not work, so make sure there exists an id in your program's HTML code). – ITguy Oct 27 '17 at 13:50
  • Open a chat with me if you are online right now. – ITguy Oct 27 '17 at 13:55
  • I am. I haven't done chat before, can you please initiate one please? – Geezus Oct 27 '17 at 13:59
  • I am in a chat with you right now. – ITguy Oct 27 '17 at 14:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.