Selenium no such element exception, unable to locate in selenium - 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']")

Related

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']")

Not able to identify custom checkbox element/Need relative xpath

I need relative xpath of the span element
<span class="icon"></span>
in selenium web driver.
<span class="custom-checkbox">
<input id="personalization1" name="personalization-terms-check" class="personalization-terms-check required" value="accept" type="checkbox">
<label for="personalization1">
<span class="icon"></span>
<span class="order-ready-text">yes! I double-checked all personalization entered above and i'm ready to order</span>
</label>
</span>
need relative xpath of the span element
<span class="icon">
in above html and I am getting relative path:
//*[#id="main"]/div[3]/div[4]/div[2]/div/span/label/span[1].
Please help me to get an relative xpath or another way to make an click on the span
NOTE:
<span class="icon">
It can be multiple so I need unique relative xpath.
You can wait for the element visibility before performing any action like below.
WebElement element=driver.findElement(By.xpath("//*[#name='personalization-terms-check']/following-sibling::label/span"));
WebDriverWait wait = new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.visibilityOf(element));
element.click();
and you can use different xpath or id to locate the element. the above xpath map be unique if only one personalization term checkbox on the page.
Can you try with this xpath?
"//label[#for='personalization1']/span[1]"
Hope this helps. Thanks.
Try this:
Try this XPath -
//input[#id='personalization1']/label/span
Try this xpath to click on checkbox:
.//input[#id='personalization1']
or you can use this xpath
.//span[contains(text(),'yes! I dou')]

Xpath not working as expected while running the test

I am trying to automate the browser, while I try to locate the element via xpath in browser in static mode it is able to highlight the element where as when I run the script it comes back with an error that it is unable to find the element.
xpath I have written:
driver.findElement(By.xpath("//input[#value='soa_b_pbtv_l0_trnkni']/following-sibling::td[1]/child::select[#name='jobaction']")));
Here is the HTML:
<form name="f2" onsubmit="return verify();" action="/ATS/cgi-bin/barcap_jobaction.pl" method="post">
<>
<input name="jobname" type="hidden" value="soa_b_pbtv_l0_trnkni"/>
<input name="jobinstance" type="hidden" value="D03"/>
<input name="jobproceed" type="hidden" value="web"/>
<td style="background-color: #ffffff;">
<select name="jobaction">
if you're trying to select the select, jobaction then try this:
use css selector for the select select[name='jobaction']
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("form[name='f2']")));
List<WebElement> eleList = driver.findElements(By.cssSelector("form[name='f2']")));
for(WebElement element: eleList) {
if(element.findElement(By.cssSelector("input[name='jobname']")).getText().equalsIgnoringCase("expectedValue")) {
WebElement element = element.findElement(By.cssSelector("select[name='jobaction']"));
}
}
The INPUT is hidden so it won't be found using typical Selenium means. Selenium was designed to only interact with elements that a user can see and interact with. You are able to locate it in the browser because you are using JS or JQuery and they are not designed to ignore hidden elements. One way to get around this is to use JavascriptExecutor... it basically allows you to run JS in Selenium and find hidden elements. Since it sounds like you already have successful locators, I would suggest you look up some tutorials on JSE and you should be set.
If you run into a new issue while using JSE, come back and post a new question and we can try to help you.

Find element by XPath isn't working in selenium

I'm trying to find a element in selenium with this XPATH /html/body/div[3]/div/div/div[2]/div[2]/div/div/div[1]/form/div/input.
I copied it from inspect element, and copy xpath. I saw that some persons with the same problem use "*" character but I don't know where I should to use it.
this the html code
<input type="text" data-bind="value: CorpItem.Name, valueUpdate: 'afterkeydown'"
class="form-control" placeholder="Enter ..." required="required">
Here is my code on Selenium
IWebElement corpName = driver.FindElement(By.CssSelector("/html/body/div[3]/div/div/div[2]/div[2]/div/div/div[1]/form/div/input"))
Try this XPath
//input[#type='text'][#class='form-control']
In your code you used XPath and gave selector as CSS. Please verify.
Using your XPath, your code must be
IWebElement corpName = driver.FindElement(By.XPath("/html/body/div[3]/div/div/div[2]/div[2]/div/div/div[1]/form/div/input"))
If it didn't work, try
IWebElement corpName = driver.FindElement(By.XPath("//input[#type='text'][#class='form-control']"));