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 ??]
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!
CodePudding user response:
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
CodePudding user response:
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.