如何在python中设置两个操作之间的延迟?

2024-04-16 08:25:23 发布

您现在位置:Python中文网/ 问答频道 /正文

程序的工作原理如下:

  1. 我找到一个元素的名字
  2. 我单击元素,它会使3中的元素出现
  3. 我通过链接文本找到另一个元素
  4. 我点击它。你知道吗

我的问题是3发生得太快,程序无法定位元素。我想我需要在3中加一个延迟或者什么东西,当元素被找到时激活4。如果有帮助的话,我也在用硒。你知道吗

我什么都没试过,因为我不知道我能做什么,因为我对这个很陌生。你知道吗

代码如下:

atc = browser.find_element_by_name('commit')
atc.click()
checkout = browser.find_element_by_link_text('checkout now')
checkout.click()

Tags: 定位文本程序browser元素by链接element
3条回答

这个问题在这里已经被问了好几次了。你能做到的

import time
time.sleep(5)   # Delays for 5 seconds.

取自(How can I make a time delay in Python?

也许您可以使用Timehttps://docs.python.org/2/library/time.html)库:

import time
atc = browser.find_element_by_name('commit')
atc.click()
time.sleep(5)
checkout = browser.find_element_by_link_text('checkout now')
checkout.click()

这将在步骤3和步骤4之间施加5毫秒的延迟。你知道吗

在你的问题中:

"I think I need to put a delay or something in 3 that activates 4 when the element is found."

我认为正确的选择是第二个:“或者别的什么”。您应该了解如何等待元素,并引用Selenium Wait docs

if an element is not yet present in the DOM, a locate function will raise an ElementNotVisibleException exception. Using waits, we can solve this issue. Waiting provides some slack between actions performed - mostly locating an element or any other operation with the element.

这个网站上的一个例子:https://stackoverflow.com/a/25851841

相关问题 更多 >