简体   繁体   English

我是使用Java(Eclipse)刚接触Selenium的人,在使用Firebug时无法确定xpath的问题

[英]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 当我使用Firebug时,我将其作为xpath回来,它给了我/ 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. 我不清楚如何在Selenium Webdriver中使用它来单击链接。

Thanks! 谢谢!

This is well covered in the documentation. 文档中对此进行了详细介绍。

http://docs.seleniumhq.org/docs/03_webdriver.jsp#selenium-webdriver-api-commands-and-operations 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). (假设driver是一个有效的WebDriver实例,并且我省略了html/body部分-不需要它)。

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

"//h3/a"

Don't use xpath unnecessarily. 不要不必要地使用xpath。 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. 如果没有其他方法可以定位该元素,如@Nora告诉您尝试简化xpath。

In your case you can use By.linkText,By.partialLinkText. 您可以使用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. 如果该锚点标签没有其他属性(id,name ..etc)可用,则可以使用上述任何一种。

暂无
暂无

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

相关问题 我在 java 中使用 selenium 识别元素时遇到问题 - I am having problems identifying the element using selenium in java 空指针异常错误,我无法搞清楚? - Null pointer exception error that I am having trouble figuring out? 我无法使用Java中的Selenium WebDriver为Walmart主页下拉框获取xpath - I am unable to get xpath for walmart homepage dropdown box using selenium webdriver in Java 在Eclipse中使用Spring MVC 5的新项目,但出现错误404 - New project using Spring MVC 5 in eclipse, but I am having error 404 我正在用 Java 构建一个计算器,但在执行运算符时遇到问题 - I am building a calculator with Java and am having issues with it executing the operators 我在使用 selenium webdriver 和 java 定位红色总线站点中的搜索总线按钮时遇到问题 - I am having trouble in locating search bus button in red bus site using selenium webdriver with java 我无法弄清楚如何处理我收到的错误。 这是我的购物车项目 - I am having trouble figuring out what to do with the error I receive. This is my Shopping Cart project 尝试使用 selenium 4 打开新选项卡时,我看到“Selenium.InvalidArgumentException: invalid argument”错误 - I am seeing "Selenium.InvalidArgumentException: invalid argument" error when trying to open New Tab using selenium 4 我遇到格式化问题 - I am having formatting issues 我在使用硒的Java中创建方法,但出现错误 - i am create method in java using selenium and i got an error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM