Jrawr Jrawr - 25 days ago 4x
Java Question

Selenium check if attribute contains part of a string in java

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?

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.