I am having problems selecting button in selenium for java - java

Hello I am trying to select and click on the button "Book Now" yet when I view the source code it shows the following...
<div class="pl-0 mr-3 sticky-btn-wrapper">
<div class="ko-container book-now-btn-container">
<button class="btn btn-secondary text-uppercase btn-landing go-to-session" data-eid="757231" data-aid="97739" data-isavailable="true"> Book now</button>
</div>
</div>
<div class="btn-book-top sticky-btn-wrapper justify-content-end" id="book-button-top">
<div id="sticky-bottom-btn" class="sticky-bottom-btn flex-row w-100">
<div class="ko-container book-now-btn-container">
<button class="btn btn-secondary text-uppercase btn-landing go-to-session" data-eid="757231" data-aid="97739" data-isavailable="true">Book now</button></div>
</div>
</div>
</div>
When I inspect the "Book Now" link in Firefox is shows the following
<div class="ko-container book-now-btn-container">
<button class="btn btn-secondary text-uppercase btn-landing go-to-session" data-eid="757231" data-aid="97739" data-isavailable="true">
Book now
</button>
</div>
[Why are there two instances of button class="btn btn-secondary text-uppercase btn-landing go-to-session" ??]
I have tried to select the first instance with
WebElement wb = myDriver.findElement(By.xpath ("//div[#class='pl-0 mr-3 sticky-btn-wrapper'] and button[#class='btn btn-secondary text-uppercase btn-landing go-to-session']"));
wb.click();
But I get the following exception in Junit...
org.openqa.selenium.InvalidSelectorException:
Given xpath expression "//div[#class='pl-0 mr-3 sticky-btn-wrapper'] and button[#class='btn btn-secondary text-uppercase btn-landing go-to-session']"
is invalid: TypeError: Document.evaluate: Result type mismatch
Any help would be Sincerely appreciated!

You do not need to use and operand for locator, just change xpath to:
//div[#class='pl-0 mr-3 sticky-btn-wrapper']//button
Keyword and you need to use in the case when you have two or more similar elements and you need to add some more restrictions. Also and argument should be applied to the same tag, not for different.
For example:
<div class='1' style='1'>
<button>Button1</button>
</div>
<div class='1' style='2'>
<button>Button2</button>
</div>
<div class='2' style='2'>
<button>Button3</button>
</div>
If you need to get locator to the Button2:
//div[#class='1' and #style='2']/button

Please try below xpath :
//div[contains(text(),'Book now')]
PS : Please check in the dev tools (Google chrome) if we have unique entry in HTML DOM or not.
Steps to check:
Press F12 in Chrome -> go to element section -> do a CTRL + F -> then paste the xpath and see, if your desired element is getting highlighted with 1/1 matching node.
If there are multiple entry, Please try indexing :
(//div[contains(text(),'Book now')])[1]
or
(//div[contains(text(),'Book now')])[2]
in fact, you can have [3], [4] and any number, But you will have to make sure that in HTML DOM it is 1/1 matching.

Related

Selenium - How to get following sibling?

So I want to target a textbox that appears in a list that comes after the label "First Name" and send a first name to the box, but can't seem to be able to target the textBox...
What I've tried:
WebElement firstNameLocTry = driver.findElement(By.xpath("//label[text()='First Name']/following-sibling::div"));
firstNameLocTry.sendKeys(firstName);
What the li look like:
<li class="WPTO WKVO" role="presentation" data-automation-id="formLabelRequired">
<div class="WBUO WETO WDUO">
<label id="56$551056--uid24-formLabel" data-automation-id="formLabel" for="56$551056--uid24-input">First Name</label>
<div class="WEUO wd-c8594868-6b31-4526-9dda-7d146648964b" aria-hidden="true">First Name</div>
</div>
<div data-automation-id="decorationWrapper" id="56$551056" class="WFUO">
<div class="WICJ">
<div class="WMP2 textInput WLP2 WJ5" data-automation-id="textInput" id="56$551056--uid24" data-metadata-id="56$551056" style="visibility: visible;">
<input type="text" class="gwt-TextBox WEQ2" data-automation-id="textInputBox" tabindex="0" role="textbox" id="56$551056--uid24-input" aria-invalid="false" aria-required="true">
</div>
</div>
</div>
</li>
Any reason my sendKeys just leads to Element not interactable?
The attached HTML code Produce below out put
With the given XPath points to the second "First Name" Div [below pic], when you perform sendKeys, it is obvious that the error "Element not interactable" will be thrown.
Try with below two Xpaths,
1. //label[text()='First Name']//parent::div/following-sibling::div
2. //label[text()='First Name']//parent::div/following-sibling::div//input
Please use following xpath:
//div[text()='First Name']
or try:
//*[contains(text(),'First Name')]

How to Access Model Dialog Box content developed in Bootstrap using Selenium Web Driver and Java

I am trying to access model dialogue box (developed in Bootstrap) using Selenium Web Driver in Java, I am unable to do so. Here i wan tot access Title, Content and Button.
One more point is here xPath is dynamic, every time xPath is generating differently. I know that can be solved by partially matching the xPath.
So help me to solve this problem. Here I am attaching image of my model box design.
Here is code snippet
Here is code
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<div class="bootstrap-dialog-header">
<div class="bootstrap-dialog-close-button" style="display: none;">
<button class="close">
×
</button>
</div>
<div class="bootstrap-dialog-title" id="5f663313-d82d-4ab8-9712-6665e80a3654_title">
Member Registration
</div>
</div>
</div>
<div class="modal-body">
<div class="bootstrap-dialog-body">
<div class="bootstrap-dialog-message">
Sample Message Sample Message Sample Message Sample Message Sample Message Sample Message.
</div>
</div>
</div>
<div class="modal-footer">
<div class="bootstrap-dialog-footer">
<div class="bootstrap-dialog-footer-buttons">
<button class="btn btn-primary" id="353ab962-44d3-497f-8318-6db703b460d8">
Ok
</button>
</div>
</div>
</div>
</div>
</div>
//div[#class="bootstrap-dialog-title"] for Title
//div[#class="bootstrap-dialog-message"] for message
//div[#class="bootstrap-dialog-footer-buttons"]/button for buttton
driver.switchTo().activeElement();
Thread.sleep(5000);
driver.findElement(By.xpath("//button[contains(text(),'"+data+"')]")).click();
This Solve My Problem ,
I also wants to access Message but i am unable to access

selenium web driver finding elements

<div class="ui-dialog-buttonset">
<button class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only ui-state-hover" type="button" role="button" aria-disabled="false">
<span>
<br/>
For use to protect against or prevent actual or potential fraud, unauthorized transactions,claims or other liability.
</span>
<br/>
<br/>
<button class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" type="button" role="button" aria-disabled="false">
</div>
<span align="center"> Selection of this option will automatically log you out of the system.</span>
</div>
</div>
I have same button class name for both the buttons . How to find the first button and click it . The only difference in those is span text which i tried but it is not working out .
List<WebElement> buttons=driver.findElements(By.className("ui-dialobuttonset"));
buttons.get(0).click().
dint work
There is a class called ui-state-hover in first button which is not present in button 2. So You can try this xpath:
driver.findElement(By.xpath("//button[contains(#class,'ui-state-hover')]")).click();
The className your finding is of div, to find list of buttons give className of button i.e. ui-button or you can find buttons by tagName
List<WebElement> buttons=driver.findElements(By.className("ui-button"));
buttons.get(0).click();
Try Below code to click on the first button directly..
Make sure to use the correct class name "Class name is wrong in the code you written above"
wd.findElement(By.xpath("//div[#class='ui-dialog-buttonset']/button[1]"))
if it doesnot work try below code
List<WebElement> buttons=wd.findElements(By.xpath("//div[#class='ui-dialog-buttonset']"));
buttons.get(0).click();

WebDriver find a particular child element [duplicate]

This question already has answers here:
Locating child nodes of WebElements in selenium
(5 answers)
Closed 7 years ago.
I have a HTML like this:
<div class="classDiv">
<header>
<button class="btn btn-icon-only btn-icon-only pull-right ng-scope" type="button">
<span class="btn-box">
<span class="sr-only">Edit</span>
<span class="icon-btn icon-edit2" aria-hidden="true"></span>
</span>
</button>
<h3 class="classH3">Text1</h3>
</header>
</div>
<div class="classDiv">
<header>
<button class="btn btn-icon-only btn-icon-only pull-right ng-scope" type="button">
<span class="btn-box">
<span class="sr-only">Edit</span>
<span class="icon-btn icon-edit2" aria-hidden="true"></span>
</span>
</button>
<h3 class="classH3">Text2</h3>
</header>
</div>
The problem is every tag is the same, only the text of H3 is different for each div. I want to click only one particular Edit button, for example the second Edit button.
So I tried to find second H3 and from there I tried to find the second Edit button, like this:
WebElement hText = webDriver.findElement(By.xpath("//h3[contains(., 'Text2')]"));
WebElement editBtn = hText.findElement(By.xpath("//button[contains(., 'Edit')]"));
editBtn.click();
But it always clicks on the first Edit button. I want to find the parent element, and from parent to find its child. But in this case, every tag is the same, so I don't know how to look for a particular child. Any help much appreciated
try this one:
WebElement divWithH3 = webDriver.findElement(By.xpath("div[.//h3[contains(., 'Text2')]]"));
WebElement editBtn = divWithH3.findElement(By.xpath("//button[.//span[contains(text(), 'Edit')]]"));
editBtn.click();
This first finds the div element that contains your "Text2" element. From there you are searching for a button element, that has a span element that contains the text "Edit"
If I don't remember bad (//button)[1] or (//button)[2] let you select the button based on his position on the returned list of button

How to click Buy Now button using selenium?

Source HTML look like this :
<script id="during-reserve-tpl" type="text/x-lodash-template">
<div class="gd-row">
<div class="gd-col gu16">
<div class="emailModule message module-tmargin">
<div class="error-msg"></div>
<div class="register brdr-btm">
<div class="jbv jbv-orange jbv-buy-big jbv-reserve">Buy Now</div>
</div>
<div class="topTextWrap brdr-btm tmargin20">
<div class="subHeading">
Only one phone per registered user
<p>
First come, first serve!
</p>
</div>
</div>
</div>
</div>
</div>
</script>
When I code : IWebElement buy = driver.FindElement(By.CssSelector(".jbv.jbv-orange.jbv-buy-big.jbv-reserve")); It says Element not found.
I tried putting By.ClassName with while spaces but it says, compound classes are not supported.
Is there any alternative to click it ?
driver.FindElement(By.cssselector("div.jbv.jbv-orange.jbv-buy-big.jbv-reserve"))
In the above example css selector looks for div tag with name and it will look for all the dot with space
Try this By.xpath("//*[contains(#class, 'jbv')]") if it works.
You can try either of these:
IWebElement buy = driver.FindElement(By.CssSelector("div.register>div"));
OR
IWebElement buy = driver.FindElement(By.CssSelector("div.register"));

Categories