Is there anyway to stop implicity wait during try/except? - python-3.x

I have a selenium script that automates signing up on a website. During the process, I have driver.implicity_wait(60) BUT there is a segment of code where I have a try/except statement where it tries to click something but if it can't be found, it continues. The issue is that if the element isn't there to be clicked, it waits 60 seconds before doing the except part of code. Is there anyway I can have it not wait the 60 seconds before doing the except part? Here is my code:
if PROXYSTATUS==False:
driver.find_element_by_css_selector("img[title='中国大陆']").click()
else:
try:
driver.find_element_by_css_selector("img[title='中国大陆']").click()
except:
pass
In other words if a proxy is used, a pop up will occasionally display, but sometimes it won't. That's why I need the try/except.

You can use set_page_load_timeout to change the default timeout to a lower value that suits you.
You will still need to wait for some amount of time, otherwise you might simply never click on the element you are looking for, because your script will be faster than the page load.

In the try block u can lower the timeout say 10 by using driver.implicity_wait(10) or even to 0. Place this before the find element statement in the try block. Add a finally block and set this back to 60 driver.implicity_wait(60).

Related

Sikulix - exists function has timeout?

i have a sikulix code with 5 if statement like this :
if exists("1642200162130.png"):
It enter in only one if statement, where there is only one click()
click(Location(me.x + paddingx, me.y + paddingy))
For a complete run, the scritp take ~7seconds to execute. It's too slow for me. Do you know if there is a timeout on exists function ? And if we can lower it ?
Thank you !
Yes, as described in the documentation here, you can overload the exists() function like this:
exists(PS[, seconds])
Check whether the give pattern is visible on the screen.
Parameters:
PS – a Pattern object or a string (path to an image file or just plain text)
seconds – a number, which can have a fraction, as maximum waiting time in seconds. The internal granularity is milliseconds. If not specified, the auto wait timeout value set by Region.setAutoWaitTimeout() is used. Use the constant FOREVER to wait for an infinite time.

How to handle a Time Out?

I am running an Excel power query in a loop. The query runs.
For a reason related to the internet (I am not in a fiber covered area) the query fails to load the data, returning a time out error.
Given that is possible the entire loop cycle has not completed, I would like to stop the refresh before the error pops up and resume with the code despite no data having been loaded.
The code breaks where it is shown in the pic.
How can I have the code keep running before the time out will appear?
Let's say I would like the code to keep executing if after 90 seconds the data cannot be loaded.
Why don't you try to change the refresh period?
You can also try to look at the code generated by Power Query by Unchecking the "Enable Background Refresh" in the Data -> Connection -> Properties.
You can also add a timeout of your choice. You can add this bit after you defined the URL
, [Timeout = #duration (X,Y,Z,N)]
Where X is Days, Y is Hours, Z is Minutes, N is Seconds
Else if you are really interested on killing the Web Query after the default 100 seconds, then before starting the code you can put this line
On Error Resume Next

How can I make a variable dynamically change while on console python3

I am working on a program and it needs to display the number of seconds that passed since it started. The variable is inside input() like this:
while True:
something = input(str(x) + "seconds have passed. Please input the number in 60 seconds:")
if x == 60:
break
Is there any way to make the x change dynamically while on screen? So it raises every second and also updates on screen without the need to press enter.
If yes, is there also a way to add an if statement that checks when the variable reached 60 and break the loop.

Browser waits longer than expected

I have some code, in which I'm trying to scrape a website. After a while, I think I'm being slowed down by the site. I can't chekc that, but this is happening within my code
z=timeit.default_timer()
try:
WebDriverWait(browser,5).until(
EC.presence_of_element_located((By.XPATH,'''
.//div[#collectionitem="title"]/descendant::div[#class="titleWidgetLayout"]/
descendant::h1[#class="title"]''')))
except:
print('Web Scraper not loaded')
return 'Error Load'
n=timeit.default_timer()
print('Time actually waited',n-z)
I find that while at the beginning this time was about 1-2 seconds, it ends up turning into a 25 seconds wait. This not only slows down the code more than what is acceptable, how can the time waited be far longer than the 5 seconds I set up as timeout error trigger?
I guess this might be a blockage from the page, but in any case, how can I fix this?
This is most likely occurring because the webdriver is set to wait until the page is completely (until the load symbol of the page is gone) done loading before looking for any elements.
So, if you page takes 23 seconds to load, and then your element is located after 3 seconds after page load, you would not throw a timeout from your WebDriverWait condition.
You can try to set the Page Load Timeout with this:
browser.set_page_load_timeout(5)
This way, if the page is taking too long to load, you can skip it? Other than that, you will have to wait until the page load is complete.

I am trying to make an image stay put longer after an if statement occurs

I am trying to edit my game so that when an if statement occurs an image appears, but the if statement only happens when the object hits the wall and the ball bounces off, so the image only appears for the amount of time the if statement is active. I tried to make a delay command but after the if statement occurs it freezes the entire game for the given time.
Does anyone know how to isolate a delay command or make the image stay visible for an extra second after the if statement expires/after the ball hits the wall and bounces off???
Here are the lines of code that has to do with this:
image_set = pygame.display.set_mode()
image = pygame.image.load("image.png")
def image():
image_set.blit(image,(600,90))
if object.left == (COLLISION_RANGE) or object.right == (WINDOWWIDTH COLLISION_RANGE):
DisplayOoface()
Oof.play()
Put the code to "hide" the image into a separate function, Use the Threading package to create a new thread and pass that function in to be executed.
Within the function you can add a delay to the start of the code. When you start the additional thread within the if statement, you will have the delay you need, and then the function can hide the image.
If you do not thread this task, then python simply processes one line of code at a time. By passing off the task of waiting and hiding the image to a secondary thread, your main thread (the game) can continue functioning as needed.
https://docs.python.org/3/library/threading.html

Resources