On the login page, the user name, password and login button fields all have id/class name in source code.
So when writing web driver script, if the code gets changed then does the web driver code also need to change?
Your WebDriver code will always depend on the application code. That's why choosing good locators is so important.
My preference is (more or less in order)
nameelement is unlikely to change - possibly less likely to change than ID because form fields generally submit by name.
I also prefer to structure my code to minimize repetition, by using patterns like the Page Object pattern. The goal here is that each field locator exists in exactly one place in my test code. That way when the code changes in a way that forces my test code to change, I only need to edit my locators in one unit.
It depends on what changes has been made on the page.
For example suppose you have 10 element in login page and developer changed id, attribute value or other thing of 2-3 elements. In this case it will throw
NoSuchElementException for changed field So you have to again locate those element and update the locator in your script
Suitable option for this case
But in case some functional change done in your site then you have to again validate your complete test script as per changed flow or functionality.
Your test automation code directly depends on the application code under test.
It is important to be smart about choosing a way to locate elements on a page - some locators have a higher probability of being affected by a layout/design change, some are more or less fragile.
At the same time, your locator should be readable and unique. There is a pretty long discussion with multiple great points on the subject here: