Why Selenium couldn't find any element on page before I used time.sleep()?
I came across this problem while automating task with Selenium.
What was the code execution process?
So first of all there're two functions inside seleniumtest.py
:
getGateway()
and restartRouter()
.
Let's focus on restartRouter()
function as it contains code related to selenium, and getGateway()
function was just function that I used in restartRouter()
just like this: driver.get(getGateway())
.
First of all, what was happening is following:
-
Start browser and open provided url;
-
Find elements username and password
-
Enter username and password;
-
Find Login element
-
Click Login;
-
Find another button
-
Click on another button
...
What is the problem?
So, the problem start occuring on step 6, just after clicking Login button (See code execution process, step 6).
After it clicks on login button, it loads new page (it doesn't open it in new tab), and after that it didn't want to work.
What have you tried to do to solve the problem
First thing that came up to mine mind was to use driver.wait(5)
just after clicking on login, which didn't work.
I've also tried to check if any other element on that page will result in action .click()
After that, I've tried Waits - SELENIUM WAITS ; which also didn't work. I tried two possible options: driver.implicitly_wait(10)
and WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,"//*[@id=\"mmManagDiag\"]"))
. It also gave me errors how my USB Device isn't working while using explicit waits.
None of these worked.
What worked for me?
After working around it came up to my mind to try with time.sleep(3)
and suddenly it worked.
My final question So, I am wondering why Selenium couldn't find any element on that page before I used time.sleep()?
And isn't selenium waits and time.sleep()
almost the same thing?
Code
driver = webdriver.Chrome('./chromedriver')
driver.get(getGateway())
#Frm_Username
username = driver.find_element(By.NAME,'Frm_Username')
username.clear()
username.send_keys("user")
#Frm_Password
passwrd = driver.find_element(By.NAME,"Frm_Password")
passwrd.clear()
passwrd.send_keys("user")
#passwrd.send_keys(Keys.RETURN)
driver.find_element(By.XPATH,"//*[@id=\"LoginId\"]").click()
time.sleep(3)
driver.find_element(By.XPATH,"//*[@id=\"mmManagDiag\"]").click()
time.sleep(3)
driver.find_element(By.XPATH,"//*[@id=\"mmManagDevice\"]").click()
time.sleep(3)
driver.find_element(By.XPATH,"//*[@id=\"Btn_restart\"]").click()
time.sleep(3)
driver.find_element(By.XPATH,"//*[@id=\"confirmOK\"]").click()
driver.quit()
Solution 1:
Waiting for the elements presence might be not enough to click on it.
Try to wait for element to be clickable.
WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "your xpath value"))
).click()