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

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

问题描述

我厌倦了从我的网页源手动查找网页元素的定位器(id,xpath,css,linkText等..)。它也消耗更多的努力。所以,为了避免我想编写一个直接与页面源交互的代码并生成定位器细节(例如id =xyz,xpath =html / body / table / tr / td / a等)
为了实现这一点,我想我可以通过使用String对象的split()函数生成ID定位器。但是,我不知道如何为所有页面组件生成xpaths,css和linkText定位器?
任何人都可以帮我这个吗?

解决方案

虽然我一般建议你自己构建XPath表达式(因为你可以更好地利用事物来对抗类属性),可能最合理和方便的自动方法来确定selenium的XPath表达式是使用Firebug或Chrome Developer Tool的查找 XPath功能。它们都至少使用 @id 属性来缩短XPath表达式。






<如果你想自己编写一些代码,例如。要嵌入您使用的其他工具中,您可能需要查看 PHP XML - 找出已知值的路径解决了PHP中的问题,或另一个解决了Javascript问题的问题: Javascript获取节点的XPath



如果您使用的是任何不在DOM上工作的工具(Selenium / Firebug / Chrome Dev'工具/ JavaScript都可以),请注意我在为什么我的XPath查询(抓取HTML表格)仅适用于Firebug,但不是我正在开发的应用程序?


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? Can anyone pls help me with this?

解决方案

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".

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天全站免登陆