1

I am trying to click on element by the label. Here is the code I am using:

driver.findElement(By.xpath("id(//label[text() = 'LABEL TEXT HERE']/@for)")).click();

It works for (Select All) & Hayward but cant find Los Angeles, San Fran, or San Jose.

UPDATE:

For now I guess this may be my best option until I see something better. This will allow the user to pass the full String and the function in the method will grab the last word of the string and insert it into the contains xpath.

public void subStringLocationTest(String location) {

    String par = location.substring(location.lastIndexOf(" ") + 1);

    driver.findElement(By.xpath("//label[contains(text(), '" + par + "')]")).click();

}

Here is the HTML code:

<div id="ReportViewer1_ctl04_ctl03_divDropDown" onclick="event.cancelBubble=true;" onactivate="event.cancelBubble=true;" style="border: 1px solid rgb(169, 169, 169); font-family: Verdana; font-size: 8pt; overflow: auto; background-color: window; display: inline; position: absolute; z-index: 11; left: 131px; top: 41px; width: 188px;">
    <span><table cellpadding="0" cellspacing="0" style="background-color:window;">
        <tbody><tr>
            <td nowrap="nowrap"><span style="font-family:Verdana;font-size:8pt;"><input id="ReportViewer1_ctl04_ctl03_divDropDown_ctl00" type="checkbox" name="ReportViewer1$ctl04$ctl03$divDropDown$ctl00" onclick="$get('ReportViewer1_ctl04_ctl03').control.OnSelectAllClick(this);"><label for="ReportViewer1_ctl04_ctl03_divDropDown_ctl00">(Select All)</label></span></td>
        </tr><tr>
            <td nowrap="nowrap"><span style="font-family:Verdana;font-size:8pt;"><input id="ReportViewer1_ctl04_ctl03_divDropDown_ctl02" type="checkbox" name="ReportViewer1$ctl04$ctl03$divDropDown$ctl02" onclick="$get('ReportViewer1_ctl04_ctl03').control.OnValidValueClick(this, 'ReportViewer1_ctl04_ctl03_divDropDown_ctl00');"><label for="ReportViewer1_ctl04_ctl03_divDropDown_ctl02">Hayward</label></span></td>
        </tr><tr>
            <td nowrap="nowrap"><span style="font-family:Verdana;font-size:8pt;"><input id="ReportViewer1_ctl04_ctl03_divDropDown_ctl03" type="checkbox" name="ReportViewer1$ctl04$ctl03$divDropDown$ctl03" onclick="$get('ReportViewer1_ctl04_ctl03').control.OnValidValueClick(this, 'ReportViewer1_ctl04_ctl03_divDropDown_ctl00');"><label for="ReportViewer1_ctl04_ctl03_divDropDown_ctl03">Los Angeles</label></span></td>
        </tr><tr>
            <td nowrap="nowrap"><span style="font-family:Verdana;font-size:8pt;"><input id="ReportViewer1_ctl04_ctl03_divDropDown_ctl04" type="checkbox" name="ReportViewer1$ctl04$ctl03$divDropDown$ctl04" onclick="$get('ReportViewer1_ctl04_ctl03').control.OnValidValueClick(this, 'ReportViewer1_ctl04_ctl03_divDropDown_ctl00');"><label for="ReportViewer1_ctl04_ctl03_divDropDown_ctl04">San Francisco</label></span></td>
        </tr><tr>
            <td nowrap="nowrap"><span style="font-family:Verdana;font-size:8pt;"><input id="ReportViewer1_ctl04_ctl03_divDropDown_ctl05" type="checkbox" name="ReportViewer1$ctl04$ctl03$divDropDown$ctl05" onclick="$get('ReportViewer1_ctl04_ctl03').control.OnValidValueClick(this, 'ReportViewer1_ctl04_ctl03_divDropDown_ctl00');"><label for="ReportViewer1_ctl04_ctl03_divDropDown_ctl05">San Jose</label></span></td>
        </tr>
    </tbody></table><input type="hidden" name="ReportViewer1$ctl04$ctl03$divDropDown$ctl01$HiddenIndices" id="ReportViewer1_ctl04_ctl03_divDropDown_ctl01_HiddenIndices" value=""></span>
</div>
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
qaRookie
  • 105
  • 1
  • 2
  • 10
  • You need to escape the space. Try using & – Ajinkya May 17 '14 at 12:57
  • I have actually tried this and it is unable to locate the element still – qaRookie May 19 '14 at 14:35
  • did you try this driver.findElement(By.cssSelector("#ReportViewer1_ctl04_ctl03_divDropDown > span > tbody > tr:nth-of-type(number if td u want)")).click(); – elcharrua May 19 '14 at 15:26
  • Cant use numbers or ids. It has to be able to do it by the selection. We are building a front end which will allow an end user (business) to enter the variables. We cannot ask the end users to enter a number. – qaRookie May 19 '14 at 15:49

8 Answers8

3

You can use xpath locater. For example:

d.findElement(By.xpath("//...../label[contains(@for,'ReportViewer1_ctl04_ctl03_divDropDown_ctl02')]")).click();
Saurabh
  • 81
  • 10
0

Use following xpaths :

//label[text()='(Select All)']
//label[text()='Hayward']
//label[text()='Los Angeles']
//label[text()='San Francisco']
//label[text()='San Jose']
Sitam Jana
  • 3,123
  • 2
  • 21
  • 40
  • Having same issue. Only the first two work //label[text()='(Select All)'] //label[text()='Hayward'] Still unable to click LA, SF or SJ. – qaRookie May 16 '14 at 22:29
  • Which browser did you use to run? I personally ran across FF and Chrome, were able to click on all of them using the posted xpaths. May be you have multiple labels present with that same name (LA, SF or SJ) in the page. In that case please post the complete html source or link to the html page. – Sitam Jana May 16 '14 at 22:52
  • I am using firefox - Here is an example of the error I get Unable to locate element: {"method":"xpath","selector":"//label[text()='San Jose']"} I did check the page and the labels do not exist anywhere else in the page. – qaRookie May 19 '14 at 15:28
0

You can also do a contains like this:

"//label[contains(text(), 'TEXT_TO_FIND')]"
  • This one wont work when you pass the entire string. For example - 'San Francisco' will Fail but 'Francisco' works. Since this is a method with String location. When they call the method they are passing the entire location (San Jose, San Francisco, Los Angeles) – qaRookie May 16 '14 at 22:38
  • Since contains seems to work the best I went ahead and created a method which has a function that turns the full string into a partial string. See method above - 'subStringLocationTest' – qaRookie May 19 '14 at 15:18
0

Try to use id instead of xpath:

//for Select All
driver.findElement(By.id("ReportViewer1_ctl04_ctl03_divDropDown_ctl00")).click();
//for Hayward
driver.findElement(By.id("ReportViewer1_ctl04_ctl03_divDropDown_ctl02")).click();
//for Los Angeles
driver.findElement(By.id("ReportViewer1_ctl04_ctl03_divDropDown_ctl03")).click();
//for San Francisco
driver.findElement(By.id("ReportViewer1_ctl04_ctl03_divDropDown_ctl04")).click();
//for San Jose
driver.findElement(By.id("ReportViewer1_ctl04_ctl03_divDropDown_ctl05")).click();

If still have problem or id is changing dynamically you cane use xpath like:

//label[contains(.,'(Select All)')]
//label[contains(.,'Hayward')]
//label[contains(.,'Los Angeles')]
//label[contains(.,'San Francisco')]
//label[contains(.,'San Jose')]
Abhishek Yadav
  • 459
  • 5
  • 16
  • Cant use ID because the users will be passing the location (LA, SJ, etc) as a variable. I do not feel like creating an if else statement because I could potentially have 100's of choices. – qaRookie May 19 '14 at 14:20
  • And what about Xpath, which I have provided. Are these working for you? – Abhishek Yadav May 19 '14 at 17:58
  • No the Xpaths do not work for the following: //label[contains(.,'Los Angeles')] //label[contains(.,'San Francisco')] //label[contains(.,'San Jose')] – qaRookie May 19 '14 at 20:13
0
driver.findElement(By.xpath("//label[text()='LABEL TEXT']/../input")).click();
stan
  • 984
  • 10
  • 15
0

Try CSS:
driver.findElement(By.CSS("label:contains('Partial or full text')");

Bandeesh R Sirga
  • 221
  • 1
  • 3
  • 6
  • Using CSS its unable to find anything. With Xpath I am at least able to get (Select All) and Hayward. – qaRookie May 19 '14 at 14:25
0

try this, maybe good startpoint:

System.out.print(driver.findElement(By.xpath("//div[@id='ReportViewer1_ctl04_ctl03_divDropDown']/span/table/tbody/tr[" + i +"]/td/span/label")).getText());
driver.findElement(By.xpath("//div[@id='ReportViewer1_ctl04_ctl03_divDropDown']/span/table/tbody/tr[" + i +"]/td/span/label")).click();

i = 3; //Los Angeles
i = 4; //San Francisco

I hope, my xpath is correct.

Milky
  • 143
  • 4
  • 15
0

Use below code to click on label or choose label

driver.findElement(By.xpath("//label[@for='your id which you want to click']")).click();
Werner Henze
  • 16,404
  • 12
  • 44
  • 69
Ksingh
  • 3
  • 5