How to click on hidden select under button - python

I need to select a URL value, but I don't understand how to do it
<span class="select select_layout_content select_size_s select_theme_normal
queries-filter-item__indicator i-bem select_js_inited _popup-destructor
_popup-destructor_js_inited"
data-bem="{"select":{"live":false}}" title="">
<button class="button button_arrow_down button_theme_normal button_size_s select__button i-bem button_js_inited" type="button" autocomplete="off" role="listbox" aria-haspopup="true" aria-expanded="false" data-bem="{"button":{}}">
<span class="button__text" aria-hidden="true">Total shows</span>
</button>
<select class="select__control" id="uniq16686900391151" tabindex="-1" aria-hidden="true">
<option class="select__option" value="TOTAL_SHOWS_COUNT" selected="selected">Total shows</option>
<option class="select__option" value="TOTAL_CLICKS_COUNT">Clicks count</option>
<option class="select__option" value="AVERAGE_SHOW_POSITION">Average Position</option>
<option class="select__option" value="TOTAL_CTR">CTR, %</option>
<option class="select__option" value="URL">URL</option>
<option class="select__option" value="QUERY">Text</option>
</select>
</span>
My code (the last command) get "Message: element not interactable: Element is not currently visible and may not be manipulated"
# Click on make filter - is works
driver.find_element(By.XPATH, "/html/body/div[3]/div[1]/div[1]/div[2]/div[2]/div/form/span/span").click()
time.sleep(1)
# Click on select button - is works
driver.find_element(By.XPATH, "/html/body/div[3]/div[1]/div[1]/div[2]/div[2]/div/form/div[1]/div/span[1]/button").click()
time.sleep(5)
# Click on URL option
driver.find_element(By.XPATH, "/html/body/div[3]/div[1]/div[1]/div[2]/div[2]/div/form/div[1]/div/span[1]/select/option[5]").click()

I'm not sure if this will work with the element that has the attribute aria-hidden="true".
There is a special class in Selenium for the select elements. First, you need to import the Select class. You can try to use this code:
from selenium.webdriver.support.select import Select
# Click on make filter - is works
driver.find_element(By.XPATH, "/html/body/div[3]/div[1]/div[1]/div[2]/div[2]/div/form/span/span").click()
time.sleep(1)
# Click on select button - is works
driver.find_element(By.XPATH, "/html/body/div[3]/div[1]/div[1]/div[2]/div[2]/div/form/div[1]/div/span[1]/button").click()
time.sleep(5)
# Click on URL option
dropdown = Select(driver.find_element(By.ID, 'uniq16686900391151'))
dropdown.select_by_value('URL')

Related

Python / Selenium - access not interactive element

What I need:
Select one of the options from the drop-down list
Only BUTTON element is visible (SELECT element is not visible)
This is html code:
<td data-bind="css: { 'cell-error': applicantNames.hasError }, attr: { title: applicantNames.message }" title="">
<select data-bind="options: $parent.applicants, multivalue: applicantNames" multiple="multiple" style="display: none;">
<option value="ABC">ABC</option>
<option value="XYZ">XYZ</option>
</select>
<button type="button" class="ui-multiselect ui-widget ui-state-default ui-corner-all" aria-haspopup="true" style="width: 236px;"><span class="ui-icon ui-icon-triangle-2-n-s"></span><span>Please select...</span></button>
</td>
This is how it looks like:
The problem is that I would like to select one of these options by using Selenium, but because the SELECT element is not interactable/not visible ... not sure what to do...
If you will choose an option from the drop-down the BUTTON element will be updated like this
<button type="button" class="ui-multiselect ui-widget ui-state-default ui-corner-all ui-state-active" aria-haspopup="true" style="width: 236px;">
<span class="ui-icon ui-icon-triangle-2-n-s"></span><span>ABC, XYZ</span>
</button>
Even if I will update the BUTTON section manually, it will still not update the element visually..... so I'm a bit stuck to find a way how to interact with these elements by using Selenium...
P.S. I'm looking for ANY headless methods, e.g. Selenium / JavaScript execution through selenium and e.t.c...
See the problem is that "This is not built using Select - option tag", so one can not directly make use of Select class from Selenium support package.
Instead you can try to click on that menu using Selenium .click()
in your case something like this
driver.find_element_by_xpath("//span[contains(text(), 'Please select')]").click()
and then store all the visible option in a list of web elements using find_elements and then iterate the list :
for option in list_of_options:
if option.text == "your desired option"
option.click()

Select option problem via Selenium - Unable to locate element error

I am new to Python and Selenium. I couldn't find how to select option on web page.
Website code Screenshoot of select tag
<div class="col-xs-6 middle">
<select class="form-control select-box hisse" size="1" id="MENKUL_NO" name="MENKUL_NO" style="display: none;">
<option value="" selected="">Seçiniz</option>
<option value="1368">ACSEL-ACIPAYAM SELÜLOZ</option>
<option value="207">ADEL-ADEL KALEMCILIK</option>
</select>
<div class="chosen-container chosen-container-single chosen-container-active chosen-with-drop" style="width: 429px;" title="" id="MENKUL_NO_chosen">
<a class="chosen-single" tabindex="-1">
<span>Seçiniz</span>
<div><b></b></div></a>
<div class="chosen-drop">
<div class="chosen-search">
<input type="text" autocomplete="off">
</div>
<ul class="chosen-results">
<li class="active-result result-selected highlighted" data-option-array-index="0" style="">Seçiniz</li>
<li class="active-result" data-option-array-index="1" style="">ACSEL-ACIPAYAM SELÜLOZ</li>
<li class="active-result" data-option-array-index="2" style="">ADEL-ADEL KALEMCILIK</li>
</ul></div></div></div>
I try this but doesn't find element Select(browser.find_element_by_xpath('//*[#id="MENKUL_NO"]')).select_by_value(1)
Try this
select_by_value("ADEL-ADEL KALEMCILIK") or
select_by_index(1)
I see that it is select and option tag in HTML. You can use the below code with Explicit wait.
select = Select(wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "select[id='MENKUL_NO'][name='MENKUL_NO']"))))
select.select_by_visible_text('ACSEL-ACIPAYAM SELÜLOZ')
Imports :
from selenium.webdriver.support.select import Select
First, try a different locator for the main element, as even id may be not unique.
Afterwards, try finding by index.
locator = driver.find_element_by_xpath('//select[#class="form-control select-box hisse"]')
select = Select(locator)
select.select_by_index(1)
Also, try selecting by value:
select.select_by_value('1368')
Value is dropdown option value which is 1368 or 207. The first option does not have a value. If you want to select the first element, use select_by_index(1)
Or, use:
select.select_by_visible_text('Seçiniz')
I found there is a frame tag. that is why it couldnt find.
browser.switch_to_frame("ifr")
with this code. I accomplish my task.
Thank you all who responses

How to select a value from options as per the HTML through Selenium and Python

I'm trying to change the value of a dropdown and facing problem while selecting the value from <select> tag and <option> tag.
This is HTML that i want to change.
<form name="frmSearch" action="" onsubmit="return false;">
<span class="seljs_title ">
<input id="searchByInput91" name="searchBy91" type="text" readonly="readOnly" class="m-tcol-c" style="width: 110px;">
<input type="hidden" name="searchBy" value="0" style="display: none;">
</span>
<select id="searchBy" name="" class="m-tcol-c" onchange="$('query').focus();" style="width:110px;display:none;">
<option value="0">one</option>
<option value="1">two</option>
<option value="3">three</option>
<option value="4">four</option>
<option value="5">five</option>
</select>
<input type="text" id="query" name="query" style="ime-mode:active" value="" onkeydown="if (event.keyCode == 13) {nhn.search.goSearch(event);clickcr(this, 'sch.bsearch','','',event)}"
class="m-tcol-c border-sub text">
<a href="#" onclick="nhn.search.goSearch(event);clickcr(this, 'sch.bsearch','','',event); return false;">
<img src="https://cafe.pstatic.net/cafe4/hidden.gif" width="42" height="21" alt="검색" class="btn-search-green">
</a>
</form>
I'm using Webdriver(Chrome).This is my code.
driver.find_element_by_name('frmSearch').find_element_by_id('searchByInput91').click()
This code makes me to click the dropdown and open the options. After that, when I use this code :
from selenium.webdriver.support.ui import Select
select = Select(driver.find_element_by_id('searchBy'))
select.select_by_value('1').click()
OR
driver.find_element_by_id("searchBy").send_keys("two")
Error messages always come out.
ElementNotVisibleException: Message: element not visible: Element is not currently visible and may not be manipulated
(Session info: chrome=69.0.3497.81)
(Driver info: chromedriver=2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e),platform=Windows NT 10.0.17134 x86_64)
what should I do?
As per the HTML you have provided it seems that the <select> tag contains style attribute as display: none;. So you can use the following solution to select an option:
from selenium.webdriver.support.ui import Select
# other lines of code
driver.find_element_by_name('frmSearch').find_element_by_id('searchByInput91').click()
element = driver.find_element_by_xpath("//select[#class='m-tcol-c' and #id='searchBy']")
driver.execute_script("arguments[0].removeAttribute('style')", element)
select = Select(driver.find_element_by_xpath("//select[#class='m-tcol-c' and #id='searchBy']"))
select.select_by_value('1')

Dropdown Menus in Selenium - Python 3

I am trying to access a dropdown menu using selenium for HTML that looks like this:
<span class="k-pager-sizes k-label">
<span title="" class="k-widget k-dropdown k-header" unselectable="on" role="listbox" aria-haspopup="true" aria-expanded="false" tabindex="0" aria-owns="" aria-disabled="false" aria-readonly="false" aria-busy="false" aria-activedescendant="8e90d557-7e8d-4c5c-b906-202fd78c6d0a"><span unselectable="on" class="k-dropdown-wrap k-state-default">
<span unselectable="on" class="k-input">20</span><span unselectable="on" class="k-select">
<span unselectable="on" class="k-icon k-i-arrow-s">select</span>
</span>
</span>
<select data-role="dropdownlist" style="display: none;">
<option value="10">10</option><option value="15">15</option>
<option value="20">20</option><option value="50">50</option>
<option value="100">100</option>
</select>
<span>items per page</span>
I have tried the following without success:
try:
driver = webdriver.Chrome('/Users/opusandcaymus/Election/chromedriver')
driver.get('http://mcad-tx.org/Property-Search-Result?searchtext=Maple%20Branch')
#dropdown=driver.find_element_by_xpath('//*[#id="grid"]/div[3]/span[2]/span/select')
#select = Select(dropdown)
dropdown = driver.find_elements_by_tag_name("option")
for row in dropdown:
print(row.value)
driver.close()
except:
print("error")
driver.close()
raise
Does anyone know how to find the options by the values? I want to select 100 every time the page is opened.
Your select dropdown is hidden so for that i would suggest first make it visible using javascriptexecuter then select the value
Use below code :
element=driver.find_element_by_xpath("//select[#data-role='dropdownlist']")
driver.execute_script("arguments[0].setAttribute('style', 'display: block;')",element)
select = Select(element)
select.select_by_value("100")
And The other way is use Explicitwait
First click on down arrow and of dropdown and then click on value 100
Something like below code in python :
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
element = wait.until(EC.visibility_of_element_located((By.XPATH, ".//*[#id='grid']/div[3]/span[2]/span/span/span[2]/span")))
element.click
element = wait.until(EC.visibility_of_element_located((By.XPATH, ".//ul[#data-role='staticlist']/li[5]")))
element.click
Note : please make correction as per python syntax

Dropdown menus in python / selenium

Trying to autofill a form using python and selenium. Dropdown menu html is:
<select id="typeOfTeacher" class="chosen-select-no-single ng-untouched ng-dirty ng-valid-parse ng-valid ng-valid-required" required="" ng-class="{ 'has-error' : positionDetailForm.typeOfTeacher.$invalid && !positionDetailForm.typeOfTeacher.$pristine }" ng-change="vm.setRequired()" tabindex="-1" ng-model="vm.data.typeOfTeacher" name="typeOfTeacher" data-placeholder="Select" style="display: none;">
<option value="" disabled="" selected="">Select</option>
<option class="ng-binding ng-scope" value="1" ng-repeat="teacherType in vm.teacherTypes">No position at the moment</option>
<option class="ng-binding ng-scope" value="2" ng-repeat="teacherType in vm.teacherTypes">Supply</option>
<option class="ng-binding ng-scope" value="3" ng-repeat="teacherType in vm.teacherTypes">Permanent</option>
</select>
Python code is:
elem = Select(browser.find_element_by_id('typeOfTeacher'))
elem.select_by_value("1")
Error is "element is not currently visible and may not be interacted with".
I have not used the python Select method, but I would guess that the error message means that the menu is not being opened, and therefore an element in the menu is still hidden and cannot be interacted with.
Try something like this:
element = driver.find_element_by_id('typeOfTeacher').click()
driver.find_element_by_css_selector("[value=\"1\"]").click()
This would work
element = driver.find_element_by_id('typeOfTeacher').click()
element.find_element_by_xpath(".//option[#value='1']").click()
It looks like timing issue. You should try using Waits.
I would suggest you, use WebDriverWait to wait until dropdown visible before interaction as below :-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
element = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID, "typeOfTeacher")))
select = Select(element)
select.select_by_value("1")

Categories

Resources