0

I have to add stopwatch to my script

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

start = time.clock()

driver = webdriver.Firefox()
driver.get('https://ebay.com')

after this code it should stay for a while(5 seconds), than do other staff, like this:

element = driver.find_element_by_id("username")
element.send_keys("some text")

How to do it?

Hero Guy
  • 217
  • 1
  • 4
  • 11
  • You need to use [waits](http://selenium-python.readthedocs.io/waits.html#waits) then, which will wait until element present. – Saurabh Gaur Jul 27 '17 at 13:45
  • @SaurabhGaur I found "until". Can i use it like, Until appearing element_by_id("username"). Can you show how to do it, please – Hero Guy Jul 27 '17 at 13:49
  • there are many example around it, follow [this](https://stackoverflow.com/questions/26566799/selenium-python-how-to-wait-until-the-page-is-loaded) or [this](https://stackoverflow.com/questions/32717334/wait-until-element-is-not-present)..Thanks – Saurabh Gaur Jul 27 '17 at 13:54
  • @NurislomTuraev Why do you want to wait for 5 secs? How about providing the `username` as soon as the field is available? – undetected Selenium Jul 27 '17 at 14:21
  • @DebanjanB Because I want to use it on many websites, even where has cloudflare protection. So thats why I need it. – Hero Guy Jul 28 '17 at 05:37
  • Wouldn't providing the "username" as soon as the `username` field is available to be filled up, suffice to your needs? Thanks – undetected Selenium Jul 28 '17 at 05:49
  • @DebanjanB This problem is related to another website, but I can't show it. There are some my private deal with that page. So I need stopwatch until appears some fields. This is a problem. Have any ideas? – Hero Guy Jul 28 '17 at 06:06
  • Okay, I understand your problem. But I am double mined now. Without any visibility to your Business Case it sounds like `time.sleep(n)` is the solution which is against all Best Practices. But again how can you conclude that `5 seconds` thresh-hold will suffice in all of your cases? Thanks – undetected Selenium Jul 28 '17 at 06:10
  • I solved problem. Thank you to everybody – Hero Guy Jul 28 '17 at 06:14

1 Answers1

1

I spent many hours and with help of God and many people from StackOverflow, I finally solved it.

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException

driver = webdriver.Firefox()
driver.get(''https://ebay.com'')
delay = 30

try:
    myElem = WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.ID, 'username')))
    element = driver.find_element_by_id("username")
    element.send_keys("some text")
except TimeoutException:
    print ("Loading took too much time!")

It would be the best, in my opinion.

Hero Guy
  • 217
  • 1
  • 4
  • 11