Select a dropdown using Python + Selenium - python-3.x

I am writing an automation for work and am stuck with a dropdown. The particular select box in question is as follows:
<span class="a-dropdown-container" id="select-size-bulk-asin">
<select name="display_type" class="a-native-dropdown">
<option value="SMALL-IMAGES">SMALL-IMAGES</option>
<option value="LARGE-IMAGES">LARGE-IMAGES</option>
<option value="TEXT">TEXT</option>
</select>
<span tabindex="-1" data-a-class="a-spacing-small" class="a-button a-button-dropdown a-spacing-small">
<span class="a-button-inner">
<span class="a-button-text a-declarative" data-action="a-dropdown-button" aria-haspopup="true" role="button" tabindex="0" aria-pressed="false" aria-owns="2_dropdown_combobox">
<span class="a-dropdown-prompt">SMALL-IMAGES</span>
</span>
<i class="a-icon a-icon-dropdown"></i>
</span>
</span>
</span>
It defaults to 'SMALL Images' and I would like to select the 'TEXT' option. I am receiving element not clickable error. The page is simple and the element is visible on the screen.
The list of methods I did try are:
Used WebDriverWait to wait for the element to be visible;
Used WebDriverWait to wait for the element to be clickable;
Used the select class to set the selected option;
I also read through a question.
I am thinking if I should just go to the next element and send Shift+Tabs until I reach this drop down and then down arrow keys. But would like to use that only as the last resort.
NOTE:
- I am using Python 3 and Chrome.

You can try this code to select value from drop down :
select = Select(driver.find_element_by_id('select-size-bulk-asin'))
select.select_by_visible_text('TEXT')
However,as you have mentioned you are receiving element not clickable exception. you can try this code :
WebDriverWait(browser, 30).until(EC.element_to_be_clickable((By.ID, "select-size-bulk-asin")))
As a last resort you can go ahead with :
drop_down= driver.find_element_by_id("select-size-bulk-asin")
drop_down.click()
actions = ActionChains(driver)
actions.send_keys(Keys.ARROW_DOWN)
actions.send_keys(Keys.ARROW_DOWN)
actions.send_keys(Keys.ENTER)
actions.perform()

Related

how to select a option on a web page drop down list using selenium with python

I wanted to select the value "LK" in the drop down list on my web page. please help.
i tried to slect by xpath as follows
driver.find_element_by_xpath("//select[contains(text(), 'GLOBAL')]").click()
driver.find_element_by_xpath("//span[contains(text(), 'LK')]/..").click()
but i get following error:
selenium.common.exceptions.ElementNotVisibleException: Message: element not interactable
Below is the web element code im trying to fetch:
<select style="border-radius: 3px;" ng-model="selectedRcc.value" ng-options="item for item in ['LK', 'US', 'GLOBAL']" class="ng-pristine ng-valid ng-touched" tabindex="0" aria-invalid="false">
<option value="0" label="LK">LK</option>
<option value="1" label="US">US</option>
<option value="2" selected="selected" label="GLOBAL">GLOBAL</option>
</select>
Use the below xpath
//select[#class='ng-pristine ng-valid ng-touched']/option[.='LK']
Code:
driver.find_element_by_xpath("//select[#class='ng-pristine ng-valid ng-touched']/option[.='LK']").click()
When you have select element it always good to use selenium select class to select item from drop down menu.
select=Select(driver.find_element_by_xpath("//select[#class='ng-pristine ng-valid ng-touched']"))
select.select_by_visible_text('GLOBAL')
select.select_by_visible_text('LK')
OR you can use index.
select=Select(driver.find_element_by_xpath("//select[#class='ng-pristine ng-valid ng-touched']"))
select.select_by_index(0) #LK
select.select_by_index(2) #GLOBAL
To use above code you need to have following imports.
from selenium.webdriver.support.select import Select

Unable to select a value from dropdown in webpage using selenium VBA

I am trying to select a value from a dropdown in a web page using Seleinum with VBA binding, but I am unable to do it.
This is the code I tried bot.FindElementById("holder-samples-date-type-inputEl").SendKeys "XX"
I am not getting any error the code runs it click the dropdown button and the list is shown but no value is selected and the dropdown does not change.
I have the same project with VBA and IE (DOM elements) and this code works fine in Internet explorer IE.document.getElementById("holder-samples-date-type-inputEl").Value = "XX"
The interesting part of this dropdown is after clicking the dropdown icon i am unable to inspect the list (like do a right click on the list) and if i click anywhere else in the web page or change the window the dropdown list get closed (like it retracts) note: this is not a mouseover drop down. Also, when the list is open there seems to be additional code added (I have pasted a snippet of the code)
Below is the HTML from the web page.
<div id="holder-samples-date-type-triggerWrap" data-ref="triggerWrap" class="x-form-trigger-wrap x-form-trigger-wrap-default x-form-trigger-wrap-invalid">
<div id="holder-samples-date-type-inputWrap" data-ref="inputWrap" class="x-form-text-wrap x-form-text-wrap-default x-form-text-wrap-invalid"><input id="holder-samples-date-type-inputEl" data-ref="inputEl" type="text" role="combobox" aria-label="Chargebacks: Date Type" aria-readonly="false" aria-required="true" aria-haspopup="true" aria-expanded="false" aria-autocomplete="list" size="1" name="P_samples_Date_Type" readonly="readonly" class="x-form-field x-form-required-field x-form-text x-form-text-default x-form-invalid-field x-form-invalid-field-default" autocomplete="off" aria-labelledby="holder-samples-date-type-labelEl" aria-owns="boundlist-1206" componentid="holder-samples-date-type" data-errorqtip="<ul class="x-list-plain"><div>Date Type</div><li>This field is required</li></ul>" aria-invalid="true"></div>
<div id="holder-samples-date-type-trigger-picker"
aria-label="Trigger" class="x-form-trigger x-form-trigger-default x-form-arrow-trigger x-form-arrow-trigger-default "><img class="combo-trigger-placeholder" src="https://www.sample.net/clr/images/16x16_transparent.png" title="combobox trigger" alt="combobox trigger"></div>
</div>
Below is the image
Since I am unable to do a right click on the dropdown option. I did a 'Find" option in the developer window and below is the webpage code for the dropdown item and it is inside a iframe
<div id="boundlist-1206-listWrap" data-ref="listWrap" role="presentation" class="x-boundlist-list-ct x-unselectable" style="overflow: auto; height: auto;">
<ul id="boundlist-1206-listEl" data-ref="listEl" class="x-list-plain">
<div data-qtip="" class="x-boundlist-item x-boundlist-item-over" tabindex="-1" data-recordindex="0" data-recordid="31" data-boundview="boundlist-1206" id="ext-932" role="option" aria-selected="false">Received Date</div>
<div data-qtip="" class="x-boundlist-item" tabindex="-1" data-recordindex="1" data-recordid="32" data-boundview="boundlist-1206" id="ext-933" role="option" aria-selected="false">Status Date</div>
<div data-qtip="" class="x-boundlist-item" tabindex="-1" data-recordindex="2" data-recordid="33" data-boundview="boundlist-1206" id="ext-934" role="option" aria-selected="false">Due Date</div>
<div data-qtip="" class="x-boundlist-item" tabindex="-1" data-recordindex="3" data-recordid="34" data-boundview="boundlist-1206" id="ext-935" role="option" aria-selected="false">Disposition Date</div>
</ul>
</div>
Some points that may help:
1) If inside an iframe you will need to switch to the iframe first
bot.SwitchToFrame(identifier)
Identifier can be the element selected by iframe name/id etc.
2) You can try click and hold on the input text box before using your SendKeys
bot.FindElementById("x").ClickAndHold
'sendkeys line here
3) You can try setting the value with javascript
bot.ExecuteScript "document.getElementById('x').value = 'xx';"

VBA fire event on empty HTML div

I try to automate some manual processes by using VBA automation. One of them is to click on a element that has an empty content and at the moment I am not able to figure out how to deal with it
The HTML code that I am trying to click on:
<div id="searchcombobox-1077-triggerWrap" data-ref="triggerWrap" class="x-form-trigger-wrap x-form-trigger-wrap-toolbar">
<div id="searchcombobox-1077-inputWrap" data-ref="inputWrap" class="x-form-text-wrap x-form-text-wrap-toolbar">
<input id="searchcombobox-1077-inputEl" data-ref="inputEl" type="text" role="combobox" size="1" name="searchcombobox-1077-inputEl" placeholder="Account Number" tabindex="-1" class="x-form-field x-form-text x-form-text-toolbar " autocomplete="off" componentid="searchcombobox-1077">
</div>
<div id="searchcombobox-1077-trigger-picker" class="x-form-trigger x-form-trigger-toolbar x-form-search-trigger x-form-search-trigger-toolbar "></div>
</div>
The VBA code used is:
HTMLDoc.getElementById("searchcombobox-1077-inputEl").Value = '11xx111'
Set click_el = HTMLDoc.querySelector("#searchcombobox-1077-trigger-picker")
With click_el
.Focus
.FireEvent "onclick"
End With
What should be the approach that I need to take into consideration since the div element that I need to click on is empty?
Thanks,

Python Selenium can't select dropdown (chrome webdriver)

I have a dropdown element, I want to select the All option, the corresponding HTML code is:
<div class="dataTables_length" id="indicators_length">
<label>
<span class="result-mune">
<span>Results </span>per page:
</span>
<select name="indicators_length" aria-controls="indicators" class="jcf-hidden">
<option value="25">25</option>
<option value="50">50</option>
<option value="100">100</option>
<option value="200">200</option>
<option value="-1">All</option>
</select>
<span class="jcf-select jcf-unselectable">
<span class="jcf-select-text">
<span class="">25</span>
</span>
<span class="jcf-select-opener"></span>
</span>
</label>
</div>
the select element is not highlighted using the browser Inspect method, looks like this drop down is triggered by js.
I tried to use the Select class described here:
select = Select(self._wait.until(EC.presence_of_element_located_by((By.XPATH, "//div[#id = 'indicators_length']//select[#name = 'indicators_length']")))
select.select_by_value('-1')
not working. and ActionChain method and even execute_script method, all not working. This bothers me a lot, hope somebody can help.
you don't really need to select the option just click the span and it will set the option automatically.
driver = webdriver.Chrome()
driver.get("......")
# click "OK, I Agree" cookies
driver.find_element_by_css_selector('.agree-button.eu-cookie-compliance-default-button').click()
# scroll down to make dropdown option visible
driver.find_element_by_css_selector('h4.pane-title').location_once_scrolled_into_view
select = driver.find_element_by_xpath('//span[#class="result-mune"]/following-sibling::span')
select.click()
# wait until javascript generate fake option element because it actually span
time.sleep(1)
select.find_element_by_css_selector('ul li span[data-index="4"]').click()
try this one:
driver.execute_script('document.getElementsByName("indicators_length")[0].value = 50;
If its created and loaded after page load make sure you add some sleep to let it render;
I tried using the Selenium Select class, it can find the element but it cannot select an option from the element. Not sure whats going on, could be the class "jcf-hidden" on the element.
Having said that, I took a stab at it and below is my approach. Try it out, it worked on my system, you have to handle the clicking of "OK, I Agree" button click, I did that manually.
import time
from selenium.webdriver import Chrome
driver = Chrome()
driver.get('https://www.topuniversities.com/university-rankings/world-university-rankings/2019')
# Remove this nasty time.sleep and implement webdriver wait
# Handle the OK, I Agree
time.sleep(5)
i_agree = driver.find_element_by_css_selector('button[class*="eu-cookie-compliance-default-button"]')
i_agree.click()
time.sleep(5)
# Open the Select
rankings_length = driver.find_element_by_id('qs-rankings_length')
select_opener = rankings_length.find_element_by_class_name('jcf-select-opener')
select_opener.click()
# loop through the list
select_content = rankings_length.find_element_by_class_name('jcf-list-content')
for content in select_content.find_elements_by_tag_name('li'):
if content.text == 'All':
content.click()

What DOM element to specify with a Directive?

I'm using Soda (node.js) w/ Selenium RC to automate browser testing.
If I have an AngularJS directive -> say a button that has a spinner -> and it appears multiple times on a page, how can I make sure to click on that particular button...when it has no DOM ID or unique class? In my case, "Login" and "Some Other Button" are dynamic and would be unique for all buttons in the ng-view.
<my-requesting-button text="Login" class="ng-isolate-scope ng-scope">
<button type="submit" class="btn btn-large">
<img src="/img/progress.gif" style="display: none;">
<span>Login</span>
</button>
</requesting-button>
<my-requesting-button text="Some Other Button" class="ng-isolate-scope ng-scope">
<button type="submit" class="btn btn-large">
<img src="/img/progress.gif" style="display: none;">
<span>Some Other Button</span>
</button>
</requesting-button>
Soda (basically directly from the example):
browser
.chain
.session()
.open('/')
.clickAndWait('...LOGIN BUTTON REF?')
.waitForPageToLoad(2 * 1000)
.clickAndWait('...SOMEOTHER BUTTON REF?')
.waitForPageToLoad(2 * 1000)
You can give it a unique ID/class name. If you know this element will only ever be on the page once, you can give it a unique ID. If not, you can give it a class name so that Selenium can find just the first one, or the one near your login form.
To do this, you can either pass the text value (e.g. "Login") to your button so that it has it:
<button class="{{text}}Button">
or you can tell Selenium to find the button that comes right after the element whose text attribute is "Login".

Resources