1

The program works like this:

  1. I find an element by name
  2. I click on the element, and it makes the element in 3 appear
  3. I find the other element by link text
  4. I click on it.

The problem that I have is that 3 happens too quick and the program is unable to locate the element. I think I need to put a delay or something in 3 that activates 4 when the element is found. Also I'm using Selenium if that helps.

I haven't been able to try anything because I have no idea of what I can do, because I'm very new to this.

Here is the code:

atc = browser.find_element_by_name('commit')
atc.click()
checkout = browser.find_element_by_link_text('checkout now')
checkout.click()
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ZaKh
  • 11
  • 2
  • Aside from use of `time` package, which is fine, have a look at https://selenium-python.readthedocs.io/waits.html – Lante Dellarovere Apr 14 '19 at 21:54
  • Import time module and use `time.sleep(2000)`.. I'm getting sleepy ;p – ZF007 Apr 14 '19 at 22:06
  • This has ***nothing*** to do with Selenium WebDriver. It is about sleep in Python and thus there are probably a gazillion duplicate questions (more than 10 years worth). The canonical for that is *[How do I get my Python program to sleep for 50 milliseconds?](https://stackoverflow.com/q/377454)* (though none of the answers really address the common problem with a 16.66 ms time resolution for sub-second sleep) - probably not an issue in this particular case. – Peter Mortensen Feb 06 '21 at 23:23

3 Answers3

1

This has been asked and answered on here several times. You could do

import time
time.sleep(5)   # Delays for 5 seconds.

taken from (How can I make a time delay in Python?)

emilaz
  • 1,722
  • 1
  • 15
  • 31
0

Maybe you can use Time (https://docs.python.org/2/library/time.html) library :

import time
atc = browser.find_element_by_name('commit')
atc.click()
time.sleep(5)
checkout = browser.find_element_by_link_text('checkout now')
checkout.click()

This impose a delay of 5 milliseconds between step 3 and 4.

Andrea Barnabò
  • 534
  • 6
  • 19
0

In your question:

"I think I need to put a delay or something in 3 that activates 4 when the element is found."

I think the right option is the second one: "or something". You should to learn about to wait for an element, quoting Selenium Wait docs:

if an element is not yet present in the DOM, a locate function will raise an ElementNotVisibleException exception. Using waits, we can solve this issue. Waiting provides some slack between actions performed - mostly locating an element or any other operation with the element.

An example on this site: https://stackoverflow.com/a/25851841

dani herrera
  • 48,760
  • 8
  • 117
  • 177