简体   繁体   English

Selenium Web驱动程序单击图像

[英]Selenium Web driver clicking images

How do I select in image on and click on it using Selenium web driver? 我如何选择图像并使用Selenium Web驱动程序单击它? Say if it says this 说这话

<style type="text/css"> <ul id="nav"> <li> <li> <li> <li> <li> <a href="dashboard.action">My Dashboard</a> </li> </ul>

Would I use 我会用吗

driver.findElement(By.linkText("My Dashboard")).click();

or something else? 或者是其他东西?

如果要在示例中单击链接,则可以使用编写的选择器,不同类型的CSS选择器(例如By.cssSelector("#nav a") (在“ nav”列表中查找链接)或By.cssSelector("a[href='dashboard.action']") (查找具有特定href的链接))或使用xPath选择器。

The important thing is to have a unique identifier to locate your element and an identifier that will fire 100% of the time. 重要的是要有一个唯一的标识符来定位您的元素,并且标识符会在100%的时间内触发。

For example, if you expect the link text to change on you, then don't look for that particular link text, because you have no guarantee that it will work 100% of the time. 例如,如果您希望链接文本发生变化,则不要查找该特定的链接文本,因为您不能保证它会在100%的时间内工作。

Similarly, if there are 30 different elements that have the same id tag, don't use that either. 同样,如果有30个不同的元素具有相同的id标签,则不要使用它们。

If things turn out to be very complex... that is, if you are in a large page with a lot of unknown variables, find by XPATH . 如果结果变得非常复杂...也就是说,如果您所在的页面很大且包含许多未知变量,请通过XPATH查找。

In the end, it really depends on the complexity of the website you are entering, and the goal of what you need done. 最后,这实际上取决于您输入的网站的复杂性以及您需要完成的工作的目标。

For more information, go to the Selenium javadocs and click BY on the sidebar for a list of different methods and how to use them. 有关更多信息,请转到Selenium javadocs ,然后单击侧栏上的BY以获取不同方法及其使用方法的列表。

If you need to click a link with an image, it would be better to locate the element with the explicit wait. 如果您需要单击带有图像的链接,则最好使用显式等待来定位元素。

Example : 范例:

new WebDriverWait(driver, timeout).until(ExpectedConditions.presenceOfElementLocated(locator)); 新的WebDriverWait(驱动程序,超时).until(ExpectedConditions.presenceOfElementLocated(locator));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM