问题
I am working in Selenium RC. Can anyone please let me know how to write xpath for button in Selenium (Java)?
回答1:
You should develop the script in the Selenium IDE (download) before porting it to Selenium RC. In Selenium IDE, when you click anything on the webpage, it should automatically generate some kind of selector for the element you clicked. Then, once you've recorded all the events, you Format it in whatever language you're using, and then you copy and paste it to your Selenium RC code.
But the Recorder Javascript isn't foolproof (e.g. if you click on a div
that causes some XMLHttpRequest
or setTimeout
, it won't be recorded). Or, the click may be recorded but you may not like the selector that Selenium chooses for the element. In either case, you'll have to write your own selector based on the DOM structure. To see the DOM structure, open Firebug if you're in Firefox (F12), or open the Inspector if you're on Chrome (Ctrl-Shift-J) Fortunately, Selenium understands a bunch of selector syntaxes, so you can use CSS selectors if you don't know XPath.
If you do decide to use XPath, you'll have to learn it first. I haven't found any good tutorials (and I'm not a fan of w3schools). But feel free to use a bookmarklet to test XPaths that I wrote. You'll probably end up with something like //button[.="text on button"]
, or //input[@value="text on button"]
.
回答2:
You can find button Xpath by using Firebug
which is an add-on for Firefox and as above answer Selenium IDE is also another, easier option to find.
来源:https://stackoverflow.com/questions/4929844/how-to-use-xpath-of-button-in-selenium-rc