Search code examples
seleniumxpathselenium-webdriverautomated-testszk

Testing ZK applications with Selenium Webdriver


I've recently started working on Selenium Webdriver (Chrome) for Java language. My application is developed using zk framework, hence its ID's are randomly generated. Eg:

/div[@id='z_j0_7!cave']/form[@id='loginForm']/table[@id='z_j0_9']/tbody/tr[@id='z_j0_a!chdextr']/td/table[@id='z_j0_a']/tbody/tr[@id='z_j0_a!cave']/td[@id='z_j0_c!chdextr']/input[@id='z_j0_c']

How can I find xpath of such elements?

The zul file is like this:

<h:form id="loginForm" action="j_spring_security_check" method="POST">
  <vbox sclass="login_grid z-grid" spacing="2px">
    <hbox widths="7em, 8em">
      <label value="${c:l('login')}:"/>
      <textbox  id="login"  name="j_username" value="${LoginForm.login}"       use="de.hybris.platform.cscockpit.components.login.LoginTextBox"/>
    </hbox>
    <hbox widths="7em, 8em">
      <label value="${c:l('password')}:"/> 
      <textbox  type="password" id="pw" name="j_password"  value="${LoginForm.password}"  use="de.hybris.platform.cscockpit.components.login.PasswordTextBox"/>
    </hbox>
  </vbox>       
</h:form>

View as in developer mode: (For label user id and its input field)

<table id="z_38_a" z.type="zul.box.Box" class="z-hbox" z.zcls="z-hbox" cellpadding="0" cellspacing="0">
    <tbody>
        <tr id="z_38_a!cave" valign="top">
        <td id="z_38_b!chdextr" z.coexist="true" align="left" style="width:7em">        <span id="z_38_b" class="z-label" z.zcls="z-label">ユーザー ID:</span>
        </td>
        <td id="z_38_b!chdextr2" class="z-hbox-sep">
        </td>
        <td id="z_38_c!chdextr" z.coexist="true" align="left" style="width:8em"> <input id="z_38_c" z.type="zul.vd.Txbox" class="z-textbox" z.zcls="z-textbox" type="text" name="j_username" value="admin">
        </td>
        </tr>
    </tbody>
</table>

Solution

  • I'm looking for the xpath of login and password input fields. Programming language is java and I using Chromedriver.

    There is no need to do extra stuff and use xpath to locate desire element, you can use By.name() locator to locate easily desire element as well as below :-

    WebElement user = driver.findElement(By.name("j_username"));
    
    WebElement password = driver.findElement(By.name("j_password"));