select the locator for passing values - selenium

This is the code :
<input _ngcontent-ljn-94="" accept-user-input="true"
autocomplete="off"
class="form-control autocompleteInput ng-touched ng-dirty ng-invalid"
display-property-name="name" formcontrolname="cityId" min-chars="2"
ng2-auto-complete="" path-to-data="payload.data" placeholder="Select
city *" value-property-name="id">
I have to use sendkeys to pass value in my autocomplete city field.I tried almost various ways to pass the value by using css,xpath ,but nothing is working.Sometimes it is showing "not able to focus on element" kinds of error.Someone can please provide me the solution.

Related

select radio button in selenium using python

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.

How to enter a different text in same element after clicking in Add button?

Application has ADD Button, that need to be clicked after entering Fname and Lname. Once ADD button is clicked, another Fname and Lname text fields appears. I have tried to use index, but not worked. Text is getting entered in the first text box multiple times.
<input class="mat-input-element mat-form-field-autofill-control ng-touched ng-dirty ng-valid" matinput="" type="text" autocomplete="on" name="undefined" min="undefined" max="undefined" required="" maxlength="50" id="mat-input-16" placeholder="First Name" aria-invalid="false" aria-required="true">
First time the text is getting entered in the Element.
private IWebElement Input_Auto_SomeOneInjuredLname => FindElement(By.Id("mat-input-13"));
Input_Auto_SomeOneInjuredLname.SendKeys(data.Auto_IsSomeoneInjuredLname);
Find Element by XPath as follows:
(//input[#placeholder="Last Name"])[3]
(//input[#placeholder="First
Name"])[3]
Change needs to be done :
(//input[#placeholder="First Name"])[_ADD_INDEX_VARIABLE]
(//input[#placeholder="Last Name"])[_ADD_INDEX_VARIABLE]
Increment the value of Index Variable through for loop or while loop
starting with value =1

Trying to fill web form with VBA, can´t address element

I am trying to fill out a web form in IE with data from a workbook but I am having trouble addressing the text box since it seems to lack a name.
The site is behind a login so no link, sorry. But the element presents itself as
<input type="text" placeholder=" Søg på brugernavn, referencenummer, eller e-mail" ng-model="filterSearch.search" ng-change="filterOnSearch()" ng-model-options="{ debounce: 500 }" class="ng-pristine ng-valid ng-empty ng-touched">
when inspecting it in chrome.
I have tried
ie.document.getelementsbyclassname("ng-pristine ng-valid ng-empty ng-touched").Value = "11"
but VBA throws me a run-time error '438': Object doesn't support this property or method.
Any suggestions?
It may sound confusing but anytime you are trying to pull an element by classname you need to use the
.item(0)
after that you would use something similar to
ie.document.getelementsbyclassname("ng-pristine ng-valid ng-empty ng-touched").item(0).Value = "11"
This is assuming that you are trying to fill out an input box that has the class above and that the element is the only 1 in its class or the first one.
In the event that it does not work it may not be the first element in that class and then you may need to do item(1) or item(2) etc.

instead of xpath what can i select from the below code

Please help me to find locator in this case as id is dynamic and it changes every time when I refresh the page.
type="text" also I will not be able to use because, for the next fields, eveything is same from the down code except, the label changes from the "first name" to the "last name" and so on.
So I should be selecting something in terms of the first name which is mentioned in the label tag below.
Please anyone can help me in this case.
<input class="md-input-element ng-valid ng-dirty ng-touched" id="md-
input-4-input" spellcheck="false" type="text">
<!--template bindings={}-->
<label class="md-input-placeholder md-float md-empty" for="md-input-
4-input">First name * <!--template bindings={}--></label>
You can use preceding-sibling or following-sibling feature from xpath. As per code mentioned above your xpath should be like this:
//input[#type='text'][following-sibling::label[contains(text(),'First name')]]
But I think in html label should be first then input, if that is case please try below one:
//input[#type='text'][preceding-sibling::label[contains(text(),'First name')]]
you can get the css selector from the class (can't do by className if it has spaces in the class name so use css, put a "." at the beginning and replace any spaces with a ".")
#FindBy(css = ".md-input-element.ng-valid.ng-dirty.ng-touched")
private WebElement elementMDInputTouched;
#FindBy(css = ".md-input-placeholder.md-float.md-empty")
private WebElement elementMDInputEmpty;

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