I have the following code for a button :
<div class="buttons">
<button class="btn dialog-confirm btn-primary" style="margin-left: 4px;">Confirm</button>
<button class="btn dialog-cancel" style="margin-left: 4px;">Cancel</button>
</div>
There are two buttons on is Confirm and another is Cancel
I can find the button with XPath but I don't want to use XPath.
Is there another way to find the button element in this case?
I tried this:
driver.findElement(By.className("btn dialog-confirm btn-primary")).click();
It did not find the button
Thank you for your help
Just check for a single dialog-confirm class:
driver.findElement(By.className("dialog-confirm")).click();
Or, use a CSS Selector:
driver.findElement(By.cssSelector("button.dialog-confirm")).click()
Added to alecxe and master slave's answer. It would be more specific if it is clicked by the button text, which is also easier to understand. Find the snippet for button click with xpath below.
driver.findElement(By.xpath("//button[text()='Confirm']")).click();
driver.findElement(By.xpath("//button[text()='Cancel']")).click();
Other ways using cssSelector:
Use full attribute i.e.:
driver.findElement(By.cssSelector("button[class='btn dialog-confirm btn-primary']"))
Use part of attribute i.e.:
driver.findElement(By.cssSelector("button[class*='dialog-confirm']"))
vote up for alecxe, your attempt was wrong on two accounts, when matching on multiple classes you should use By.cssSelector, and when they are set on the same element, you concatenate them with a dot, like
driver.findElement(By.cssSelector(".btn.dialog-confirm.btn-primary")).click();
this worked for me:
driver.find_element_by_class_name('buyable-full-width').click();
Related
I am having trouble in selecting a selector when I am trying to select it as a 'css-selector'
I have this selector:
<div role="button" class="jss300 jss299" tabindex="-1">
<span class="jss313">system-all</span></div>
</div>
and I am trying to get the css-selector from it, I tried this way:
"div[class~='system-paloaltonetworks']"
and my need is to get the text from the selector, in this case I want to get "system-paloaltonetworks" into string variable.
hope now the question is clear.
"system-paloaltonetworks" is the element text, not the class attribute (the class is jss313). You can't locate it with cssSelector you need to use xpath (you should also notice the element has span tag, not div)
driver.findElement(By.xpath("//span[text()='system-paloaltonetworks']"));
You are using class~= but aren't comparing with the class...
You try: driver.findElement(By.xpath("//*[#class='jss313']"));
HTML CODE
input class="btn primary" onclick="return login_jsp.saveRemember && login_jsp.saveRemember() || true;"
type="submit" value="Sign In"
Following approaches did not help :
1)
driver.findElement(By.className("btn primary")).sendKeys(Keys.ENTER).click();
2)
driver.findElement(By.cssSelector("input[class='btn primary']")).click();
(OR)
1)
driver.findElement(By.className("btn primary")).sendKeys(Keys.ENTER).sendKeys(Keys.ENTER);
2)
driver.findElement(By.cssSelector("input[class='btn primary']")).sendKeys(Keys.ENTER);
Please suggest.
you can try like this:
driver.findElement(By.cssSelector("input.btn.primary")).click()
See Sendkeys and Click won't work together.
So if there is some text field you can enter value there by :
driver.findElement(By.className("btn primary")).sendKeys(Keys.ENTER);
and then you if need to click some button or link you can use Click() as:
driver.findElement(By.cssSelector("input[class='btn primary']")).click();
Note: You should take the xpath of the same, to perform either sendkeys() or click()
Reply back to me if you have further query.
Happy learning:-)
You can try with below locators
Css Selector
input[value='Sign In']
input.btn.primary
xpath
//input[#value='Sign In']
//input[#class='btn primary']
please make use that above ones fetching specific element or not, may be by using firepath before using in webdriver script.
If you want to simulate ENTER then go for sendkeys with key events. If you want to just click on element then just use click();
this link helps you to write css selectors and this link you on xpath
Thank You,
Murali
input class="btn primary" onclick="return login_jsp.saveRemember && login_jsp.saveRemember() || true;"
type="submit" value="Sign In"
try below approches
1. driver.findElement(By.cssSelector(".btn primary")).click();
2. driver.findElement(By.xpath("//input[#type='submit']").click();
3. driver.findElement(By.xpath("//input[#value='Sign In']").click();
3. driver.findElement(By.xpath("//input[text()='Sign In']").click();
This is my button link:
<a class="button" href="#" onclick="ajaxtoelement('include/system.php?mode=begin&location='+getSelectedValue('location')+'&terminallane='+getSelectedValue('terminallane')+'','keyboard')</a>"
And I have tried doing
driver.findElement(By.xpath("//a[#class='ajaxtoelement('include/system.php?mode=begin&location='+getSelectedValue('location')+'&terminallane='+getSelectedValue('terminallane')]"));
Why can't selenium still find the button I'm specifying?
It should be accessed via onclick and not class one simple way is,
driver.findElement(By.xpath("//a[contains(#onclick, 'onClickValue')]");
I don't know your HTML structure, but
How many buttons do you have?
If you only have one button you don't need xpath, use the simpler way:
driver.findElement(By.className("button"));
If there are several more buttons, but the button you described is the only one that contains for example the word "terminallane":
driver.findElement(By.xpath("//a[#class = 'button' and contains(#onclick, 'terminallane')]"));
Try with the below xpath::
//a[#href='#']
else
//a[contains(#onclick, 'ajaxtoelement('include/system.php?mode=begin&location='+getSelectedValue('location')+'&terminallane='+getSelectedValue('terminallane')+'','keyboard')')]
I am new to selenium and trying to autoamte a web application in junit framework. As many get some problem in identifying web elements,I too stuck at a point where two submit buttons are having same xpath and css selector.
The only difference what I can observe is.. In the two form tags, I can see that className is different(for first form tag it is "feature_space_checkbox" and for second form tag it is "auto_fs_steps_checkbox")
As, I need to identify the second submit button..So I tried to identify the second submit button as below
driver.findElement(new ByChained(By.className("auto_fs_steps_checkbox"),By.xpath("//*[#id='edit_brochure_2863']/input[3]")));
When I try to execute this, I got the error as
org.openqa.selenium.NoSuchElementException: Cannot locate an element using By.chained({By.className: auto_fs_steps_checkbox,By.xpath: //*[#id='edit_brochure_2863']/input[3]})
Can anyone please correct me where I made the mistake
Adding DOM for this scenario
<form action="/brochures/2865/feature_space_checked" class="feature_space_checkbox" id="edit_brochure_2865" method="post"><div style="margin:0;padding:0">
<input name="commit" type="submit" value="Submit">
</form>
For the second submit button it is..
<form action="/brochures/2865/update_auto_fs_steps" class="auto_fs_steps_checkbox" id="edit_brochure_2865" method="post"><div style="margin:0;padding:0">
<input name="commit" type="submit" value="Submit">
</form>
Firstly, XPath and CSS selectors are not definitive. There are many XPath and CSS for every element on a page so to say they have the same Xpath and CSS selectors is incorrect.
For your example, is there any need to use XPath or combine two selectors?
The following CSS would work;
form.auto_fs_steps_checkbox input
There is no need to use chaining as this can all be expressed in XPath:
//*[#id='edit_brochure_2863' and #class='feature_space_checkbox']/input
So this will be in Java:
driver.findElement(By.xpath("//*[#id='edit_brochure_2863' and #class='feature_space_checkbox']/input"));
Of course, for the second submit button it will be
driver.findElement(By.xpath("//*[#id='edit_brochure_2863' and #class='auto_fs_steps_checkbox']/input"));
xpath for the second submit would be
driver.findElement(By.xpath("//form[#class='auto_fs_steps_checkbox']/input"));
This is enough to identify the second button as here class name is unique and id is same for both. So its better we do it by class name .
continue_button = browser.find_element_by_xpath("//input[#type='button' and #class='primary button']")
continue_button.click();
for some reason this does not work. this is what i know about the button i am clicking on:
its in a div AND
<button class="primary button" type="button"></button>
that is all i have to work with. help? browser.find_element_by_id works great for other items on the same page so my driver and everything is working.
it loads the page, types in text into fields that i have setup and i want it to click continue button and it just stops and ends.
the original html:
<div class="GLHWMP-BMTC">
<span class="inlineBlock GLHWMP-BDUC">
<button class="primary button" type="button"></button>
<span class="GLHWMP-BPTC GLHWMP-BEUC"></span>
<span class="GLHWMP-BPTC GLHWMP-BLTC"></span>
</span>
The type of the element that you are trying to click is <button>.
So to begin with, you need to change //input to //button.
In addition, you don't have to use both the class attribute and the type attribute, unless that is the only combination that uniquely identifies the element. Otherwise, you may simply use the attribute that uniquely identifies the element (either the class attribute or the type attribute).