I have a problem how to select a desired option (e.g. From, To...etc.) from list if 'datalist id' and 'select name' are randomly generated. This means that the ID and name has another value after refresh page. Here is a code for a first row, and 1st Field list:
It is a matter of proper xpath. You know the row and the column. You also know that the
select is under
datalist. The thing that is left to do is
N is the row number. You can extend this xpath so that it captures the particular option if you need.
Assuming that the class name of the td = field is unique for the page, you can use the below code to set "To" value in the select dropdown
IWebElement option = driver.FindElement(By.XPath("//*[@class='field']/datalist/select")); var selectElement = new SelectElement(option); selectElement.SelectByText("To");
Instead of making "complex" XPaths I like to find a container I know the Id of. Then within that container search for in this case rows, within the second row search for a cell, within the cell find the dropdown.
var table = driver.FindElement(By.Id("advFilter")); var rows = table.FindElements(By.TagName("tr")); var cell = rows.FindElement(By.ClassName("field")); var dropdown = new SelectElement(cell.FindElement(By.TagName("select"))); dropdown.SelectByText("YourOption");
Now this might be slower, but I dont think you will notice it. It will be much easier to understand and read in the future.