select radio button in selenium using python - selenium

I tried
driver.find_element(By.XPATH, "//input[#value='option1']").click
But Error message generated
Note:
value attributes is not supported by find_element
ID is not available as an attribute
Below is the HTML code
label _ngcontent-c12="" class="custom-control custom-radio"
input _ngcontent-c12="" class="custom-control-input input-md input-rectangle ng-dirty ng-valid ng-touched" formcontrolname="radioBtns" name="radioBtns" nbinput="" type="radio" value="option1"
span _ngcontent-c12="" class="custom-control-description">

You'd better use find_element_by_css_selector function.
driver.find_element_by_css_selector("input[value='option1']").click()
Hope it could help.

Related

Selenium no such element exception, unable to locate in selenium

I'm unable to use selenium in this code.By ID,Name or xpath using both.PLease tell me way to do so. how to use this in selenium . getting this error:-OpenQA.Selenium.NoSuchElementException
HResult=0x80131500
Message=no such element: Unable to locate element: {"method":"css selector","selector":"*[name =""]"}
<input id="sys_display.incident.location" name="sys_display.incident.location" aria-labelledby="label.incident.location" type="search" autocomplete="off" autocorrect="off" value="" ac_columns="u_location_code" ac_order_by="u_location_code" data-type="ac_reference_input" data-completer="AJAXTableCompleter" data-dependent="" data-dependent-value="" data-ref-qual="" data-ref="incident.location" data-ref-key="null" data-ref-dynamic="false" data-name="location" data-table="cmn_location" class="form-control element_reference_input" style="; width:240px;" spellcheck="false" onfocus="if (!this.ac) addLoadEvent(function() {var e = gel('sys_display.incident.location'); if (!e.ac) new AJAXTableCompleter(gel('sys_display.incident.location'), 'incident.location', '', ''); e.ac.onFocus();})" aria-required="true" role="combobox" aria-autocomplete="list" aria-owns="AC.incident.location" aria-expanded="false" title="" aria-invalid="false">```
It is difficult to assess without actually inspecting the DOM, but since you mentioned that the website cannot be shared; with the given input in the query, here are some of the possibilities:
Please note that these may work or may not, as I have no way to test them. They are just built using your line in the query.
There could be another possibility that your component maybe sitting in an iframe. Please check that also. If it is, then you have to first switch to iframe, and then traverse this element.
driver.find_element(By.ID, "sys_display.incident.location")
driver.find_element(By.NAME, "sys_display.incident.location")
driver.find_element(By.XPATH, "//input[#aria-labelledby='label.incident.location']")
driver.find_element(By.CSS_SELECTOR, "input[aria-labelledby='label.incident.location']")
driver.find_element(By.XPATH, "//input[#type='Search']")
driver.find_element(By.CSS_SELECTOR, "input[type='search']")
driver.find_element(By.TAG_NAME, "input")
driver.find_element(By.XPATH, "//input[#ac_columns='u_location_code']")
driver.find_element(By.XPATH, "//input[#role='combobox']")
driver.find_element(By.CSS_SELECTOR, "input[role='combobox']")

How to write xpath which has ::before and ::after (selenium)

Line 1 ->
<input _ngcontent-huh-c120="" name="options" id="line" type="radio"
ng-reflect-name="options" ng-reflect-model="0" ng-reflect-value="0" class="ng-untouched ng-valid ng-dirty" xpath="1">
Line 2-> ::before
Line 3-> ::after
I am trying to click on a radio button called line. I have 3 lines in html line 1,2 & 3. When I hover over all 3 lines all of them has exact same xpath
so its getting confuse on which xpath to select.
I tried //*[#id='line'] but its not working.
You can differentiate using xpath indexing, like below :
for first this should work.
(//*[#id='line'])[1]
for second this should work.
(//*[#id='line'])[2]
and so on..

Can anyboby help find me xpath for below ? i tried with //input[#class='new-todo ng-pristine ng-valid ng-touched'] but its not able to find it

https://todomvc.com/examples/angular2/ this is the website. and I'm trying to locate text box.
I tried with Xpath driver.findElement(By.xpath("//input[#class='new-todo ng-pristine ng-valid ng-touched']")).sendKeys("Go to GYM");
Also with driver.findElement(By.className("new-todo ng-pristine ng-valid ng-touched")).sendKeys("Blah");
But its still not able to locate element , can someone please help
//input xpath is enough
or //input[contains(#class,'new-todo')] xpath
or new-todo by class name
or .new-todo css selector
Don't forget to set some delay before accessing the element to let the page and the element completely loaded.

How to locate the input tag as per the HTML provided through Selenium?

Please what locator do i use for the below. I have tried Xpath and CSS Selector but no luck.
<input type="password"
class="input-block-level ng-dirty ng-valid ng-valid-required"
placeholder="Password" ng-model="password" ng-trim="false"
required="" ng-disabled="isLogging"
ng-hide="changePassword" autocomplete="off">
As per the HTML you can use either of the following solution:
CSS_SELECTOR:
"input.input-block-level.ng-dirty.ng-valid.ng-valid-required[ng-model='password']"
XPATH:
"//input[#class='input-block-level ng-dirty ng-valid ng-valid-required' and #ng-model='password']"
Note: The element is an Angular element, ensure that you interact with the element inducing WebDriverwait.
For style class ng-dirty ng-valid ng-valid-required are inserted automatically by angular compiler after angular complete compile the source code. So your locator should not rely on these style class.
1) Using Java as script language
driver.findElement(By.cssSelector("input[placeholder='Password']"))
// or
driver.findElement(By.xpath("//input[#placeholder='Password']"))
2) Using python as script language
dirver.find_element_by_css_selector("input[placeholder='Password']")
// or
driver.find_element_by_xpath("//input[#placeholder='Password']")

How to 'click' a radio button -> xPath needed

I need to check one of three radio buttons. I tried this code:
//input[#type='radio']/following-sibling::*[contains(., 'Inne akcje')]
but I think it's wrong way.
<label class="HoldersInLineLabel">Rodzaj akcji</label>
<input type="radio" ng-model="holdersModel.OperationType" class="prettifiedIeCheckbox ng-valid ng-dirty" value="P" name="01H">
"Przekazanie"
<input type="radio" ng-model="holdersModel.OperationType" value="D" class="prettifiedIeCheckbox ng-valid ng-dirty" name="01I">
"Dekretacja"
<input type="radio" ng-model="holdersModel.OperationType" value="O" class="prettifiedIeCheckbox ng-valid ng-dirty" name="01J">
"Inne akcje"
<span class="k-widget k-dropdown k-header ng-pristine ng-valid" unselectable="on" role="listbox" aria-haspopup="true" aria-expanded="false"...></span>
The following XPath expression will get just the input element you want:
//input[#type='radio'][following-sibling::text()[position()=1][contains(., 'Inne akcje')]]
That returns this element:
<input type="radio" ng-model="holdersModel.OperationType" value="O"
class="prettifiedIeCheckbox ng-valid ng-dirty" name="01J" />
The key differences from your original XPath expression are:
Don’t use the syntax input[#type='radio']/following-sibling…; instead use //input[#type='radio'][following-sibling….
Don’t use following-sibling::*; instead use following-sibling::text() (because in this context * means “any element“; so if you want that text node instead, you have to explicitly indicate it by using text() instead)
Do use [position()=1] in following-sibling::*[position()=1] to indicate that you want the first following sibling.
Xpath seems to get more complicated and would be hard to maintain in long term in this case. Names seem to be unique for these radio buttons. Are they not static? If they are, then you can just use name as selector. If you really want to use xpath, then try something concise like
.//input[contains(#name, '01J') and text() = 'Inne akcje']
I agree with nilesh about XPath. Here's how I would do this using CSS Selectors.
WebElement przekazanie = driver.findElement(By.cssSelector("input[value='P']"));
WebElement dekretacja = driver.findElement(By.cssSelector("input[value='D']"));
WebElement inneAkcje = driver.findElement(By.cssSelector("input[value='O']"));
// pick the one you want to click and .click() it
przekazanie.click();