Selenium java can not find the right element

时间:2015-12-14 17:54:57

标签: java selenium

I want to find the element for a checkbox on the page:

this is the source code of the page:

<tr>
                        <td class="tablecell"  width="200" valign="top">Filter_list</td>
                        <td class="tablecell"  width="360">
                            <div id="div_filterlist">
                                <table border="0">
                                    <tr>
                                        <td class="tablecell"  width="160" valign="top"><b>Available Filters</b><br>
                                            <ul id="unselected_ilters" class="unselected_filter">

                                                <li itemID="test">
                                                    <input type="checkbox" value="test" onClick="toggleUnselected(this,'test')" onMouseOver="this.style.cursor='hand'"/>&nbsp;&nbsp;&nbsp;
                                                    <a href="#" onClick="document.filterform.name.value='test';document.filterform.submit();">test</a>
                                                </li>

My code is not working:

String xpathLocater = "//input[ @type=\"checkbox\" and @value=\" test\"]"; 

1 个答案:

答案 0 :(得分:1)

css selector:
String cssLocator = "input[type=checkbox]"; or
String cssLocator = "input[type=checkbox][value=test]";

xpath selector:
String xpathLocator = "//input[@type=\"checkbox\"]"; or
String xpathLocator = "//input[@type=\"checkbox\" and @value=\"test\"]";

If you still can't find the element check 2 things:

  • whether that element inside iframe: if yes - you need to switch to iframe first
  • add wait before finding an element (you could try to find element before it was added to DOM)