I've come across a block of code that utilizes action sequences to execute the following steps:
- click on a link that directs to a specific page
- wait for an input field to become visible on the page
- click on the input field
- clear any existing text in the field
- type in a sequence of keys to specify a location
- press the down arrow key (as the input uses autcomplete)
- press the enter key to choose the correct location
- click on the save button
I've heard that using driver.sleep()
commands is not advised, but I can't seem to get the code to function correctly without them.
Below is the snippet of code:
driver.wait(until.elementLocated(By.css("a[href*='details/location']")), 5000)
driver.findElement(By.css("a[href*='details/location']")).click()
driver.wait(until.elementLocated(By.id("user_location")), 5000)
let loc = driver.findElement(By.id("user_location"))
let save = driver.findElement(By.xpath("//span[contains(text(), 'Save')]"))
driver.sleep(3000)
driver.actions().
click(loc).
sendKeys(Key.DELETE)
.sendKeys('My location')
.perform()
driver.sleep(1000)
driver.actions().
sendKeys(Key.ARROW_DOWN).
sendKeys(Key.ENTER).
perform()
driver.sleep(1000)
driver.actions().
click(save).
perform()
Is there a more efficient approach to achieve this and could the stale element
errors be avoided by eliminating the sleep statements? What causes these errors to occur when the sleep statements are removed?