Python Selenium如何在单击链接之前等待 [英] Python Selenium how to wait before clicking on link
问题描述
我只是想知道,如何让浏览器在单击链接之前等待?我的目标是从动态网页抓取内容,内容是动态的,但我设法获取表单ID.唯一的问题是,提交按钮仅在2-3秒后显示.但是,我的Firefox驱动程序在页面加载后(而不是动态部分)立即开始单击链接.
I just wonder, how to have the browser wait before clicking on a link? My goal is that I'm scraping from a dynamic webpage, the content is dynamic but I manage to get the form id. The only problem is that the submit button is only displayed after 2-3 seconds. However, my Firefox driver start clicking on the link immediately when the page is loaded (not the dynamic part).
有什么方法可以让我的浏览器等待2-3秒,直到出现提交按钮?我尝试使用time.sleep()
,但是它会暂停所有操作,time.sleep
期间,提交按钮不会出现,而是在2-3秒后出现.
Is there any way that I can make my browser wait 2-3 seconds until the submit button appears? I tried to use time.sleep()
but it pauses everything, the submit button doesn't appear during time.sleep
but appears after 2-3 seconds when time.sleep
ends.
推荐答案
您可以按以下方式设置等待时间:
You can set wait like following :
明确等待:
element = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.ID, "myElement"))
隐式等待:
driver.implicitly_wait(20) # seconds
driver.get("Your-URL")
myElement = driver.find_element_by_id("myElement")
您可以使用以上任何一种.两者都有效.
You can use any of above. Both are valid.
这篇关于Python Selenium如何在单击链接之前等待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!