php - 如何通过 Selenium 找到元素
问题描述
我将 Selenium 与 PHP WebDriver 一起使用。我正在尝试单击导航栏中的链接,该链接显示在 HTML 中,如下所示:
<li class=""><a href="javascript:;" showpage="cards"><i class="zzdi zzdi-closed-caption zzdi-hx-fw"></i> User Cards</a></li>
我正在尝试选择元素然后单击它。但是,当我尝试按如下方式使用 xpath 时:
//a[@showpage="cards"]
或者
//*[@showpage="cards"]
我收到以下错误:
没有这样的元素:无法找到元素:{"method":"xpath","selector":"/*[@showpage="cards"]"}(会话信息:chrome=70.0.3538.110)(驱动程序信息:chromedriver =2.43.600233 (523efee95e3d68b8719b3a1c83051aa63aa6b10d),platform=Linux 4.15.0-43-generic x86_64) (警告:服务器未提供任何堆栈跟踪信息)命令持续时间或超时:24 毫秒有关此错误的文档,请访问: http: //seleniumhq.org/exceptions/no_such_element.html构建信息:版本:'3.4.0',修订:'未知',时间:'未知'系统信息:主机:'ubuntu',ip:'127.0.1.1',os.name:'Linux',os.arch : 'amd64', os.version: '4.15.0-43-generic', java.version: '1.8.0_191' 驱动信息: org.openqa.selenium.chrome.ChromeDriver Capabilities [{mobileEmulationEnabled=false, hasTouchScreen=false ,平台=LINUX,acceptSslCerts=false,goog:chromeOptions={debuggerAddress=localhost:43431},acceptInsecureCerts=false,webStorageEnabled=true,browserName=chrome,takeScreenshot=true,javascriptEnabled=true,setWindowRect=true,unexpectedAlertBehaviour=,applicationCacheEnabled=假,可旋转=假,networkConnectionEnabled=假,铬= {chromedriverVersion = 2.43.600233(523efee95e3d68b8719b3a1c83051aa63aa6b10d),userDataDir=/tmp/.org.chromium.Chromium.Teyqo7},takeHeapSnapshot=true,pageLoadStrategy=normal,databaseEnabled=false,handlesAlerts=true,version=70.0.3538.110,browserConnectionEnabled=false,nativeEvents=true,locationContextEnabled=true,cssSelectorsEnabled =true}] 会话 ID:367080a50b61c75a8c16a17b954a5972 *** 元素信息:{Using=xpath, value=/*[@showpage="cards"]}
任何有关如何使用 xpath 或如何使用任何其他使我能够单击链接的选择方法的帮助将不胜感激。
解决方案
您可能需要等待元素能够单击它:
$driver->wait()->until(
WebDriverExpectedCondition::elementToBeClickable(WebDriverBy::xpath('//a[@showpage="cards"]')))->click();
推荐阅读
- bash - 在shell中的某个位置替换两个分隔符shell之间的字符串
- c# - 找到循环中的最大数字
- c# - 如何表示足球比赛模型?
- docker - docker compose 文件无效,不支持的配置选项“container-name”
- reactjs - React Native Contacts 我无法删除联系人
- swift - 如何在 Xcode 的包管理器中为包创建 Package.swift 文件?
- php - 将多个 RewriteRule 指令组合成 .htaccess 中的单个规则
- javascript - 具有前项索引的JS组数组
- linux - AWS Linux t2.micro 上的 Yum Lock
- javascript - 如何在服务器中运行或安装不带 node.js 的 discord.js(部署)