I am working on a testing program that operates with very little information. In this particular case, my program doesn't know the ID of elements in the page before it runs, because the Javascript on the page dynamically assigns those at run time. The only constants I have is the structure and the text I'm looking for. I'm including a screenshot of one example of the DOM being generated. In this case I know that I want to access the button with text apply that is displayed next to the label with the text "To Location:" Is there a way to use xpath manipulate their relationship and ensure that I'm accessing the right element. I can't just access the apply button because there are 6 apply buttons on the page with dynamically generated IDs. The label's next to them are different so I'm trying to use that and manipulate the path's from there. Help?
Asked
Active
Viewed 209 times
0

Jrawr
- 199
- 2
- 3
- 15
-
You can use `driver.findElements(...)` and pick the first button (or whatever it should be). You can also write a bunch of code to descent into the dom step by step. – Todd Sewell Jan 26 '17 at 20:59
-
You might be looking for this, actually: http://stackoverflow.com/questions/8577636/select-parent-element-of-known-element-in-selenium – Todd Sewell Jan 26 '17 at 21:00
-
Dom? Find? sounds a lot like xpath – Eugene Jan 26 '17 at 21:04
-
1Please do not add code in images. Instead post a complete, minimal example of the HTML document _as text_, together with all _relevant_ Java/Selenium code. While you're at it, you should also show the output you expect from your code. Thanks. More help: http://stackoverflow.com/help/mcve. – Mathias Müller Jan 26 '17 at 21:44
-
can you share your html code? – Jainish Kapadia Jan 31 '17 at 06:39
2 Answers
0
This is possible. If you provide the entire html code I could provide a better xpath. But for what you pasted, here's a simple one that might work:
//td[div//label[text()='To Location:']]/following-sibling::td[1]//button[text()='Apply']

becixb
- 365
- 1
- 9
0
There's a slightly longer winded way but thats generating a list of elements by class and clicking the one with the right text
`var elements = driver.FindElements(By.Class("text-pb"));
foreach(var element in elements)
{
if(element.Text.Equals("Searched Text"))
{
element.click();
}
}`
that might work thats if you want to click the button. i use these sort of things on the pages works site generates so it should do what your after.

Bithellio
- 23
- 7