Selenium cannot find valid xpath for the button click - java

Hello I'm learning selenium and I've met one problem. I was doing all things using xpath for buttons but with this one this doesn't work and I don't know why.
This is how looks button which I want to click (I want to click Order tickets button )
<div id="bookingOption" class="row top5" style="display: block;">
<div class="col-md-6">
<input name="bookButton" class="btn btn-primary" type="button" value="Order tickets">
</div>
</div>
My java code for clicking this button
I am using xPath //*[#id="bookingOption"]/div/input
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[ before #id='bookingOption']/div/input")));
driver.findElement(By.xpath("//*[ before #id='bookingOption']/div/input")).click();
Here is website on which on I am practising it may be helpful. http://ticketmonster-jdf.rhcloud.com/
I will be very thankful for every help.

Try:
//input[#value='Order tickets']

Remove before from xpath("//*[ before #id='bookingOption']/div/input")
the correct form for defining the xpath is
xpath("//*[#id='bookingOption']/div/input")

Related

Click Radio Button Selenium

Been trying to click a radio button on website to no avail.
<df-radio-group class="o-flex o-flex--distribute df-question ng-tns-c33-15 ng-has-value ng-touched ng-dirty ng-valid" aria-label="Title" id="title" aria-labelledby="title" role="radiogroup" style="float: left; width: 100%;">
<!----><df-radio _nghost-c40="" class="ng-tns-c33-15 is-checked" id="md-radio-93fd918"><label _ngcontent-c40="" class="df-radio-label" for="md-radio-93fd918-input">
<input _ngcontent-c40="" class="df-radio-input visually-hidden" type="radio" id="md-radio-93fd918-input" name="df-radio-group-0">
<div _ngcontent-c40="" class="md-radio-label-content">
Mr
</div>
</label></df-radio><df-radio _nghost-c40="" class="ng-tns-c33-15" id="md-radio-bd05b81"><label _ngcontent-c40="" class="df-radio-label" for="md-radio-bd05b81-input">
<input _ngcontent-c40="" class="df-radio-input visually-hidden" type="radio" id="md-radio-bd05b81-input" name="df-radio-group-0">
<div _ngcontent-c40="" class="md-radio-label-content">
Mrs
</div>
</label></df-radio><df-radio _nghost-c40="" class="ng-tns-c33-15" id="md-radio-ba9f195"><label _ngcontent-c40="" class="df-radio-label" for="md-radio-ba9f195-input">
<input _ngcontent-c40="" class="df-radio-input visually-hidden" type="radio" id="md-radio-ba9f195-input" name="df-radio-group-0">
<div _ngcontent-c40="" class="md-radio-label-content">
Miss
</div>
</label></df-radio><df-radio _nghost-c40="" class="ng-tns-c33-15" id="md-radio-ec973a5"><label _ngcontent-c40="" class="df-radio-label" for="md-radio-ec973a5-input">
<input _ngcontent-c40="" class="df-radio-input visually-hidden" type="radio" id="md-radio-ec973a5-input" name="df-radio-group-0">
<div _ngcontent-c40="" class="md-radio-label-content">
Ms
</div>
</label></df-radio>
</df-radio-group>
Been trying to click both the radio button and the label but selenium keeps throwing the no such element error and I'm kinda frustrated at this stage.
Might be easier to see on the actual website:
https://www.theaa.ie/car-insurance/journey/getting-started
Its on the page after entering the email. Trying to get some test cases going but these radio buttons dont want to be clicked.
Having a look to the html structure:
You could wait the presence of the web element with id "title" and then find all the elements with class name "md-radio-label-content".
Once you have all of them, you can check the text and click the interested one.
So, for example, if you want to click on "Mr":
WebElement titleRadiogroup = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.presenceOfElementLocated(By.id("title")));
List<WebElement> ele= titleRadiogroup.findElements(By.className("md-radio-label-content"));
for (WebElement el : ele)
{
if(el.getText().equalsIgnoreCase("Mr"))
{
el.click();
}
}
In cases like this, I would use an XPath to click on the element by its contents. One issue you run into here is that these DIVs that contain the titles are full of whitespace (see below).
<div _ngcontent-c40="" class="md-radio-label-content">
Mr
</div>
Because of this, we can't do something simple like
//div[.='Mr']
Another issue is that you can't use contains() because Mr and Mrs both contain the string Mr. We can avoid the whole thing by using normalize-space() which trims whitespace.
//div[normalize-space(.)='Mr']
If it were me, I would wrap this in a function since you are likely to use it repeatedly.
public void selectTitle(String title)
{
driver.findElement(By.xpath("//div[normalize-space(.)='" + title + "']")).click();
}
and call it like
selectTitle("Miss");

Clicking span button by id of the parent(with id) - selenium with java

Is it possible to click a span button which is under the div element, also I would like to use the id of the input to make my test stable.
HTML:
<div class="idit-go">
<input id="IDITForm#checkRuleVO" class="GoLong align-left idit-go-text" type="text" value="" title="Validation Rule" tabindex="257" name="unknown" ignoredisableenableaction="true" helptextinfo="" disabled="">
<div class="idit-go-buttons">
<span id="IDITForm#checkRuleVOGoClearButtonClear" class="idit-go-clear" title="Clear" tabindex="259" onclick="clearField('IDITForm#checkRuleVOhidden');clearField('IDITForm#checkRuleVO');;">
<span class="fa-stack idit-go-button" value="" title="" tabindex="258" onclick="newPerformSubAction('selectRule',true);">
<i class="fa fa-stack-1x fa-square"></i>
<i class="fa fa-stack-1x fa-ellipsis-h"></i>
</span>
</div>
How it looks on screen:
As you can see the 3 dots button to the right, that is the button I'd like to click. Thanks in advance.
In this case you could identify the span that has the id attribute and then just use following-sibling (from your example they seems to be on the same level).
driver.findElement(By.xpath(".//[#id='IDITForm#checkRuleVOGoClearButtonClear']/following-sibling::span"));
using css - "span[id='IDITForm#checkRuleVOGoClearButtonClear'] + span[class='fa-stack idit-go-button']"
Okay, no-one got this from what I can see, so I can have a go as well! :)
Here we go Zvika :
element = driver.findElement(By.xpath("//div[#class='idit-go']//span[#class='fa-stack idit-go-button']"));
element.click();
(locating and clicking an element with span id="IDITForm#checkRuleVOGoClearButtonClear")
Best of luck man!:)

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();

Get id where style equals x - Java - Selenium WebDriver

When I open a page, there is code that looks like the following:
<div id="policySetup_content">
<div id="bCS_insureds_contentWrap" style="display: none;">
<div id="bCS_policy_contentWrap" style="display: block;">
<div id="bCS_risks_contentWrap" style="display: none;">
<div id="bCS_rating_contentWrap" style="display: none;">
<div id="bCS_billing_contentWrap" style="display: none;">
<div id="bCS_attachments_contentWrap" style="display: none;">
<div id="bCS_submit_contentWrap" style="display: none;">
</div>
How would I go about getting the #id of whichever one is set to (style="display: block;) inside the #id policySetup_content?
The reason for this is so I can know which page I'm on (because it can be any one of them for various reasons). I need to know the page in order to know which Wrap id to use when working with elements.
Judging by this previou SO question you should be able to use the CSS Selector (div[style*="display:block"]), something along the lines of the below (untested).
String id = driver.findElement(By.cssSelector("div[style*=\"display:block\"]").getAttribute("id");
Because Selenium will not interact with elements that are not visible, you should be able to pull all the DIVs under the parent DIV and only get the one that is not hidden. I've never tried this approach before but I think it will work...
String id = driver.findElement(By.cssSelector("#policySetup_content > div[id]")).getAttribute("id");
BTW, if you aren't familiar with CSS Selectors this reads find an element with ID (#) policySetup_content that has an immediate child (>) DIV that has an ID. This may need to be tweaked depending on the real HTML that you are dealing with. If it doesn't work, let me know and I can try to help tweak it.
CSS Selector reference

How to click on a close button of a modal dialog with PhantomJS and GhostDriver

I'm trying to take a screenshot of some twitter pages with Java, PhantomJS and GhostDriver, but all the time I'm getting screenshots with modal dialogs (sign-up modal or cockies modal).
Can somene suggest me how to find this close button and click it? There is no class or id directly on the button as you can see on the picture:
and here is the html:
<body class="three-col logged-in user-style-Nike ms-windows enhanced-mini-profile ProfilePage ProfilePage--withBlockedWarning supports-drag-and-drop" dir="ltr" data-fouc-class-names="swift-loading" style="background-position: 0% 46px;">
<div id="kb-shortcuts-msg" class="visuallyhidden">
<script id="swift_loading_indicator" nonce="ZLwCWCggSYkG1TzZ4188og==">
<div id="doc" class="route-profile">
<div class="topbar js-topbar">
<div id="page-outer">
<div id="page-container" class="AppContent">
<div class="BannersContainer BannersContainer--overlay">
<div class="Banner eu-cookie-notice">
<style>
<div class="flex-module">
<div class="banner-row">
<span class="title">
To bring you Twitter, we and our partners use cookies on our and other websites. Cookies help personalize Twitter content, tailor Twitter Ads, measure their performance, and provide you with a better, faster, safer Twitter experience. By using our services, you agree to our
Cookie Use
.
</span>
<button type="button">
<span class="icon close-medium">
<span class="visuallyhidden">Close</span>
</span>
</button>
</div>
</div>
</div>
Signing in is not an option.
Every DOM element may have a click handler added to them. Some elements already have a handler automatically added on them, for example <a> elements or <button> inside of a form.
So, you can just click on the .eu-cookie-notice span.icon.close-medium span or its parent .eu-cookie-notice .banner-row > button.

Categories