1

Context So I have to make an automated test for my work. I come across this problem: - I have a table with 'subcontractors' - If I create an 'subcontractor' it will get in to the list (table) - But always on a random place (row 1, row 2, row 8 whatever) - Also the ID's change everytime. - All the rows have 4 buttons in the last (7th) column. - With the same names and the same ID as the tablerow.

Quesiton

I have to click on the button on lets say, the 4th row.

How can I do this? If I use Xpath it will just click the button on the first row. Thanks

EDIT! //get table WebElement baseTable = driver.findElement(By.id("gridSubContractors"));

            String first_part = "//*[@id='gridSubContractors']/tbody/tr[";
            String second_part = "]/td[3]";

            String final_xpath;

            int numOfRow = baseTable.findElements(By.tagName("tr")).size(); 
            boolean found = false;
             WebElement cellIneed = null;
            for (int i=1; i<=numOfRow && found==false; i++){
                final_xpath = first_part+i+second_part;
                String test_name = baseTable.findElement(By.xpath(final_xpath)).getText();
                System.out.println(test_name);
                if (test_name.equals(subcon)){
                    WebElement tableRow = baseTable.findElement(By.xpath(final_xpath));
                    cellIneed = tableRow.findElement(By.xpath("//td[7]"));
                    found=true;
                }
            }
            if (cellIneed != null){
            cellIneed.findElement(By.xpath("//a [@data-original-title='Subcontractor name']")).click();
            }

That is my code, cant realy get thw code in here. but what this code does is click the first button in the table with the name 'Subcontractor name' and not the one on the row that has been found with "if (test_name.equals(subcon)". subcon = the name of the subcontractor that needs to be found in the table. it is created by this: "subcon = name.getTextContent();" earlier in the code. hope this is information you need

5
  • 2
    Could you show your HTML table code to us? Commented Dec 9, 2015 at 8:55
  • pls post a fitting HTML snippet, what you already tried, and what did not work Commented Dec 9, 2015 at 8:55
  • edit done. the html code is weird and can't copy it properly but hope what i did edit is enough Commented Dec 9, 2015 at 9:27
  • without the HTML, it's hard to help you mate. What do you mean with "copy it properly"? Just look at the page source via your browser and publish the relevant parts of and around the table you're interacting with Commented Dec 9, 2015 at 10:06
  • the problem is that if i right click > show source, the table is not in that source. its in a div. and if i do it with firebug it does the same. can i show it to you with teamviewer or something? Commented Dec 9, 2015 at 10:17

1 Answer 1

0

I have got the simular problem since upgrading to newer versions.

The problem what I found is that the search /div[]/[]... doesn't work like I was used to it before.

I avoided it, by running over result-elements and checking which should be clicked.

Maybe I'm wrong, but for me it seams to be a bug in selenium.

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

3 Comments

that is wat we are trying (running over result-elements and checking which should be clicked.) how did you do this? any examples maybe?
Your iteration seam to be right. I think the part with "//*[@id='gridSubContractors']/tbody/tr["; String second_part = "]/td[3]" gets you the wrong results. Iterate over the items and print out the 'text' and attributes of the elements.
ah! i see, it now thinks that the correct cell i need is for example : //table/tr[5]/td[3]/td[7] but there is no td in td! Thanks for helping!

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.