javascript - 无法定位元素 - Selenium Javascript 测试
问题描述
我有一个用 selenium 测试的 javascript。但我正面临这个问题
无法定位特定元素
执行点击操作。下面是我的 JS 的片段,我想点击 Shipping。通过 id、linktext、cssselector 等尝试了所有定位器,如 findelement,但没有任何效果。
<ul
className="navbar-nav bg-black sidebar sidebar-dark accordion"
id="accordionSidebar"
>
<Link
className="sidebar-brand d-flex align-items-center justify-content-center"
to="/ONE"
>
<div className="sidebar-brand-text mx-3">
<img src={logo} width={220} height={60}/>
</div>
</Link>
<hr className="sidebar-divider my-0" />
<li className={`nav-item ${props.selectedTab === 'ONE' ? 'active' : ''}`}>
<Link className="nav-link" to="/ONE">
<i className="fas fa-fw fa-th" />
<span>ONE</span>
</Link>
</li>
<li className={`nav-item ${props.selectedTab === 'TWO' ? 'active' : ''}`}>
<Link className="nav-link" to="/TWO">
<i className="fas fa-fw fa-truck" />
<span>TWO</span>
</Link>
</li>
</ul>
解决方案
很难确切地说出代码是什么,因为您没有提供当前的硒测试,而且我没有看到运输链接,但是您可以尝试这样的事情
// Targeting a button element for this example
When('I click {string} button', async function(buttonText) {
const driver: webdriver.WebDriver = this.driver;
const buttonElement = await driver.findElement(
webdriver.By.xpath(`//button[contains(.,'${buttonText}')]`),
);
await buttonElement.sendKeys(webdriver.Key.ENTER);
});
推荐阅读
- c# - 如何在 AvalonEdit 中设置相等的字符间距,以使视觉行列与文档行列相同
- mongodb - Scala MongoDB,覆盖编解码器以写入/读取数组 [字节]
- python - "while" 在 "for" 中不起作用(完全忽略)
- java - 是否可以找到连接到我的服务器的远程客户端的侦听端口?
- java - 使用 Java 为 JDBC 连接填充 DB2 参数
- cocoa - 在 NSOutlineView 中编辑文本,如 Finder
- angular - Angular 7无法获取http标头
- c# - 枚举作为 ASP.NET Core WebAPI 中的必填字段
- python - 如何为图像切片选择具有不同可能组合的字典列表?
- python - 在数据框中查找模式并进行替换