1

I'm testing an application and in the application there is a table containing checkboxes (3 rows, x columns). Below is the html for part of this table (1row). I'm trying to click on the text of one of these items to either select or deselect a checkbox based on the text. So for example, I want to click on the text 'Text next to checkbox for nr 8' in order to select that textbox. However, I don't want to go there via a hard xpath using code like /table/tbody/tr[2]/td[2]/div/span)[2] but based on the text. I'm trying code like the ones below but they don't seem to work:

driver.findElement(By.xpath("(//*[@class='settingsPanelTD' and text()='Text next to checkbox for nr 8']")).click();
driver.findElement(By.xpath("(//*[@class='settingswidgetTitle' and text()='Text next to checkbox for nr 8']")).click();
driver.findElement(By.xpath("(//*[@class='settingswidgets' and text()='Text next to checkbox for nr 8']")).click();

I would like to create a string for the final xpath so you only have to change the text you'd like to click to change the test, something like:

String text = "Text next to checkbox for nr 8"; /** variable text based on what you want to click */
String locator = "//*[@class='settingswidgets' and text()='";
String xpathitem = locator + text + "']";
driver.findElement(By.xpath(xpathitem)).click();

This is the HTML:

<td class="settingsPanelTD" width="33%">
<div class="settingwidgetsTitle">
    <input id="widgetsettings8" class="settingwidgets" type="checkbox" alt="2">
    <span>Text next to checkbox for nr 8</span>
    <a class="AddWidget" href="#"></a>
</div>
</td>
<td class="settingsPanelTD" width="33%">
<div class="settingwidgetsTitle">
    <input id="widgetsettings9" class="settingwidgets" type="checkbox" alt="2">
    <span>Text next to checkbox for nr 9</span>
    <a class="AddWidget" href="#"></a>
</div>
</td>
<td class="settingsPanelTD" width="33%">
<div class="settingwidgetsTitle">
    <input id="widgetsettings9" class="settingwidgets" type="checkbox" alt="2">
    <span>Text next to checkbox for nr 10</span>
    <a class="AddWidget" href="#"></a>
</div>
</td>

I'm using eclipse, Selenium and Java.

2 Answers 2

1

The XPaths you are using are incorrect. The third XPath you are using is closest to the correct solution:

//*[@class='settingswidgets' and text()='Text next to checkbox for nr 8']

In plain English, your XPath means - look for any element in the document with a class="settingswidgets" and text equal to Text next to checkbox for nr 8. Unfortunately, no such element exists.

One potential XPath you could use is:

//span[text()="Text next to checkbox for nr 8"]

This searches for any span element with the appropriate text.

You could then use:

String text = "Text next to checkbox for nr 8";
String locator = "//span[text()='";
String xpathitem = locator + text + "']";
driver.findElement(By.xpath(xpathitem)).click();

This should generate the desired click.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! that worked like a charm. Didn't know you could use span text too. Still new at this and learning.
0

I don't have enough reputation to add comment but in case you are unable to select option by clicking Span tag you need to click on input tag

ArrayList<WebElement> al=new ArrayList<WebElement>(); driver.findElements(By.className("settingwidgetsTitle"));

for(int i=0;i<al.size();i++){ if(driver.findElement(By.xpath(".//div[@class='settingwidgetsTitle']/span")).getText().equals("Option to Select")){

driver.findElement(By.xpath(".//div[@class='settingwidgetsTitle']["+i+"] /input").click()); break; }}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.