How to find xpath of this button - java

enter image description here
<button class="MuiButtonBase-root MuiButton-root MuiButton-contained MuiButton-containedPrimary MuiButton-sizeMedium MuiButton-containedSizeMedium MuiButton-root MuiButton-contained MuiButton-containedPrimary MuiButton-sizeMedium MuiButton-containedSizeMedium css-7l669b" tabindex="0" type="button">Add New Profile<span class="MuiTouchRipple-root css-w0pj6f""style=""></span></button>

According to the picture seems that this element has unique id attribute, so I'd suggest to try this XPath:
"//button[#id='AddNewProfile']"
Selenium command could be like this:
driver.findElement(By.xpath("//button[#id='AddNewProfile']"));

Select the element from the inspect tools by right click. Click on copy and then click on copy Xpath as highlighted in picture below.

It would have been more helpful if the entire HTML document was available. But I suggest you look to see if the class value of the button element is stable and do either //button[#class='<CLASS VALUE>'] or //button[contains(#class,'<ADD PART OF THE CLASS VALUE HERE>')]

Related

How to click a button where the class is the same for another options?

I am trying to click a button from a list but this button has the same class than others in the list because they have the same name (btn ban-red) so how can I click it if in the inspect I have this information:
<a class=“btn ban-red” data-track-event=“navigate” data-track=name=“Jobylon” - Quality Engineer” href=“https://emp.jobylon.com/jobs/16654-f/” target=“_blank”>View job/a>
The inspect is copying this xpath:
/html/body/div[1]/div[4]/div/div/div/div[3]/div/div/div/div[1]/section/div/div[2]/div[1]/div[1]/article[14]/a
But it is not working
I also created my own xpath this way:
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[#data-track-name=‘Jobylon - Quality Engineer’]"))).click();
But is not working either
I am using Selenium with java and I am in a Macbook, thank you for your help.
Absolute xpath not recommended. You can try using relative xpath.
Locate based on element text
driver.findElement(By.xpath("//a[contains(.,'View job')]")).click()
Locate using combination of element attribute if classes are not unique
driver.findElement(By.xpath("//a[#class='btn ban-red'][#data-track-event='navigate']")).click()
OR
driver.findElement(By.xpath("//a[#class="btn ban-red"][#href='https://emp.jobylon.com/jobs/16654-f/']")).click()
Better to use CSS selector as its faster then xpath. So you try like
driver.findElement(By.cssSelector("a[class='btn ban-red'][data-track-event='navigate']")).click()
OR
driver.findElement(By.cssSelector("a[class="btn ban-red"][href='https://emp.jobylon.com/jobs/16654-f/']")).click()
Still facing some issue like element not visible or no such element then try with explicit wait conditions until your element gets visible or clickable.

Selenium WebDriver "java", I can not click on button on footer

The scenario is:
Try to add experience in linkedin.
Then click on save button to save the added experience.
The below is html code for this button:
<button class="pe-form-footer__action--submit form-submit-action Sans-15px-white-100%" type="submit">
Save
</button>
I am trying to find it by xpath using:
#FindBy (xpath = "//*[contains(text(), 'Save')]")
WebElement saveExperienceButton;
Following screenshot may help:
I will appreciate your help.
if you do not mind css/xpath selectors that do not look very elegant, you can always open up Chrome developer tools on the website you wanna test with Selenium, mark the DOM elements you wanna access and in the context menu choose 'Copy xpath' or 'copy selector':
Try this xpath:
(//*[text()='Save'])[2]
On my profile there are 2 Save buttons - the second one is the skill save. Also, you might want to check this question for the contains syntax.
Creating an XPath using text is a less preferable way. instead of that use other attribute value which is unique.
for ex: in your case
//footer//*[contains(#class, 'form-submit')]

How to find element having dynamic ID / Name changing on every page load using Selenium WebDriver

I found a few answers about this but they did not answer my question an so I am writing a new question.
I have HTML code having below kind of checkbox elements (in browser's inspect element)
<input role="checkbox" type="checkbox" id="jqg_TransactionFormModel501EditCollection2_147354_grid_-1274" class="cbox" name="jqg_TransactionFormModel501EditCollection2_147354_grid_-1274" value="true">
In my test case I want to click on checkbox using its ID using Selenium Webdriver.
here Id= "jqg_TransactionFormModel501EditCollection2_147354grid-1274" is dynamic.
in above id, Bold & Italic marked letters (dynamic) will change with different check boxes in same page as well as page refresh.
Bold marked letters (dynamic) will change on page refresh only (remain same through all the check boxes in same page.)
How shoud I format/write XPATH so that I can click on desired check boxes using below statement.
WebElement checkbox = webDriver.findElement(By.id("idOfTheElement"));
if (!checkbox.isSelected()) {
checkbox.click();
}
Thanks for your help in advance.. !
Here are a few examples of xpaths which you can use to find your checkbox
//input[contains(#id,'jqg_TransactionFormModel')]
OR, if you want more checks, try something like
//*[starts-with(#id,'jqg_TransactionFormModel') and contains(#id,'EditCollection2_')]
Additionally, you can try regex as well using matches
//*[matches(#id,'<regex matching your id>')]
You can use partial ID using cssSelector or xpath
webDriver.findElement(By.cssSelector("[id*='TransactionFormModel']"));
webDriver.findElement(By.xpath("//input[contains(#id, 'TransactionFormModel')]"));
You can replace TransactionFormModel with any other fixed part of the ID.
As a side note, no need to locate the element twice. You can do
WebElement checkbox = webDriver.findElement(By.id("idOfTheElement"));
if (!checkbox.isSelected()) {
checkbox.click();
}
You can write xpath like this :
//input[starts-with(#id,'jqg_TransactionFormModel')]
//input[contains(#id,'jqg_TransactionFormModel')]
I recommend not using ID's or xpath and adding a data attribute to your elements. This way, you can avoid the annoyance of xpath and have a strong selector that you feel confident will always be selectable.
For example, you could call the attribute: data-selector. You can assign a value of "transactionFormModelCheckbox". Then, when creating a new element, you create by css selector with the value referenced above.
No matter what type of formatting, you'll always be able to select that checkbox - as long as the element exists in the DOM. If you need to investigate other attributes, you can use the selector to do things like hasClass() or anything else you need.
Hope that helps !

Unable to find a button wrapping elements containing specific text for Selenium testing

Given the following Html:
<button type="button"><b><em><u>Next</u></em></b></button>
In the page source I have more than one button. I have only option to find this element with text Next. I tried following methods but without success:
driver.findElement(By.cssSelector("button>b>em>u[text()='Next']"))
driver.findElement(By.cssSelector("button>b>em>u[.='Next']"))
driver.findElement(By.cssSelector("button>b>em>u.contains('Next')"))
It might be the case that the button has whitespace or other characters, I would switch to an xpath selector.
Selecting the u element
This xpath will select the u element:
driver.findElement(By.xpath("//button/b/em/u[contains(., 'Next')]"))
Selecting the button
To select the button containing the above u element, e.g. so that the button can be clicked:
driver.findElement(By.xpath("//button[b/em/u[contains(., 'Next')]]"))
Xsl fiddle of this here
Element not Found?
As an aside, and in general, when looking at HTML to determine css or xpath selectors for Se, ensure that you are looking at the final rendered version of Html, and not just the original Html served from the web server - client side javascript may have modified the html significantly after loading, and also note that if the served html was not well formed, that browsers can also change html. In Chrome, I use the Developer tools : Elements pane to view html.
Try assigning an ID to the button and then map the functionality to the button ID rather than button text like this:
<button id ="btn1" type=button">
You are combining xpath selector syntax in your css selector. Try this in stead:
driver.findElement(By.xpath("//button/b/em/u[text()='Next']"))
or
driver.findElement(By.xpath("//u[text()='Next']")).
If you do wish to use a css selector, remove the text() part like so:
driver.findElement(By.cssSelector("button>b>em>u"))
Below xpath should find 1st occurrence of "Next" button:
//button[text()='Next'][1]
If you want to get 2nd occurrence:
//button[text()='Next'][2]

Button location error using Xpath?

I have to test a dynamic app using a button like that:
<button class="btn btn-primary btn-mini" ng-click="addAnswer(question)" ng-show="question.editing">Add Answer</button>
I was trying to locate button with that but I failed
getDriver().findElement(By.xpath("//img [#ng-click='addAnswer(question)']")).click();
using css
List AddAnswerBtn =
getDriver().findElements(By.className("btn-primary"));
AddAnswerBtn.get(0).click();
What is the correct line to locate it? I don't want to locate it using class and I wants to use Xpath to locate button location.
Your example shows it is a button, but your XPath is looking for an image:
//img[#ng-click='addAnswer(question)']
The above is what you are using, with img as the element type.
Change this to button.
Xpath doesn't work properly sometimes in chrome browser. Use firefox driver to find it, if you are not sure if your xpath is correct, use Firepath. Or in Chrome Xpath Helper.
Your xpath refers to a image, not button.
getDriver().findElement(By.xpath("//img [#ng-click='addAnswer(question)']")).click();
Change this to
getDriver().findElement(By.xpath("//button[#ng-click='addAnswer(question)']")).click();

Categories

Resources