-1

Button HTML Code How would I write the command to click the xpath of a button?

the Xpath is

html/body/div[1]/div[1]/form/div/div[1]/div/div/a[1]/button

would it be

driver.findElement(By.xpath("html/body/div[1]/div[1]/form/div/div[1]/div/div/a[1]/button")).click();
user6401108
  • 3
  • 1
  • 6
  • 1
    What's wrong with the code you posted? Does it work? If not, what is it doing differently than you expect? Do you get any errors? – Bryan Oakley Jun 20 '16 at 19:29
  • Possible duplicate of [click command in selenium webdriver does not work](http://stackoverflow.com/questions/11676790/click-command-in-selenium-webdriver-does-not-work) – Andrejs Jun 20 '16 at 20:53
  • Can you share the actual HTML of the page you're working with, or at least the HMTL for the button? – Tom Trumper Jun 21 '16 at 10:20
  • @TomTrumper yes just added it right now – user6401108 Jun 21 '16 at 15:31
  • Please post the HTML of the relevant section of the page as text. A linked image will eventually no longer be available and will make this question less useful. – JeffC Jun 21 '16 at 16:28

1 Answers1

1

Using XPath in this way is "easy" but is very fragile. You'd be better served getting the element in a different way, e.g. by CSS Selector "button.search-button".

driver.findElement(By.cssSelector("button.search-button")).click();

Learn more about CSS Selectors online. Here's a good reference guide, https://www.w3.org/TR/selectors/#selectors. There are many tutorials on the web, but here's a good one to start with, https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Getting_started/Selectors.

JeffC
  • 22,180
  • 5
  • 32
  • 55
  • thank you greatly Jeff, looking at the link you posted right now! – user6401108 Jun 21 '16 at 16:36
  • I added another link to a basic tutorial. The reference is good but it's not as useful if you don't know how to use it. Try the tutorial/guide I linked or there are many more on the web. I use CSS selectors all the time and I find them easier to use than XPath. There's very, very little that XPath can do that CSS selectors can't. XPath is generally slower and depending on how you build them, they are more fragile. – JeffC Jun 21 '16 at 16:42
  • What do you mean by more fragile? As in harder to mess up the code? And once again thank you! – user6401108 Jun 21 '16 at 16:49
  • By more fragile I mean more likely to break. When you use an XPath that starts from html, if *anything* in that element chain changes, the XPath will no longer work. Instead, focus on how to find just that element with properties on the element itself. That will prevent breakage from other parts of the page changing and make your test(s) more robust. – JeffC Jun 21 '16 at 16:51
  • In this specific case, if the `BUTTON` element that has class `search.button` is unique on the page, then the CSS selector I provided will find that button and no other element and will not be affected by *any* changes to the rest of the page. – JeffC Jun 21 '16 at 16:52
  • Ohhh that makes sense, so its basically the xpath could stop working depending on if the code changes or not – user6401108 Jun 21 '16 at 18:29
  • Yes, it really depends on how you construct the XPath. They can be constructed in ways that make them less likely to break but most of the people I see on here asking about XPaths copy them from Firebug, etc. and they are many times starting at the html tag. It's an easy way to get the XPath but they are fragile, as I was saying. It's more work to handcraft an XPath or CSS selector but in the long run it will be better and with experience, you will be able to create them faster. – JeffC Jun 21 '16 at 18:38
  • Thank for the information Jeff, definitely did learn a lot and pretty sure others will too who view this post! – user6401108 Jun 21 '16 at 18:46
  • You are welcome. If this answer answered your question, please accept it as the answer. Thanks. – JeffC Jun 21 '16 at 18:51