Selenium web driver in nodejs: no such element Error - node.js

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]);

Related

Cheerio JS Can I check if an attribute is included in an element I already selected in cheerio JS

I am wondering if I can check if an element has a certain attribute after locating it, and if it has that attribute, avoid it and get a new one.
Here is the html I am using:
<option
value="19438389624960">
1
</option>
<option
disabled="disabled"
value="19438389657728">
2
</option>
<option
value="19438389690496">
3
</option>
<option
disabled="disabled"
value="19438389723264">
4
</option>
As you can see, 2 of the 4 options have a disabled="disabled" attribute that I want to avoid, is there anyway I can do that ?
I currently have this code that chooses a random value out of all the options:
var list = [];
$('select[name=id]').find('option').each(function (index, element) {
list.push($(element).attr('value'));
});
const randomVar = list[Math.random() * list.length | 0]
variant = randomVar
But this code still will choose the ones that have the disabled="disabled" attr
You can use this:
$('select[name=id]').find('option[disabled!="disabled"]').each(...)
to skip the options that have disabled=disabled. Then, you will be picking a random one from only the non-disabled items. Since cheerio uses the same selector engine as jQuery, you can find this kind of stuff in the relevant page of the jQuery doc.
:not is a regular css pseudo, which means it works everywhere:
$('option:not([disabled="disabled"])')
or probably just:
$('option:not([disabled])')

Trying to select item from dropdown box

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

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.

How to select a value from a drop-down using Selenium and Python

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)

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