0

I add a number to a set of numbers on a table I have figured out how to scroll to the number

IWebElement element = wb.wd.FindElement(By.XPath("//span[contains(text(),'1111111111')]"));



((IJavaScriptExecutor)wb.wd).ExecuteScript("arguments[0].scrollIntoView(true);", element);

System.Threading.Thread.Sleep(500); 

Now at the top of the current in view screen to the right of my number is an Edit button I would like to click on I do this

wb.wd.FindElement(By.XPath("//input[@type='submit'][@value='Edit']")).Click();

But it scrolls up to the top of the page and clicks the Edit button for the number at the top of the page. How can I click the Edit button I want. Id, names are dynamic as well as CSS xpath and Indexes.

3
  • Can you provide the html you are dealing with. Commented Feb 20, 2015 at 18:55
  • No but if you want to see a similar issue you can look at this theinternet.herokuapp.com/challenging_dom Commented Feb 20, 2015 at 19:04
  • The link you provided doesn't exist. Please post html if you want help, or post mock html which emulates your page. Commented Feb 20, 2015 at 21:15

1 Answer 1

1

Without the html, it is difficult to determine the best course of action. I would suggest first, if there is a distinction in the html hierarchy which you could utilize for the xpath. For example, perhaps one button is located in a form with a unique id, you could use:

wb.wd.FindElement(By.XPath("*//form[@id='uniqueId']//input[@type='submit'][@value='Edit']")).Click();

To better help yourself in this, I would recommend this website as a resource to see which element you are selecting (change the node and output format to HTML to see which you are selecting).

Alternatively, FindElement will select the first element it encounters on the page, whereas FindElements will return a list of all elements. From here you could assign the specific element you want based on its index.

EDIT

It is also possible to select a specific row by using unique text that is also in that row. In the example that you showed here, if you wanted to select the edit button on the fifth row then you could use the xpath:

*//tr[td/text()='Iuvaret5']/td/a[@href='#edit']

The *//tr[td/text()='Iuvaret5']section will select the table row which contains a td with the text Iuvaret5. From this tr, it will further select with /td/a[@href='#edit']to select the specific edit button.

I hope that you can use this logic to work in your situation.

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

5 Comments

I can't use Id's they are dynamic. Each number has an Edit button and the numbers could change place which would change index.
Is there any relationship in the HTML between the edit button you wish to select, and the number you wish to scroll to. By this, I mean are the edit button and number both under the same <tr>, or <span>, or <table>? If possible, could you post the html of a single edit button and number?
In the question it refers to an xpath and the value of an edit button.
I'm sorry, but you will either need to be more specific, or post some html because otherwise it is a shot in the dark here...
What should I be more specific about?

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.