Python 每次操作之间的等待时间
您好,我是python新手,我正在尝试创建一个自动机器人(我对python非常陌生),它登录instagram并喜欢一定数量的帖子,但我正在尝试找出如何在输入用户名信息和密码时添加延迟,但我不确定如何进行,此外,如果您有任何反馈/建议,我将不胜感激。谢谢。这是我到目前为止的代码Python 每次操作之间的等待时间,python,python-3.x,selenium,selenium-webdriver,Python,Python 3.x,Selenium,Selenium Webdriver,您好,我是python新手,我正在尝试创建一个自动机器人(我对python非常陌生),它登录instagram并喜欢一定数量的帖子,但我正在尝试找出如何在输入用户名信息和密码时添加延迟,但我不确定如何进行,此外,如果您有任何反馈/建议,我将不胜感激。谢谢。这是我到目前为止的代码 from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdr
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
browser = webdriver.Chrome()
def site_login():
browser.get('https://www.instagram.com/')
browser.find_element_by_name("username").send_keys(‘username’)
browser.find_element_by_name("password").send_keys(“password”)
browser.find_element_by_name("Log In").click() #not sure if this works
时间模块应该对您有所帮助
import time
time.sleep(seconds) #Enter the time in seconds here
时间模块应该对您有所帮助
import time
time.sleep(seconds) #Enter the time in seconds here
如果要等待指定的时间段,则可以使用需要导入时间的
time.sleep(timeinsectonds)
import time
time.sleep(number_of_seconds)
但是,无论如何,我想使用显式等待。与硬超时不同,这将等待条件满足后继续脚本
# needed the imports
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
# wait for the element and click (using xpath locator)
WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH, "xpath_goes_here"))).click()
# wait for the element and enter value (using css locator)
WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "css_locator_goes_here")))).send_keys("enter input")
# store the element and then perform action
loginButton = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "css_locator_goes_here"))))
loginButton.click()
您可以使用CSS或xpath定位策略
您还可以通过在下面的代码行中添加代码,在驱动程序级别使用隐式等待
browser.implicitly_wait(10)
如果要等待指定的时间段,则可以使用需要导入时间的
time.sleep(timeinsectonds)
import time
time.sleep(number_of_seconds)
但是,无论如何,我想使用显式等待。与硬超时不同,这将等待条件满足后继续脚本
# needed the imports
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
# wait for the element and click (using xpath locator)
WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH, "xpath_goes_here"))).click()
# wait for the element and enter value (using css locator)
WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "css_locator_goes_here")))).send_keys("enter input")
# store the element and then perform action
loginButton = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "css_locator_goes_here"))))
loginButton.click()
您可以使用CSS或xpath定位策略
您还可以通过在下面的代码行中添加代码,在驱动程序级别使用隐式等待
browser.implicitly_wait(10)
我建议在前面的注释中使用显式等待,而不是使用
time.sleep(seconds)
。由于环境的变化,时间可能会有所不同。所以最好使用显式等待。使用time.sleep(秒)
将休眠当前线程。这是个坏习惯
谢谢我建议在前面的评论中使用显式等待,而不是使用
time.sleep(seconds)
。由于环境的变化,时间可能会有所不同。所以最好使用显式等待。使用time.sleep(秒)
将休眠当前线程。这是个坏习惯
谢谢