如何在Selenium(Java)中使用If-else条件使else块中的条件起作用

时间:2019-04-10 20:32:47

标签: selenium

如果if块中的条件不起作用,我正在尝试在else块中找到一个元素。

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-277-628dc6c46325> in <module>()
      3 im = M[1,1:]
      4 im = im.reshape(28,28)
----> 5 plt.imshow(im)

C:\Users\mapppppp\AppData\Local\Enthought\Canopy\edm\envs\User\lib\site-packages\matplotlib\pyplot.py in imshow(X, cmap, norm, aspect, interpolation, alpha, vmin, vmax, origin, extent, shape, filternorm, filterrad, imlim, resample, url, hold, data, **kwargs)
   3156                         filternorm=filternorm, filterrad=filterrad,
   3157                         imlim=imlim, resample=resample, url=url, data=data,
-> 3158                         **kwargs)
   3159     finally:
   3160         ax._hold = washold

C:\Users\mapppppp\AppData\Local\Enthought\Canopy\edm\envs\User\lib\site-packages\matplotlib\__init__.py in inner(ax, *args, **kwargs)
   1890                     warnings.warn(msg % (label_namer, func.__name__),
   1891                                   RuntimeWarning, stacklevel=2)
-> 1892             return func(ax, *args, **kwargs)
   1893         pre_doc = inner.__doc__
   1894         if pre_doc is None:

C:\Users\mapppppp\AppData\Local\Enthought\Canopy\edm\envs\User\lib\site-packages\matplotlib\axes\_axes.py in imshow(self, X, cmap, norm, aspect, interpolation, alpha, vmin, vmax, origin, extent, shape, filternorm, filterrad, imlim, resample, url, **kwargs)
   5116                               resample=resample, **kwargs)
   5117 
-> 5118         im.set_data(X)
   5119         im.set_alpha(alpha)
   5120         if im.get_clip_path() is None:

C:\Users\mapppppp\AppData\Local\Enthought\Canopy\edm\envs\User\lib\site-packages\matplotlib\image.py in set_data(self, A)
    543         if (self._A.dtype != np.uint8 and
    544                 not np.can_cast(self._A.dtype, np.float)):
--> 545             raise TypeError("Image data can not convert to float")
    546 
    547         if (self._A.ndim not in (2, 3) or

TypeError: Image data can not convert to float

我收到一条错误消息:

  try
    {
       if(Driver.driver.findElement(By.xpath("//div[@id='Tpay_success']")).isDisplayed())
        {
            System.out.println("Payment is successful");
            Reporter.log("Payment is successful");
        }
        else
        {
            Thread.sleep(2000);
            {
            if(Driver.driver.findElement(By.id("pay_decline")).isEnabled()) 
             {
                System.out.println("pay declined");
                action.moveToElement(By.id("pay_decline")).isEnabled()).click().perform();
                Reporter.log("PAYMENT DECLINED!!");
              }

         }  
    catch(ExceptionInInitializerError ex)
    {
            System.out.println(ex);
    }
  }

如果if块没有被执行,我希望else块被执行。 欢迎任何建议。谢谢。

1 个答案:

答案 0 :(得分:1)

if(Driver.driver.findElement(By.xpath("//div[@id='Tpay_success']")).isDisplayed())

这不符合您的想法。如果当前加载页面的DOM中不存在该元素,则findElement()将引发错误。这意味着在这种情况下不会调用isDisplayed()

您可以使用try...catch做您想做的事情:

  try
    {
       Driver.driver.findElement(By.xpath("//div[@id='Tpay_success']"))
       System.out.println("Payment is successful");
       Reporter.log("Payment is successful");
    } catch(ExceptionInInitializerError ex) {
            Thread.sleep(2000);
            if(Driver.driver.findElement(By.id("pay_decline")).isEnabled()) {
                System.out.println("pay declined");
                action.moveToElement(By.id("pay_decline")).isEnabled()).click().perform();
                Reporter.log("PAYMENT DECLINED!!");
            }
    }

请注意,您应该了解如何使硒驱动程序等待特定元素,而不是使用Thread.sleep()