简体   繁体   English

如何让 selenium 等到找到元素而不是使用 time.sleep()

[英]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.

相关问题 将 time.sleep 与 selenium 一起使用(睡眠直到完全加载某些内容) - Using time.sleep with selenium (sleep until something is completly loaded) time.sleep给出了所需的刮擦但等到没有 - time.sleep gives desired scrape but wait until does not 如何在不使用 time.sleep 方法的情况下在 python 中进行倒计时? - How to make countdown in python without using time.sleep method? 不使用Time.Sleep()将多个线程置于睡眠/等待状态 - Put several Threads in sleep/wait not using Time.Sleep() 如何使用 PySimpleGUI 使 time.sleep 成为输入 - How to make the time.sleep an input using PySimpleGUI 相当于 Selenium 中的 time.sleep() - Equivalent of time.sleep() in Selenium 尽管添加了 time.sleep(),但 Selenium 无法定位元素 - Selenium unable to locate element despite adding time.sleep() 我能否等到一个 object 被销毁,直到它在循环中创建另一个和 time.sleep() - Am I able to wait until one object is destroyed until it creates another and time.sleep() in a loop Selenium Python 中不使用 time.sleep() 无法打印文本 - Not able to print the text without using time.sleep() in Selenium Python 在 selenium 中替代 time.sleep() 使用 python 而 web 抓取? - Alternative to time.sleep() in selenium using python while web scraping?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM