自定义is_element_present执行速度慢

发布于 2024-10-07 05:54:58 字数 554 浏览 0 评论 0原文

我在另一个方法的方法体中:

for i in range(60):
    try:
        if sel.is_element_present("//div[@id='result']/form[3]/strong/div/button"): break
    except: pass
    time.sleep(1)

它在 5 秒内执行。

现场没有任何变化,我执行了这一行:

self.WaitForElement(u"//div[@id='result']/form[3]/strong/div/button")     


def WaitForElement(self,name):
    for i in range(60):
        try:
            if sel.is_element_present(name): break
        except: pass
        time.sleep(1)

它执行了近 30 秒,所以这很奇怪。

你有什么想法吗?

I have in method body of another method:

for i in range(60):
    try:
        if sel.is_element_present("//div[@id='result']/form[3]/strong/div/button"): break
    except: pass
    time.sleep(1)

and it executes in 5 seconds.

Nothing changes on site and I execute this line:

self.WaitForElement(u"//div[@id='result']/form[3]/strong/div/button")     


def WaitForElement(self,name):
    for i in range(60):
        try:
            if sel.is_element_present(name): break
        except: pass
        time.sleep(1)

and it executes in almost 30 seconds, so it's very weird.

Do you have any idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半﹌身腐败 2024-10-14 05:54:58

快速查看您的代码,我唯一能看到的是您在第一个示例中定义了一个标准字符串,但在第二个示例中定义了一个 unicode 字符串。

Looking quickly over your code, the only thing I can see is you defined a standard string in the first example but a unicode string in the second.

忆梦 2024-10-14 05:54:58

以下是 is_element_present 方法的内容:

   def is_element_present(self, how, what):
    try: self.driver.find_element(by=how, value=what)
    except NoSuchElementException, e: return False
    return True

由于它已经执行了 try... except 块,因此您不需要再次将其包装在 try... except 中。由于 is_element_present 只会返回 True 或 False,因此代码的 except: pass 部分可能永远不会被执行。相反,只需使用 if 语句即可。

我还认为您的代码中有一个拼写错误(“sel”而不是“self”)。

Here is what I have for the is_element_present method:

   def is_element_present(self, how, what):
    try: self.driver.find_element(by=how, value=what)
    except NoSuchElementException, e: return False
    return True

Since it is already doing a try...except block, you don't need to wrap it in try...except again. Since is_element_present will only return True or False, the except: pass part of your code will probably never be executed. Instead, just use the if statement.

I also think you have a typo in your code ("sel" instead of "self").

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文