简体   繁体   English

如何验证硒webdriver中是否存在浏览器

[英]How to verify whether a browser exists in selenium webdriver

I am using webdriver to open a Firefox browser and iteratively pass a URL to it using a spreadsheet (that contains 5 URLs in different cells of column 1). 我正在使用webdriver打开Firefox浏览器,并使用电子表格(在第1列的不同单元格中包含5个URL)迭代地向其传递URL。 Now if suppose i manually close the firefox browser after two URLs were opened. 现在,假设在打开两个URL之后,我手动关闭了firefox浏览器。 I want my script to handle this situation and continue the execution for which i tried the below three conditions, however everytime i get a Null pointer exception. 我希望我的脚本能够处理这种情况,并继续尝试以下三种条件进行执行,但是每次我得到一个Null指针异常时,都可以。 Please suggest how to handle this situation. 请建议如何处理这种情况。

if (driver.toString().contains("null"))
if (driver.getTitle().contains("null")) 
if (driver.getCurrentUrl().contains("null")) 

There is no inbuilt method for that However you can use this as a workaround 没有内置的方法,但是您可以将其用作解决方法

/**
 * 
 * @return true if driver is alive else false
 */
public Boolean isAlive() {
    try {
        driver.getCurrentUrl();//or driver.getTitle();
        return true;
    } catch (Exception ex) {
        return false;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM