Python / Selenium - access not interactive element - python

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

Related

Python Selenium webdriver get XPATH and select dropdown

I've found the word 'Burger' in HTML table with this code
findRow = driver.find_element(By.XPATH, "//*[contains(text(),'Burger')]").value_of_css_property('#name')
how do I can get XPATH 'Burger'?
how do I can select the column beside it (example select 'Fish' beside column 'Burger') and then submit button?
HTML code
<tbody>
<tr>
<td>..</td>
<td>.....</td>
</tr>
<tr>
<td>Burger</td>
<td>
<select class="form-control input-sm" id="sel222" name="sel222" type="68" group="433" onchange="count(this)">
<option value="1">Vegetables</option>
<option value="2">Fish</option>
<option value="3">Beef</option>
</select>
</td>
</tr>
</tbody>
</table>
<button type="button" class="btn btn-primary" id="submit"><span class="fa fa-save"></span> Save</button>
Based on the HTML posted, the simplest XPath to find "Burger" would be,
//td[text()='Burger']
The XPath to find the SELECT in the cell to the right of the "Burger" cell would be,
//td[text()='Burger']/following-sibling::td/select
^ the XPath to find the TD that contains "Burger", from above
^ then find the sibling TD
^ then the SELECT child of the sibling
Putting this all together to select "Fish" from the SELECT element next to the "Burger" cell and click Submit,
from selenium.webdriver.support.ui import Select
select = Select(driver.find_element(By.XPATH, "//td[text()='Burger']/following-sibling::td/select"))
select.select_by_visible_text("Fish")
driver.find_element(By.ID, "submit").click()
In this scenario, you can identify the select list box using xpath following technique. Use the below xpath to identify select object
//td[contains(.,'Burger')]/following::select
You can select the option Burger using the below XPath:
.//td[text()='Burger']
You can select the options inside select tag using the below XPath:
.//td[text()='Burger']//parent::tr//select/option

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

ElementNotVisibleException: element not visible Python

I'm new to Python and Selenium and I've come across an issue when trying to click on a button via webdriver.
The HTML of the div I'm trying to click through is:
<div class="">
<form method="POST">
<input class="hide" id="accept" name="accept" type="text" value="yes" readonly="">
<a href="/" class="btn btn-red">
<div class="svg-group group icn-bg-circle" data-png-fallback="">
<svg width="8" height="8">
<use xmlns:xlink=" " xlink:href=""></use>
</svg>
</div>
Decline
</a>
<button class="btn btn-green" type="submit">
<div class="svg-group group icn-bg-circle" data-png-fallback="">
<svg width="8" height="8">
<use xmlns:xlink="" xlink:href=""></use>
</svg>
</div>
Accept
</button>
</form>
</div>
I want to use .click() on the button with class name 'class="btn btn-green"'.
I have used the following code to select the element (after following the solutions in other similar SO questions.
driver.find_element_by_css_selector(".btn-green")
It looks like webdriver can find the element but when I try to apply .click() I get:
ElementNotVisibleException: element not visible
I then did some digging into the element (thinking I could bypass by using x, y coordinates to click through) but after using .size and .location I get:
{'x': 0, 'y': 0}
{'height': 0, 'width': 0}
Any help you could give on how to get around this would be really appreciated.
Thanks
The error hints to the potential problem, selenium does find it but it is not visible so you cannot click on it. You could check for visibility with is_displayed before clicking on it. Another possibility is if there is another button with that class that is hidden, try to see if you can select a visible element in the browser console (Control+Shift+J) with $(".btn-green")
Some xpaths that might work that are more robust for your problem:
//button[#type="submit" and text()="Accept"]
//button[#type="submit" and contains(#class, "btn-green")]
To click on the button with text as Accept you can use the following line of code :
driver.find_element_by_css_selector("button.btn.btn-green[type='submit']")

Finding elements in Selenium

My button looks like this:
<button type="button" data-toggle="modal" data-target="#modal-order-export" data-backdrop="false" class="btn btn-default btn-sm">
<i class="fa fa-file-excel-o"></i> 엑셀 저장
</button>`
How can I click this object via selenium to automate some work? I tried:
driver.find_element_by_link_text('엑셀 저장').click()
You could take its xpath by right-clicking the element in developer tools and choosing copy. Then just find the element using xpath.
The WebElement which you are trying to search/find is within <button> tag hence find_element_by_link_text may not work. We will be using an xpath to locate the WebElement as follows:
driver.find_element_by_xpath("//button[#class='btn btn-default btn-sm']/i[#class='fa fa-file-excel-o']").click()

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

Categories

Resources