如何单击 Robot Framework Selenium 中的元素

How to click on an element in Robot Framework Selenium

如何使用 Selenium2Library 为我的测试用例编写关键字以单击没有元素 ID 的按钮?

下面是 html 代码。我是硒的新手!

<a href="redirect.cfm?app=profile">
<div id="profile" class="icon">
<img style="display:block; margin:0 auto;" src="assets/images/profile.png">
<h3>Profile</h3>
</div>
</a>

我试过了

driver.findElement(By.Xpath("//img[@src='assets/images/profile.png']")).click();

但出现错误:

LoginTest
| FAIL | ValueError: Element locator with prefix 'driver.findElement(By.Xpath("//img[@ src' is not supported

如果您实际使用的是 Robot Framework,那么要使用该功能,您只需确保已导入该库,然后使用您需要的关键字。

明确地说,在设置部分:

Library     Selenium2Library 

并且在测试用例或关键字中:

Selenium2library.Click Element    xpath=//img[contains(@src, 'profile.png')]

假设你的 xpath 是正确的。鉴于定位器不佳,通常您要做的是将 ID 添加到您需要的东西中。