Selenium 2:以编程方式查找Web元素定位器 [英] Selenium 2 : finding web element locators programmatically

查看:109
本文介绍了Selenium 2:以编程方式查找Web元素定位器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我厌倦了从我的网页源中手动查找Web元素的定位符(id,xpath,css,linkText等.).它还需要付出更多的努力.因此,为避免出现这种情况,我想编写直接与页面源进行交互并生成定位器详细信息的代码(例如id ="xyz",xpath ="html/body/table/tr/td/a"等) 为此,我想我可以使用String对象的split()函数生成ID定位器.但是,我不知道如何为所有页面组件生成xpath,css和linkText定位符?

I am tired of manually finding locators (id,xpath,css,linkText etc..) for web elements from my web page source. It also consumes more efforts. So, to avoid that I want to write a code that interacts with page source directly and generates locators details (e.g. id="xyz" , xpath ="html/body/table/tr/td/a" etc.) To achieve this ,I think I can generate ID locator by using split() function of String object. But, what I don't know is how to generate xpaths, css and linkText locators for all page components?

推荐答案

尽管我通常建议您自行构建XPath表达式(因为您可以更好地利用它来对类似类属性进行处理),但这可能是最合理且合理的确定硒XPath表达式的便捷的自动方法是使用Firebug或Chrome开发者工具的查找XPath"功能.它们都至少使用@id属性来缩短XPath表达式.

Although I'd generally recommend to construct XPath expressions on your own (as you can better exploit things to mach against like class attributes), probably the most reasonable and convenient automatic way to determine XPath expressions for selenium is to use either Firebug's or Chrome Developer Tool's "Find XPath" feature. They both at least use @id attributes to shorten XPath expressions.

如果您想自己编写一些代码,例如嵌入到您使用的其他工具中,您可能希望看看"

If you want to write some code yourself, eg. for embedding in other tools you use, you might want to have a look at the answers of "PHP XML - Find out the path to a known value" which solves the problem in PHP, or another one with answers for Javascript: "Javascript get XPath of a node".

如果您正在使用在DOM上不起作用的任何工具(Selenium/Firebug/Chrome Dev'Tools/JavaScript可以使用),请当心我在"

If you're using any tools not working on the DOM (Selenium/Firebug/Chrome Dev' Tools/JavaScript will do), watch out for the problems I described in "Why does my XPath query (scraping HTML tables) only work in Firebug, but not the application I'm developing?".

这篇关于Selenium 2:以编程方式查找Web元素定位器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆