首页 > 解决方案 > 无法定位元素 - 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>

标签: javascriptselenium

解决方案


很难确切地说出代码是什么,因为您没有提供当前的硒测试,而且我没有看到运输链接,但是您可以尝试这样的事情

// 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);
});

推荐阅读