Reputation: 199
So I have a webpage where the Id's of certain elements are generated dynamically with certain parts changing and certain parts being constant like so:
<div id= "xxxdiv" title= 'this title'>
<p id = "xxxp">
<table id = "xxxtable" title = 'not derp'>
<div id = "yyyydiv">
<table id = "yyytable" title= 'derp'>
<table id = "yyytable2" title = 'not derp'>
<table id = "zzztable" title = derp>
I'm trying to do some dynamic lookups on the page that can look for a given element where the id contains a known value. For instance, I do not always know how many nested elements might exist so the number that gets generated on the end can vary. So I would like to find an element by xpath where the @title = not derp and the @id contains yyy. How can I accomplish this?
Upvotes: 3
Views: 16378
Reputation: 23805
I would like to find an element by xpath where the @title = not derp and the @id contains yyy
I would suggest you cssSelector
instead, because using By.cssSelector()
to locate an element is much faster than By.xpath()
in performance. So you should try as below :-
driver.findElement(By.cssSelector("table[id *= 'yyy'][title = 'not derp']"));
Upvotes: 1
Reputation: 4820
driver.findElement(By.xpath("//table[@title='not derp' and contains(@id, 'yyy')]"));
will find the first element which matches your search criteria which would be <table id = "yyytable2" title = 'not derp'>
of your HTML snippet.
Upvotes: 7
Reputation: 247
There are a whole host of relational and logical operators you can use. In this case, assuming you're looking for the table element, you can use: //table[@title='not derp' and contains(@id,'yyyy')]
Upvotes: 2