简体   繁体   中英

Selenium Web driver clicking images

How do I select in image on and click on it using Selenium web driver? 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.

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.

Similarly, if there are 30 different elements that have the same id tag, don't use that either.

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 .

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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