Selenium: How to identify the button WebElement - xpath

On my webpage I have two buttons, how to identify which one is which?
<button onclick="addToSelected('newApplicationForm');">Add Strategy</button>
<button onclick="submitAddNewApplication('newApplicationForm');">Submit</button>

You say it's your webpage. Any chance you could put IDs to your elements? Would make it easier to identify them uniquely. If not, solution below.
You could use this XPath expression:
//button[contains(#onclick,"addToSelected('newApplicationForm');")]
Which will identify it by the javascript call, or you could try:
//button[.='Add Strategy']
^
|_ May need to be replaced by text()
Which will match the content.
For the submit-button, you could try the same principle with identifying by javascript:
//button[contains(#onclick,"submitAddNewApplication('newApplicationForm');")]
or by content:
//button[.='Submit']
Note: Some languages seem to use text() instead of the dot . to refer to the actual text in an element.

Related

How to check if div contains text (XPath)

I use https://autorefresh.io/expressions/ chrome extension for page refreshing. The next level for me is checking if a specific page part contains the predefined text. This extension supports XPath.
I was trying to use //*[contains(text(),"Brak towaru")]. It worked until I put this cell "address" instead of *.
/html/body[#class='b--desktop breakpoint-xl']/div[#class='body-inner']/
section[#class='pt-2']/div[#id='main']/div[#class='shop']/
div[#class='row mt-4 justify-content-center'][1]/div[#class='col-4 col-md-2 pt-3 text-center']
I was trying to insert this "address" in many ways... No success.
Any hints?
screenshot of XPath extension
Instead of using contains(text()) try using contains(.,). This will check for text content in the element itself and in it's child nodes.
So, instead of
//*[contains(text(),"Brak towaru")]
Try using
//*[contains(.,"Brak towaru")]
And in case this is a div element use
//div[contains(.,"Brak towaru")]
Thank you very much. Works :)
//*[#id="main"]/div/div[3]/div[3][contains(.,"Brak towaru")]

Can't get xpath to locate element

I have this piece of HTML and I'm trying to select the <a href> link using xpath.
<li class="footable-page-nav" data-page="next" aria-label="next"><a class="footable-page-link xh-highlight" href="#">›</a></li>
I need the selector to be reasonably specific since "footable-page-link" exists in multiple places in the HTML.
I've tried this:
//li[#class='footable-page-nav']/a[#class='xh-highlight']//#href
Selenium throws an error: selenium.common.exceptions.NoSuchElementException
If I shorten the xpath expression to //li[#class='footable-page-nav'] just to see if I'm on the right track then I get
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable: element has zero size
What am I missing?
Try changing your xpath expression to
//li[#class='footable-page-nav']/a[contains(#class,'xh-highlight')]//#href
and see if it works.

I have doubts about two mappings on the site-prism

I don't want to use xpath on the elements below.
element :img_login, :xpath, '//[#id="main-wrapper"]/div/section/div/div[2]/div/div/div[1]/img'
element :msg_login_senha_invalidos, :xpath, '//[#id="main-wrapper"]/div/section/div/div[2]/div/div/div[2]/div/p'
They are on the page as follows:
element img_login
<div class="sc-jRQAMF eRnhep">
<img src="https://quasar-flash-staging.herokuapp.com/assets/login/flashLogo-3a77796fc2a3316fe0945c6faf248b57a2545077fac44301de3ec3d8c30eba3f.png" alt="Quasar Flash">
</div>
element msg_login_senha_invalidos
<p class="MuiFormHelperText-root MuiFormHelperText-contained Mui-error MuiFormHelperText-filled">Login e/ou senha inválidos</p>
You have asked multiple questions about converting from using XPath to some other type of selector when using Site-Prism. StackOverflow is meant to be a place to come, learn, and improve your skills - not just to get someone else to do your work. It really seems you'd be better off reading up on CSS and how it can be used to select elements. Also note that there's nothing specifically wrong with using XPath, per se, it's just the way people new to testing and selecting elements on a page tend to use it (just copying a fully specified selector from their browser) that leads to having selectors that are way too specific and therefore brittle. A good site for you to learn about the different general CSS selector options available is https://flukeout.github.io/ - and you can look at the built-in selector types provided by Capybara at https://github.com/teamcapybara/capybara/blob/master/lib/capybara/selector.rb#L18
In your current case the below may work, but with the HTML you have provided all that's possible to say is that they will match the elements shown however they may also match other elements which will give you ambiguous element errors.
element :img_login, :css, 'img[alt="Quasar Flash"]' # CSS attribute selector
element :msg_login_senha_invalidos, :css, 'p.Mui-error', text: 'Login e/ou senha inválidos' # CSS class selector combined with Capybara text filter

selenium find child's child elements

I have following html like:
<form name="form1">
<input name="a" ...>
<input name="b" ...>
...
<div><span><select name="c">...</select></span></div>
</form>
I would like to find out all elements within the form element. First I use findElement() to get the form element form1, then use form1.findElements(By.xpath(".//*[#name]")) to get all its children having attribute name. However, for the select element, since it's grand-grand child of form1, how can I get it as well?
Is there a way to find all elements containing attribute name (not only child elements, but also child's child's child...) within form1?
Thanks!
if you want to get an WebElement by xpath, and so get child of it... you may use webElement.findElement(By.xpath("./*")) ... this "." before the "/" makes the magic, you'll need it to get only the children of the webElement...
Do you have to find the form element? If not, then you can do it in one select statement using css or xpath.
The css would be 'form[name="form1"] [name]'
Note the space between the closing and opening brackets.
You would use this selector with FindElement on the driver object rather than finding the form first.
You should be able to use the descendant:: as described in this post.
http://hedleyproctor.com/2011/05/tutorial-writing-xpath-selectors-for-selenium-tests/
Here are a few examples from the article:
//div[h3/text()='Credit Card']/descendant::*
//div[h3/text()='Credit Card']/descendant::input[#id='cardNumber']
//div[*/text()='Credit Card']/descendant::input[#id='cardNumber']
webDriver.findElement(
By.xpath("//div[*/text()='Credit Card']/descendant::input[#id='cardNumber']")
).sendKeys("1234123412341234");

Writing xpath and CSS locator/path for Selenium Automated Test

I have the following HTML:
<input type="submit" style="-webkit-user-select:none;line-height:100%;height:30px" value="Advanced Search" class="jfk-button jfk-button-action adv-button">
I have written xpath as: //input[#value='Advanced Search']
What is the CSS locator/path?
It's difficult to answer as optimum search selectors need the entire source code to be written, as several DOM Elements in the document could be returned for a generic selector.
In this case, a more detailed selector would be :
input.adv-button[value='Advanced Search']
You can convert your xpath into corresponding CSS Locator by using the following website:
http://cssify.appspot.com/
For example:
Go to site http://cssify.appspot.com/
Insert the XPath //input[#value='Advanced Search'] into text field
Click submit button and observe the result
You can see the corresponding CSS Locator as follows:
input[value="Advanced Search"]

Resources