0

Information:

So currently im writing a Python programm which allows me to check if any four letter usernames are still valid on Ps4 website.

Problem: It works pretty fine until a random error comes up. I have literally tried everything to avoid this error, but haven't found a solution. My program works like this:

On the PSN website there is a field where u can change your username. I have all my four-letter names in a text file. To try each of them I wrote this:

 with open("names.txt", "r+", encoding="utf-8") as file:
    lines = file.readlines()
    wait.until(EC.element_to_be_clickable((By.ID, input_id))) # Waits for input after redirect
    for line in lines: # goes through every four-letter name
        driver.find_element(By.ID, input_id).send_keys(line)
        driver.find_element(By.CLASS_NAME, primary_button_class).click()
        time.sleep(0.5) #This is where it fails
        driver.find_element(By.ID, input_id).clear()
        wait.until(EC.element_to_be_clickable((By.ID, input_id)))
        

On the time.sleep(0.5) it throws an error because it doesnt find the field after the first try. I figured out that it needs more time, so I tried 1sec which works fine until it gets to 300-1000 tries. Than it fails randomly. I also have tried to wait for the element with EC.waituntil... Question: Why does it fail after a view thousend or hundrets of tries if I time.sleep(1). And why does it fail if I "wait" for the element to load with wait.until(EC.element_to_be_clickable((By.ID, input_id)))

how do i fix it, that it throws no error after n tries?

1 Answers1

1

UI tests becomes slow when you run the same "test" in a loop. As the loops becomes bigger it takes more time to find the elements + it takes the site more time to respond. Try setting the sleep to 5 seconds and more and for sure you will get better results.

If you want a better way rather than Thread.Sleep or wait for 1 or 5 seconds, please read about WebDriverWait. Using this object you can set a maximum threshold for the element to be found. If you set the timeout (the threshold) to 5 seconds it means that each element has up tp 5 seconds to be found in the DOM / webpage. Yet, the element may be found after 1 or 0.5 seconds so it is good for performance as well.

Tal Angel
  • 1,301
  • 3
  • 29
  • 63