[英]How to make selenium wait until the element is found instead of using time.sleep()
Basically the tittle, i'm building a small script to automate some basic stuff.基本上,我正在构建一个小脚本来自动化一些基本的东西。 I've been using time.sleep() to make the software wait a little until everything is loaded, but is there a better way to do this??我一直在使用 time.sleep() 让软件稍等片刻,直到所有内容都加载完毕,但有更好的方法吗?
I want the script to wait as long as necessary by itself to make things faster and more clean.我希望脚本本身等待尽可能长的时间,以使事情变得更快、更干净。
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import Select
import time
from tkinter import *
def cs_update():
PATH = "C:\Program Files (x86)\chromedriver.exe"
options = Options()
options.headless = True
driver = webdriver.Chrome(PATH,options = options)
driver.set_window_size(1920,1080)
driver.minimize_window()
user_email = e.get()
user_password = e2.get()
driver.get("https://www.compraensanjuan.com")
time.sleep(3)
link = driver.find_element_by_link_text("Mi cuenta")
link.click()
time.sleep(3)
email = driver.find_element_by_name("email")
email.send_keys(user_email)
print("I sent the keys")
This is how you can use ExplicitWait这就是您可以使用ExplicitWait的方式
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver.get("https://www.compraensanjuan.com")
timeout = 20 # set seconds to wait until TimeOutException raised
link = WebDriverWait(driver, timeout).until(EC.element_to_be_clickable((By.LINK_TEXT, "Mi cuenta")))
link.click()
email = WebDriverWait(driver, timeout).until(EC.presence_of_element_located((By.NAME, "email")))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.