相同的命令在执行时可以工作一次,但是在第二次执行时会抛出异常? [英] Same command works once when executed but throws an exception when executed a second time?

查看:90
本文介绍了相同的命令在执行时可以工作一次,但是在第二次执行时会抛出异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我很新,试图找出一些在python中使用的东西.

So I am very new and trying to figure out something for use in python.

元素:

<div id="scroll2" class="fm2 p8 cur m_bt2" onclick="javascript:displayResultsLogin('scroll2')"> Show More Results </div>

Xpath://*[@id="scroll2"]

我使用shell执行:

I execute, using shell:

wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.XPATH, "//*[contains(text(), ' Show More Results')]")))
element.click()

执行上述操作后,元素:

Once the above has executed, the element:

<div id="scroll3" class="fm2 p8 cur m_bt2" onclick="javascript:displayResultsLogin('scroll3')"> Show More Results </div>

XPath://*[@id="scroll3"]

当我再次执行同一命令时,它会引发异常:

When I execute the same command again, it throws an exception:

wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.XPATH, "//*[contains(text(), ' Show More Results')]")))
element.click()

例外

回溯(最近通话最近): 文件",第1行,在 元素= wait.until(EC.element_to_be_clickable((By.XPATH,"//* [包含(text(),'显示更多结果')]"))))) 文件"C:\ Python34 \ lib \ site-packages \ selenium \ webdriver \ support \ wait.py",第80行,直到 引发TimeoutException(消息,屏幕,堆栈跟踪) selenium.common.exceptions.TimeoutException:消息:

Traceback (most recent call last): File "", line 1, in element = wait.until(EC.element_to_be_clickable((By.XPATH, "//*[contains(text(), ' Show More Results')]"))) File "C:\Python34\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message:

无法理解原因.或解决方法.

Unable to understand why. Or a way around it.

推荐答案

我坚信这与您正在测试的应用程序的构建方式有关:似乎单击 scroll2 按钮将使该按钮不可见,并在页面上添加 scroll3 按钮.

I strongly believe this has to do with the way that the application you’re testing is built: It seems like clicking scroll2 button would make that one invisible and add the scroll3 button to the page.

然后,当您调用 wait.until(...)时,WebDriver会找到 scroll2 按钮(因为XPath表达式仅检查内部文本仍会与之匹配),然后等待其变为可点击状态,因为它不再可见,因此永远不会被单击.

After that, when you call wait.until(...) what happens is that WebDriver will find the scroll2 button (because the XPath expression that only checks for the inner text will still match it), and then will wait for it to get clickable, which it never will because it's no longer visible.

您可以使用以下简单的网页重新创建它:

You can recreate that with a simple web page like this:

<html>
    <head>    
        <script>
            document.onreadystatechange = function() {
                var element = document.getElementById("1");

                element.addEventListener("click", function() {
                    this.style.display = "none";

                    document.getElementById("2").style = "";
                });
            }
        </script>
    </head>
    <body>
        <div id="1">Show More Results</div>
        <div id="2" style="display:none">Show More Results</div>
    </body>
</html>

这个python代码段:

And this python snippet:

wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.XPATH, "//*[contains(text(), 'Show More Results')]")))
element.click()

wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.XPATH, "//*[contains(text(), 'Show More Results')]")))
element.click()

即使页面外观看上去正确,第二个 wait.until(...)也将超时.

Here also the second wait.until(...) will time out even though the page visually looks correct.

基本上,有两种解决方案:

Essentially there are two solutions to this:

  • 更改HTML,以便一旦添加了scroll3按钮,则实际上已从DOM中删除了scroll2按钮,或者
  • 更改测试,以便它可以更稳定地找到要单击的按钮.例如,对于上述示例,您可以将显示样式添加到XPath定位器中,以确保仅查找显示的元素.

示例:

wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.XPATH, "//*[contains(text(), 'Show More Results') and not(contains(@style, 'display: none'))]")))
element.click()

wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.XPATH, "//*[contains(text(), 'Show More Results') and not(contains(@style, 'display: none'))]")))
element.click()

请注意,您可能还必须在定位器中包括祖先可见性,有关详细信息,请参见以下问题:

Note that you might have to include ancestor visibility as well in your locator, for details see this question: How do I select only visible elements using XPath?

这篇关于相同的命令在执行时可以工作一次,但是在第二次执行时会抛出异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆