简体   繁体   中英

I am new to Selenium, using Java(Eclipse), I am having issues figuring out the xpath when using firebug

When I use firebug I get back this as the xpath it gives me back this /html/body/div[5]/div[2]/div/div[7]/div/div[4]/div/div[2]/div/ol/li/div/h3/a

I am unclear how to use this in Selenium Webdriver to click on a link.

Thanks!

This is well covered in the documentation.

http://docs.seleniumhq.org/docs/03_webdriver.jsp#selenium-webdriver-api-commands-and-operations

However, because its a very simple answer, you'd simply do something like:

driver.findElement(By.xpath("div[5]/div[2]/div/div[7]/div/div[4]/div/div[2]/div/ol/li/div/h3/a"));

(Assuming driver is a valid WebDriver instance and I've omitted the html/body section - it isn't needed).

始终尝试简化xpath,尝试将firebug与该xpath一起使用,以查看它是否唯一,如果不是,则需要更具体一些。

"//h3/a"

Don't use xpath unnecessarily. It would make some problem in future. If there is no other way to locate that element as @Nora told try to simplify the xpath.

In your case you can use By.linkText,By.partialLinkText.

driver.findElement(By.linkText("linkName")).click();

driver.findElement(By.partialLinkText("partialTextOfLink")).click();

driver.findElement(By.xpath("//a[text()='LinkText']")).click(); //simplified xpath

You can use any of the above if there is no other attribute(id, name ..etc) available for that anchor tag.

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