select2 dropdown with selenium - selenium

I have a drop down in WordPress products page which is a select2 ajax enabled.
I have managed to show the options in the drop down using selenium.
But i am not able to select one of the options from the list.
I am able to get the element using the below code. But the element is not castable to Select nor clickable.
driver.findElement(By.cssSelector("[class='multiselect attribute_values wc-enhanced-select select2-hidden-accessible enhanced']"))
Any ideas how to select any option from it?
The HTML code for the drop down is
<tr>
<td class="attribute_name">
<label>Name:</label>
<strong>Brand</strong>
<input type="hidden" name="attribute_names[0]" value="pa_brand">
<input type="hidden" name="attribute_position[0]" class="attribute_position" value="0">
</td>
<td rowspan="3">
<label>Value(s):</label>
<select multiple="" data-placeholder="Select terms" class="multiselect attribute_values wc-enhanced-select select2-hidden-accessible enhanced" name="attribute_values[0][]" tabindex="-1" aria-hidden="true">
<option value="107">Adidas</option>
<option value="110">Gul Ahmed</option>
<option value="111">Khadi</option>
</select>
<span class="select2 select2-container select2-container--default select2-container--above select2-container--open" dir="ltr" style="width: auto;">
<span class="selection">
<span class="select2-selection select2-selection--multiple" aria-haspopup="true" aria-expanded="true" tabindex="-1" aria-owns="select2-attribute_values0-to-results" aria-activedescendant="select2-attribute_values0-to-result-d16b-111">
<ul class="select2-selection__rendered" aria-live="polite" aria-relevant="additions removals" aria-atomic="true">
<li class="select2-search select2-search--inline"><input class="select2-search__field" type="text" tabindex="0" autocomplete="off" autocorrect="off" autocapitalize="none" spellcheck="false" role="textbox" aria-autocomplete="list" placeholder="Select terms" style="width: 418.797px;" aria-owns="select2-attribute_values0-to-results" aria-activedescendant="select2-attribute_values0-to-result-d16b-111"></li>
</ul>
</span>
</span>
<span class="dropdown-wrapper" aria-hidden="true"></span>
</span>
<button class="button plus select_all_attributes">Select all</button>
<button class="button minus select_no_attributes">Select none</button>
<button class="button fr plus add_new_attribute">Add new</button>
</td>
</tr>

As per the HTML:
<select multiple="" data-placeholder="Select terms" class="multiselect attribute_values wc-enhanced-select select2-hidden-accessible enhanced" name="attribute_values[0][]" tabindex="-1" aria-hidden="true">
The WebElement is clearly a <select> node and to select one of the options from the html-select tag you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following locator strategies:
Using cssSelector and selectByVisibleText():
new Select(new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.cssSelector("select.multiselect.attribute_values.wc-enhanced-select.select2-hidden-accessible.enhanced[data-placeholder='Select terms']")))).selectByVisibleText("Adidas");
Using xpath and selectByValue():
new Select(new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//select[#class='multiselect attribute_values wc-enhanced-select select2-hidden-accessible enhanced' and #data-placeholder='Select terms']")))).selectByValue("110");

Related

Couldn't click on a link that opens a menu

I'm trying to click on a link after the text "action". The HTML is:
<div _ngcontent-c10="" class="col-md-3" style="padding-left: 20px;">
<div _ngcontent-c10="" class="card" style="width:100% !important">
<div _ngcontent-c10="" class="headerText">FLOATS & MOVE</div>
<hr _ngcontent-c10="">
<div _ngcontent-c10="" class="text1">action</div>
<div _ngcontent-c10="" class="text2">
<a _ngcontent-c10="" class="waves-light" data-toggle="modal" mdbwaveseffect="" style="margin-right: -25%;" type="button">
<img _ngcontent-c10="" src="assets/co-assets/icon-down-arrow-med.png">
</a>
</div>
<div _ngcontent-c10=""class="text1">from</div>
<div _ngcontent-c10="" class="text2">
<a _ngcontent-c10="" class="waves-light" data-toggle="modal" mdbwaveseffect=""
style="margin-right: -25%;" type="button">
<img _ngcontent-c10="" src="assets/co-assets/icon-down-arrow-med.png">
</a>
</div>
<div _ngcontent-c10="" class="text1">to</div>
<div _ngcontent-c10="" class="text2">
<a _ngcontent-c10="" class="waves-light" data-toggle="modal" mdbwaveseffect="" style="margin-right: -25%;" type="button">
<img _ngcontent-c10="" src="assets/co-assets/icon-down-arrow-med.png">
</a>
</div>
The XPath I've used is:
//div[./text()='action']/following-sibling::div[1]/a
I've waited explicitly for 20 seconds with the condition ElementToBeClickable as follows:
wait.until(ExpectedConditions.elementToBeClickable(actionDropDown));
The interesting thing is I'm not getting TimeOutException.
Seems like Webdriver unable to click on the element.Try the following options to click.
Use Action class
WebElement ele=driver.findElement(By.xpath("//div[#class='text1'][contains(.,'action')]/following-sibling::div[1]/a"));
Actions action=new Actions(driver);
action.moveToElement(ele).click().build().perform();
Use Javascripts executor
WebElement ele=driver.findElement(By.xpath("//div[#class='text1'][contains(.,'action')]/following-sibling::div[1]/a"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", ele);

handle vue-js dropdown plugin in selenium webdriver

I am testing a web application which has v-select vuejs dropdown plugin for Country field. How Can I select values in dropdown using selenium webdriver.
It has no select/div.
Below is the HTML before selecting the country from the dropwdown
<div data-v-ce984332="" id="country-fg" class="mg-t-20">
<p data-v-ce984332="" class="control has-icon has-icon-right">
<div data-v-ce984332="" dir="auto" class="dropdown v-select single searchable" name="country" aria-required="true" aria-invalid="false">
<div class="dropdown-toggle clearfix">
<input type="search" autocomplete="false" placeholder="Country" aria-label="Search for option" class="form-control" style="width: 100%;"> <button type="button" title="Clear selection" class="clear"><span aria-hidden="true">×</span></button> <i role="presentation" class="open-indicator"></i>
<div class="spinner" style="display: none;">Loading...</div>
</div>
<!---->
</div>
<span data-v-ce984332="" class="small tx-warning" style="display: none;"></span></p>
</div>
And this is the HTML after selecting the country as United States from the dropdown
<div data-v-ce984332="" id="country-fg" class="mg-t-20">
<p data-v-ce984332="" class="control has-icon has-icon-right">
<div data-v-ce984332="" dir="auto" class="dropdown v-select single searchable" name="country" aria-required="true" aria-invalid="true">
<div class="dropdown-toggle clearfix">
<span class="selected-tag">
United States
<!---->
</span>
<input type="search" autocomplete="false" aria-label="Search for option" class="form-control" style="width: auto;"> <button type="button" title="Clear selection" class="clear"><span aria-hidden="true">×</span></button> <i role="presentation" class="open-indicator"></i>
<div class="spinner" style="display: none;">Loading...</div>
</div>
<!---->
</div>
<span data-v-ce984332="" class="small tx-warning" style="display: none;"></span></p>
</div>
Please specify the language binding you are using and the things you've tried so far.
Algo in Java:
click the 'dropdown' field: driver.findElement(By.name("country")).click()
click the option: driver.findElement(By.name("country-1")).click()
These kinds of 'dropdown' fields are usually tied to another div / element.
E.g., when you click the 'dropdown' field (item 1), another dynamic div may appear containing the options in some form of tag.
Most common examples would be, <li>, <div>, <span>. You'll then have to do another click() on the option you want. (item 2)
There are even cases where the dropdown div encloses an input tag to which you can do sendKeys() or setAttribute() as well as cases where you can do a javascript click directly into one of the options.
Suggest you provide more info so we can help you better.
selenium language bindings
html snippet of the dropdown - please avoid using screenshots for snippets
html element of the options that appear when you click the dropdown

How can I Selecting Kendo Drop Down using xpath

I have the following HTML:
<table class="w98">
<tbody>
<tr>
<tr>
<tr>
<td class="p-controlSetLabel">
<td class="editor-field">
**<span class="k-widget k-dropdown k-header" style="width: 165px;" title="" unselectable="on" role="listbox" aria-haspopup="true" aria-expanded="false" tabindex="0" aria-owns="VatRateId_listbox" aria-disabled="false" aria-readonly="false" aria-busy="false" aria-activedescendant="304dd433-4a68-458b-a6d6-03b72335f1cb">**
<span class="k-dropdown-wrap k-state-default" unselectable="on">
<span class="k-input" unselectable="on">Standard - Standard</span>
<span class="k-select" unselectable="on">
<span class="k-icon k-i-arrow-s" unselectable="on">select</span>
</span>
</span>
<input id="VatRateId" class="k-valid" type="text" value="d20b" style="width: 165px; display: none;" name="VatRateId" data-val-required="Please select a VAT Rate." data-val="true" data-role="dropdownlist">
The input id="VatRateId" is hidden, so I can't access it.
My current solution is the following:
var allDropDownFields = FindElements(By.XPath(".//*[#class='k-dropdown-wrap k-state-default']") -- This will return a list of all drop down fields
Then I loop each one of them to find the correct drop down field I want.
Is there a better way of finding the element without getting a list, I tried the following:
FindElement(By.XPath(".//*[#class='k-dropdown-wrap k-state-default'and #aria-owns = 'VatRateId_listbox']")
and the above code is not work.
HTML code is as below:-
<span class="k-widget k-dropdown k-header" style="width: 165px;" title="" unselectable="on" role="listbox" aria-haspopup="true" aria-expanded="false" tabindex="0" aria-owns="VatRateId_listbox" aria-disabled="false" aria-readonly="false" aria-busy="false" aria-activedescendant="304dd433-4a68-458b-a6d6-03b72335f1cb">* <span class="k-dropdown-wrap k-state-default" unselectable="on"> <span class="k-input" unselectable="on">Standard - Standard</span>
Any better suggestion?

Is it possible for selenium to find a label element by its "for" attribute using xpath?

Is it possible for selenium to find a label element by its "for" attribute using xpath?
I'm testing a site that has 7 yes or no questions in row. The way the CSS is done, you click on the label instead of the input radio button. Normally I'd just find the element by text content, but there is a yes and no for each question.
I'm thinking that a work-around would be to do a javascript click on the input button to get around the "element is not visible" error that I get when trying to .click() the input directly, but I wanted to see if there was a better way to locate the label which is what users would actually be clicking.
*Edit for all of your who can't remember what a label element looks like. How would you select the middle one from this group?
<label for="superLongSetOfRandomCharacters123" class="common-to-all-buttons"> No </label>
<label for="superLongSetOfRandomCharacters345" class="common-to-all-buttons"> No </label>
<label for="superLongSetOfRandomCharacters456" class="common-to-all-buttons"> No </label>
More specifically, this is how they sit in the DOM
<ul class="list-view list-view-dividers">
<li class="list-view-item">
<div class="content">
<div id="variableValue1" class="multipleChoice">
<div class="form-row">
<label>Question 1</label>
<div class="form-row">
<table class="choices" id="randomlyGenerated-1">
<tbody>
<tr>
<td>
<div class="ui-radio"><label for="randomlyGenerated1" class="ui-btn ui-corner-all ui-btn-inherit ui-btn-icon-left ui-radio-off"> Yes</label><input id="randomlyGenerated1" type="radio" name="randomlyGenerated1" value="Yes"></div>
</td>
<td>
<div class="ui-radio"><label for="randomlyGenerated2" class="ui-btn ui-corner-all ui-btn-inherit ui-btn-icon-left ui-radio-off"> No</label><input id="randomlyGenerated2" type="radio" name="randomlyGenerated2" value="No"></div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div id="randomlyGenerated1-Error" class="component-error"></div>
</div>
</div>
</li>
<li class="list-view-item">
<div class="content">
<div id="variableValue2" class="multipleChoice">
<div class="form-row">
<label>Question 2</label>
<div class="form-row">
<table class="choices" id="randomlyGenerated-2">
<tbody>
<tr>
<td>
<div class="ui-radio"><label for="randomlyGenerated21" class="ui-btn ui-corner-all ui-btn-inherit ui-btn-icon-left ui-radio-off"> Yes</label><input id="randomlyGenerated21" type="radio" name="randomlyGenerated21" value="Yes"></div>
</td>
<td>
<div class="ui-radio"><label for="randomlyGenerated22" class="ui-btn ui-corner-all ui-btn-inherit ui-btn-icon-left ui-radio-off"> No</label><input id="randomlyGenerated22" type="radio" name="randomlyGenerated22" value="No"></div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div id="randomlyGenerated2-Error" class="component-error"></div>
</div>
</div>
</li>
</ul>
It works for selecting all labels with target 'for':
label[#for='superLongSetOfRandomCharacters123']
or you can leave #for='' blank for selecting all labels with any values (i.e. label[#for])
I guess, the XPath-Option you are looking for would be "#". so in your case something like [...]/label/#for would return you that for-value, which you could then use to check where you are ;)
Cheers
D
Use find_element_by_xpath
label = self.browser.find_element_by_xpath('//label[#for="id_field"]')

FileUpload with HtmlUnit doesn't fire fileUploadListener-Event (Primefaces auto upload)

I'm trying to automate a file-upload with WebDriver. It works fine for ChromeDriver and FirefoxDriver, but refuses to work for HTMLUnit.
I've already read
Using Webdriver for PrimeFaces file upload
https://stackoverflow.com/questions/21753497/unable-to-automate-filling-of-form-with-file-upload-using-htmlunit
Unable to upload file using Selenium web driver
but both were not helpfull.
The selenium (java)code for this upload-action is quiete simple:
String elementXPath = "//input[contains(#id,'FileUpload_input')]";
WebElement element = driver.findElement(By.xpath(elementXPath));
element.sendKeys(pathToFile);
The html-code of the inputElement is:
<div class="fileupload-buttonbar ui-widget-header ui-corner-top">
<label class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-left fileinput-button" role="button" aria-disabled="false">
<span class="ui-button-icon-left ui-icon ui-icon-plusthick"></span>
<span class="ui-button-text">Upload</span>
<input type="file" id="form:FileUpload_input" name="form:FileUpload_input">
</label>
</div>
<div class="fileupload-content ui-widget-content ui-corner-bottom">
<table class="files"></table>
</div>
maybe necessary, the primefaces-codesnippet of the input-Element:
<div class="#{modalDialog ? 'span5' : 'span6'}">
<p:fileUpload id="FileUpload" mode="advanced" auto="true" sizeLimit="2097152" fileUploadListener="#{ClassView.handleFileUpload}"
label="Upload" allowTypes="/(\.|\/)(gif|GIF|jpe?g|JPE?G|png|PNG)$/" process="#this"
showButtons="false"/>
</div>
As you can see, it's a picure-Upload. The noteworthy characteristic of this upload is, that there is no 'confirm'- or 'submit'-button.
The testautomation works fine for the main-browsers, but fails with htmlUnit. After hours of debugging, I can confirm, that htmlUnit performs the 'sendKeys'-Methode, but this doesn't trigger the fileUploadListener. I already tried to click other Elements, so there is a focusLost-Action, but that didn't help.
Indeed, a 'driver.getPageSource()' after the 'sendKeys' delivers:
<div class="fileupload-buttonbar ui-widget-header ui-corner-top">
<label class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-left fileinput-button" role="button" aria-disabled="false">
<span class="ui-button-icon-left ui-icon ui-icon-plusthick"></span>
<span class="ui-button-text">Upload</span>
<input id="form:FileUpload_input" name="form:FileUpload_input" value="correct\path\to\file\pic.png" type="file" >
</label>
</div>
<div class="fileupload-content ui-widget-content ui-corner-bottom">
<table class="files">
<tbody align="left">
<tr class="template-upload" style="">
<td class="preview"></td>
<td class="name">pic.png</td>
<td class="size"></td>
<td class="progress">
<div class="ui-progressbar ui-widget ui-widget-content ui-corner-all" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuenow="100">
<div class="ui-progressbar-value ui-widget-header ui-corner-left ui-corner-right" style="width: 100%; display: block;"></div>
</div>
</td>
<td class="start">
<button class="ui-button ui-widget ui-state-default ui-corner-all ui-button-icon-only ui-state-hover" type="submit">
<span class="ui-button-icon-left ui-icon ui-icon ui-icon-arrowreturnthick-1-n"></span>
<span class="ui-button-text">ui-button</span>
</button>
</td>
<td class="cancel">
<button class="ui-button ui-widget ui-state-default ui-corner-all ui-button-icon-only">
<span class="ui-button-icon-left ui-icon ui-icon ui-icon-cancel"></span>
<span class="ui-button-text">ui-button</span>
</button>
</td>
</tr>
</tbody>
</table>
</div>
So there are now buttons...why?
I still tried to click the button with xPath "//td[#class='start']//button" but still nothing happend. No Fileupload.
HtmlDriver has javascript enabled and also I'm using NicelyResynchronizingAjaxController(). I already tried waiting for over 30 Seconds but still that doesn't work.
Is there someoone who knows this problem and it's solution...or at least a workaround?
I found the solution by myself.
I was still using selenium 2.40, upgrading to 2.42 solved the problem although the changelog didn't suggested a possible bugfix for my problem.