0

I'm new to Selinium. There is a table (inside another table) that I want to click each <td>click here</td> with a matching class value (note the text between opening and closing td will change but is irrelevant for matching purposes). The class value I'm trying to match is open. From here I learn the right way is with //*[contains(concat(" ", normalize-space(@class), " "), " open ")]

This seems to work but just for one random cell. How do I make it click all? I was planning on accomplishing this first and repeating the step, but it may be worth noting that the script should also do the same for class value available and not just open Is there away to do logical or?

TL;DR I want to click everything with <td class="open">...</td> and
<td class="available">...</td> where ... is example text that will vary but should be ignored.

Community
  • 1
  • 1
Celeritas
  • 14,489
  • 36
  • 113
  • 194
  • So is it `click here` or `click here` ? – Andersson Jan 12 '17 at 09:32
  • @Andersson `click here` but the text (e.g. `click here`) changes, so the thing being matched is the `open`. Sorry if this was confusing, I'm having trouble being clear. – Celeritas Jan 12 '17 at 09:34

4 Answers4

3

Get all elements and store them in a List, then iterate over them and click the buttons in sequence.

List<WebElement> list = driver.findElements(By.xpath("//*[contains(concat(" ", normalize-space(@class), " "), " open ")]"));
for(WebElement webelement : list) {
       // webelement Click the button here
}
StefanE
  • 7,578
  • 10
  • 48
  • 75
  • How can this be added to Selenium IDE, FireFox add-on? – Celeritas Jan 12 '17 at 09:44
  • 1
    The IDE plugin is limited when it comes to these type of more advanced operations. There might be a way of creating a loop, check if the element class match and then click it.. Start use a programming language and the selenium api is my tip if you really want to be able to solve problems like this. Otherwise someone else have to answer your question! – StefanE Jan 12 '17 at 09:55
0

To get all elements, you should use driver.FindElements. Make sure it's what you used, not driver.FindElement?

Linh Nguyen
  • 1,120
  • 7
  • 13
0

Please use following XPath:

//td[normalize-space(@class)="open" or normalize-space(@class)="available"]

This should match both <td class=" open ">...</td> and <td class=" available ">...</td>

Andersson
  • 51,635
  • 17
  • 77
  • 129
  • I guess my actual question is how to iterate over each returned result. I'm using Selenium IDE and I have that in the target, and the command is `click` but it only clicks one. And I want it to click each one that is matched. – Celeritas Jan 12 '17 at 09:42
  • I guess you can use `while` loop for this purpose. Check this http://www.testingdiaries.com/selenium-ide-loop/ – Andersson Jan 12 '17 at 09:57
0

Use following approach may help you -

List<WebElement> allElements = driver.findElements(By.tagName("td"));

for(WebElement element:allElements)
{
    if(element.getAttribute("class").contains("open"))
    {
        // manipulate with <td> tag data 
    }
}
NarendraR
  • 7,577
  • 10
  • 44
  • 82
  • How can this be added to Selenium IDE, FireFox add-on? – Celeritas Jan 12 '17 at 09:43
  • I guess there is no way to do the same logic in 'Selenium IDE' but some additional plugins are there to manage conditions and looping – NarendraR Jan 12 '17 at 09:46
  • If Selenium IDE is too weak, what else should be used to run the source code? Is the normal approach to write another program e.g. Java and use it to run Selenium? – Celeritas Jan 12 '17 at 09:50
  • Yes Selenium webdriver is the best option to do that. You can choose your preferred language and manage your logic what you want to do browser. – NarendraR Jan 12 '17 at 09:59