Trying to select an item from a drop down list using a variable.
I'm able to click the drop down list, I'm able to click on the item with explicit text but not using a variable.
Chrome (v 75.0.3770.142) using Selenium Basic ChromeDriver (v 75.0.3770.140) in Excel (2013) VBA.
it works if i use explicit:
Obj.FindElementByXPath("//option[#label='Z01 - Customer Request - Paid']").Click
but doesn't work if using a string variable:
Obj.FindElementByXPath("//option[#label=SomeStringVariable]").Click
Here's the HTML:
<select id="tickboxreason" name="boxreason" class="emergency-reasons form-control ng-touched ng-dirty ng-valid-parse ng-valid ng-valid-required" ng-model="currentCartConfig.boxreason" ng-options="reason.displayValue for reason in reasonList.emergencyOrderReasons | orderBy:'code' track by reason.code" ng-change="updateRDD()" ng-required="currentCartConfig.emergencyFlag" required="required">
<option value="">Select Reason</option>
<option value="Z01" label="Z01 - Customer Request - Paid">Z01 - Customer Request - Paid</option>
</select>
You have missed the string quotes.Try the below option.
Obj.FindElementByXPath("//option[#label='" & SomeStringVariable & "']").Click
I would go with a faster css attribute = value selector
obj.FindElementByCss("[label='" & variable & "']").click
Related
I have a list of suppliers and a defaultSupplier assigned to a template, I add the suppliers to a dropdown and iterate through them using a foreach and I try to make the default selected value depending on the defaultSupplier like this:
<select name="supplier_id" id="supplier_id" class="selectpicker" data-live-search="true" data-width="100%" data-show-tick="false">
<option value="" {if $defaultSupplier eq ""}selected="selected"{/if}>-- {"Choose a supplier"|t} --</option>
{foreach $suppliers as $s}
<option value="{$s.supplier_id}" {if ($defaultSupplier == $s.supplier_name)}selected="selected"{/if}>{$s.supplier_name}</option>
{/foreach}
</select>
But it doesn't work, I get the "Choose a supplier" option selected then I have to manually select my supplier from the dropdown
I had to close the page and navigate on it again and it worked
I need some help. Chrome (v 75.0.3770.100) using Selenium Basic ChromeDriver (v 75.0.3770.140) in Excel (2013) VBE. There's an input box which generates a dynamic list if the customer id# exists. I wish to fill in the customer id# then select from the dynamic drop down. But first step, I'm struggling to input my text to the box. I'm able to click on the box with
obj.FindElementById("selectcustTxt").Click
but when I try to fill in the box with:
obj.FindElementById("selectcustTxt").Value = "1111"
I get an error Run-time error '424': Object required
I tried the following FindElementByXPath with both .Value and .Text but get the same Run-time error '424': Object required
obj.FindElementByXPath("//input[#class='form-control cust-autosuggest ng-pristine ng-valid ng-touched'][#id='selectcustTxt']").Value = "1111"
Here's the HTML:
<div class="form-group search-field"><input id="selectcustTxt" type="text" class="form-control cust-autosuggest ng-valid ng-touched ng-dirty ng-valid-parse" autocomplete="off" plshholder="Enter Cust name" autocomplepte="off" ng-model="cust" suggest-type="custService" sh-autosuggest="custAddresses" data-validation="required">
To send a character sequence within the desired element you can use either of the following Locator Strategies:
Using FindElementByCss:
obj.FindElementByCss("input.form-control.cust-autosuggest.ng-valid.ng-touched.ng-dirty.ng-valid-parse#selectcustTxt").SendKeys ("1111")
Using FindElementByXPath:
obj.FindElementByXPath("//input[#class='form-control cust-autosuggest ng-valid ng-touched ng-dirty ng-valid-parse' and #id='selectcustTxt']").SendKeys ("1111")
Reference
You can find a couple of relevant discussions in:
How to send text to some HTML elements?
Need help to fill number into Chrome input box with 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"> &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.
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()
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'