How to select a value from a drop-down using Selenium and Python - python-3.x

I want to select an option from a select field using Selenium & Python.
The HTML is as follows:
<select autocomplete="off" class="style_input_item" name="AccountEnable" id="Enable" value="0" onchange="onPageDataChange()">
<option value="0" selected="selected"><script>T("Disabled")</script>Disabled</option>
<option value="1"><script>T("Enabled")</script>Enabled</option>
</select>
And I tried as follows:
driver.find_element_by_xpath('//*[#id="Enable"]/option[value="1"]').click()
I received that error:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id="Enable"]/option[value="0"]"}

Just try:
mydriver.find_element_by_xpath('//*[#id="Enable"]/option[#value="1"]').click()
or
mydriver.find_element_by_xpath('//*[#id="Enable"]/option[2]').click()

Make sure you include Select:
from selenium.webdriver.support.select import Select
then
select = Select(driver.find_element_by_id('Enable'))
select.select_by_index(0)

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

Change Element in Python using Selenium

I have been researching for a way to change the element of a web page and have yet found (or understood) how to do it.
In the above element I want to be able to change numerical value of to another number that is represented by a placeholder (i.e. nod for number of draws. Or do I have to do it manually every time I run the program?).
My OS is Ubunt 18.04 and using latest python, selenium and pycharm IDE.
These are just a couple of the websites I have visited and I have visited a lot.
https://www.youtube.com/watch?v=tdgAIDR9snc
http://allselenium.info/javascript-using-python-selenium-webdriver/
Set value of input instead of sendKeys() - selenium webdriver nodejs
WEBELEMENT:
<select name="ctl00$MainContent$ddlRange" id="ctl00_MainContent_ddlRange">
<option selected="selected" value="1"> Single Draw</option>
<option value="2"> 2 Consecutive</option>
<option value="3"> 3 Consecutive</option>
<option value="4"> 4 Consecutive</option>
<option value="5"> 5 Consecutive</option>
<option value="10">10 Consecutive</option>
<option value="20">20 Consecutive</option>
</select>
And my basic code is:
from Selenium import WebDriver
browser = webdriver.Chrome('/usr/bin/chromedriver')
page=browser.get("whatever address of whatever webpage interested in")
elem=browser.find_element_by_css_selector("#ctl00_MainContent_ddlRange")
browser.execute_script("arguments[0].setAttribute('<option selected="selected" value="20">','20 Consecutive</option>')", elem)
My goal is to change the value number in the first part of the option tag from:
<option selected="selected" value="1"> Single Draw</option>
to this:
<option selected="selected" value="44"> Single Draw</option>
In the execute_script I am always getting end of statement expected after the last ).
I am also getting errors of ',' or ')' expected in the areas of "selected" and "20"
Also the css and xpath selectors to these are:
for <select name="ctl00$MainContent$ddlRange" id="ctl00_MainContent_ddlRange">
selector = #ctl00_MainContent_ddlRange
xpath = //*[#id="ctl00_MainContent_ddlRange"]
for <option>
selector = #ctl00_MainContent_ddlRange > option:nth-child(1) (note that nth-child() can be 1 - 7)
xpath = //*[#id="ctl00_MainContent_ddlRange"]/option[1] (note that option[] can also be 1 - 7)
I have the webpage saved to disc and I can go into the html and change that one number then save the file and when I click on it works. I just want to be able to automate it.
Any help is much appreciated
Idzireit
EDIT:
So far this is the closest I have come to actually inserting the value I want. The new code is as follows:
browser.execute_script("""arguments[0].setAttribute
('select', '<option selected="selected" value="nod"> &snbsp;draws </option>')""", get_draws)
Results in this:
<option value="20" select="<option selected="selected" value="nod"> &nbsp;&snbsp;draws </option>">20 Consecutive</option>
The javascript I am trying to inject is getting injected in the middle of the element I am trying to modify (notice the option sequence repeats in the middle of the first bracket).
How can I correct this to make it look like this:
<option selected="selected" value="a number I am trying to change"> Draw<\option>
EDIT SOLVED:
Figured out how to change the value of an element. I had to change the search method of that element to this:
get_draws = browser.find_element_by_tag_name('option')
Then my next line was pretty simple:
browser.execute_script("""arguments[0].setAttribute('value', '100')""", get_draws)
For your second question, parametrizing the int value, try the following:
var = 100
js = f"arguments[0].setAttribute('value', '{var}')"
browser.execute_script(js, get_draws)
Just pass the value you want to the var variable.

Select a dropdown using Python + Selenium

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

Selenium web driver in nodejs: no such element Error

I am trying to locate the element using selenium web driver having name attribute on select element using the following statement but I got an error that "no such element"
driver.findElement(By.name('TripType option:nth-child(ST)')).click()
Then Select element where I am trying to locate that above id is
<select name="TripType" onchange="toggleT('divt1')">
<option value="ST">Single Trip</option>
<option value="AMT">Annual Multi Trip</option>
<option value="LS">Long Stay</option>
</select>
Why not try this -
var value1 = element(by.tagname('option')).getAttribute('value');
expect(value1).toEqual('ST');
If there are multiple options due to multiple options tag, then it can be modified to
var values2 = element(by.name('TripType')).all(by.tagname('option')).getAttribute('value');
expect(value2).toEqual([value1,value2,value3]);

how to select a value in combo boxes with geb groovy

i tried to select a value out of a combo box via groovy(geb).
the html code is:
<select id="entity-list-form:statusSearchBtn" name="entity-list-form:statusSearchBtn" size="1" style="width: 200px;">
<option value="">alle</option>
<option value="REGISTERED" selected="selected">Wartet auf Bestätigung</option>
<option value="REJECTED">Registrierung zurückgewiesen</option>
<option value="APPROVED">Registrierung angenommen</option>
<option value="UNSUBSCRIBED">Abgemeldet</option>
</select>
i tried to access these elements via
$("entity-list-form").statusSearchBtn = "alle"
or
$("entity-list-form").statusSearchBtn.value() == "alle"
a different approach was in the page siet to add
statusSearchBtn { $('select[name$="entity-list-form:statusSearchBtn"]') }
and also the case with the name only like entity-list-form. in this caes i tried it like
statusSearchBtn = "alle"
or
statusSearchBtn.value() == "alle"
the last one end without any errors, but didnt change the selected value to "alle".
the former one ended in No such property: statusSearchBtn for class: geb.navigator.EmptyNavigator.
i greatly apprichiate any advice,
I think:
$("select", name : "entity-list-form:statusSearchBtn").value('alle')
$("select", name : "entity-list-form:statusSearchBtn").value() = 'alle'

Resources