Moshe
Moshe

Reputation: 117

repeat selenium command line

Good day, I am new in coding and have created a web automated script that logs into a specified web page. After logging in it I need to use a value from a span that displays/pops up numbers every second. Suppose the first number is 8.2165 and the one that follows it is 8.2984.I have used the following line to print the numbera = print(driver.find_element_by_id('spot').text). How do I print the second number(8.2984) because if I type b = print(WebDriverWait(driver, 1).until(EC.visibility_of_element_located((By.ID, 'spot'))).text) to wait one second to print the second number it still returns the same number8.2165.

Statement:: a = print('first number') b = print('second number')

if a= 300.0 and b=6985 print('true')

Question:: how do I loop through the statement in Selenium Python script?

Upvotes: 0

Views: 88

Answers (1)

JeffC
JeffC

Reputation: 25645

A couple things:

  1. WebDriverWait(driver, 1).until(EC.visibility_of_element_located((By.ID, 'spot') doesn't wait 1s... it actually waits up to 1s for the element to become visible. It polls the DOM for the element every 250ms until the element becomes visible or times out. I think the issue you are running into is that on the second call, the popup is currently up so it meets the wait criteria so the same number is printed again.
  2. The second issue is that you need to wait for the first popup to disappear and then wait for the second popup to appear. One way to do this is to wait for the first popup to become stale. A stale element is one that no longer is attached to the DOM (it no longer exists). The first popup will appear and then when it disappears, it will become stale or detached from the DOM. In order to wait for an element to become stale, you have to grab a reference to that element (store it in a variable) and then use WebDriverWait to wait for it to become stale.

On to the code...

# store the WebDriverWait instance in a variable for reuse
wait = WebDriverWait(driver, 3)

# wait for the first popup to appear
popup = wait.until(EC.visibility_of_element_located((By.ID, 'spot')))
# print the text
print(popup.text)
# wait for the first popup to disappear
wait.until(EC.staleness_of(popup))

# wait for the second popup to appear
popup = wait.until(EC.visibility_of_element_located((By.ID, 'spot')))
# print the text
print(popup.text)
# wait for the second popup to disappear
wait.until(EC.staleness_of(popup))

... and so on

As you can see, the code is the same for each popup and so can be looped, if you wanted.

Upvotes: 1

Related Questions