gpt4 book ai didi

php - 使用 PHP Selenium 2 Webdriver 检查元素是否存在?

转载 作者:可可西里 更新时间:2023-11-01 13:41:27 29 4
gpt4 key购买 nike

我正在尝试使用 Selenium 2 通过 CSS 检查页面上的元素是否存在。任何人都有使用 PHP Selenium Webdriver 的示例 Facebook包装器?

我试过下面的代码:

if($driver->findElement(WebDriverBy::xpath("image-e4e")) != 0)
{
}

但是给我这个错误:

Fatal error: Uncaught exception 'NoSuchElementWebDriverError' with message 'Unable to locate element: {"method":"xpath","selector":"image-e4e"}

最佳答案

根据设计,findElement 如果找到则返回 WebDriverElement,但如果找不到则抛出异常。

要检查元素是否在页面上而不出现异常,诀窍是使用 findElementsfindElements 返回在页面上找到的所有元素的数组。如果没有找到,它返回一个空数组。

if (count($driver->findElements(WebDriverBy::xpath("image-e4e"))) === 0) {
echo 'not found';
}

关于php - 使用 PHP Selenium 2 Webdriver 检查元素是否存在?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19496802/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com