Reputation: 1
import pyautogui
import selenium
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
Hello! So I have been able to detect in Selenium Solving is in process...
http://prntscr.com/pib0pf and than if the Element is Active, a time.sleep()
will be activated before the rest of the code continues. To give Captcha some time before it is solved. But the thing is that I changed my mind and thought it would be actually better if I found a way to make selenium check for the element and if the element is not yet available, a time.sleep()
should be executed. I want this because the first idea will run into errors if the captcha is not solved in the given time. But with the second idea, Selenium will automatically check if the given Element is active, and if not, a 30 seconds should be added to the script before the rest of the code executes.
#~ Continuing code
time.sleep(3)
print("Form filled!")
time.sleep(10)
if driver.find_element_by_xpath("//div[@class='antigate_solver recaptcha in_process']"):
print("Waiting 60 seconds...\n")
time.sleep(60)
if driver.find_element_by_xpath("//div[@class='antigate_solver recaptcha solved']"):
time.sleep(1.5)
print("Captcha Solved...")
driver.find_element_by_xpath('/html[1]/body[1]/main[1]/div[1]/div[2]/form[1]/small[1]/div[1]/label[1]/input[1]').click()
print("Submitting...")
time.sleep(1.5)
driver.find_element_by_xpath('/html[1]/body[1]/main[1]/div[1]/div[2]/form[1]/div[12]/button[1]').click()
Upvotes: 0
Views: 407
Reputation: 197
Explicit Waits
An explicit wait is a code you define to wait for a certain condition to occur before proceeding further in the code. The extreme case of this is time.sleep(), which sets the condition to an exact time period to wait. There are some convenience methods provided that help you write code that will wait only as long as required. WebDriverWait in combination with ExpectedCondition is one way this can be accomplished.
EXP:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox()
driver.get("http://somedomain/url_that_delays_loading")
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "myDynamicElement"))
)
finally:
driver.quit()
This waits up to 10 seconds before throwing a TimeoutException unless it finds the element to return within 10 seconds. WebDriverWait by default calls the ExpectedCondition every 500 milliseconds until it returns successfully. A successful return is for ExpectedCondition type is Boolean return true or not null return value for all other ExpectedCondition types.
Source : https://selenium-python.readthedocs.io/waits.html
Upvotes: 1