如果找不到元素,selenium webdriver将终止执行

时间:2015-03-26 07:40:19

标签: php selenium-webdriver

以下是我用于使用selenium web driver在某些第三方网站上发布数据的PHP代码

$webdriver = new WebDriver("localhost", "4444");
$webdriver->connect("firefox");
 try{       
        $webdriver->get(trim($push[$key]['app']));
        sleep(4);
        if($webdriver->findElementBy(LocatorStrategy::xpath,'//a[@ng-click="vm.isEditorEdit=true;"]')){
            $editBtn=$webdriver-findElementBy(LocatorStrategy::xpath,'//a[@ng-click="vm.isEditorEdit=true;"]');
            $editBtn->click();
            sleep(2);
        }
        $pushTitle=$webdriver-findElementBy(LocatorStrategy::xpath,'//input[1]');
        $pushTitle->sendKeys(array($push[$key]['title']));
        $webdriver-executeScript("tinyMCE.activeEditor.setContent('".$push[$key]['message']."')",array());
        $sendBtn=$webdriver-findElementBy(LocatorStrategy::xpath,'//a[@id="sendButton"]');
        $sendBtn->click();      sleep(2);
        if($webdriver->findElementBy(LocatorStrategy::xpath,'//a[@ng-click="onSave();"]'))
        {
            $sureBtn=$webdriver-findElementBy(LocatorStrategy::xpath,'//a[@ng-click="onSave();"]');
            $sureBtn->click();
            sleep(5);
        }
    }catch (Exception $e) {
        echo 'Caught exception: ',  $e->getMessage(), "\n"; 
    }   

看到有两个元素($ editBtn,$ sureBtn)总是不存在,所以我把它们放进去了

  

如果条件

如果$ editBtn和$ sureBtn出现在我用来发布数据的页面上,

代码工作正常,否则会以错误“捕获异常:没有这样的元素异常”终止,但脚本不应该终止。

请建议我解决。

感谢。

0 个答案:

没有答案