Unable to locate element by name - selenium

Can someone please explain to me why Selenium webdriver can't be find by name=userid?
<input class="required" type="text" size="15" maxlength="64" **name="userid"**
value="" style="background-color: rgb(255, 255, 255);">
My code:
driver.findElement(By.name("userid")).sendKeys(prop.getProperty("userName"));
Error:
Unable to locate element: {"method":"name","selector":"userid"}

Yes, in my application there are frames involved and I didn't include switching to main as elements are in main frame.
To resolve this, I have simply add "driver.switchTo().frame("main");" before searching the element.
My final code are as below
driver.switchTo().frame("main");
driver.findElement(By.name("userid")).sendKeys(userName);
Hope my answer helps for those who are same on new (Webdriver boat) :)

try below line of code and let me know:
driver.findElement(By.xpath("//input[contains(#name,'userid')]")).sendKeys(prop.getProperty("userName"));

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

Invalid xpath expression to find a slider element

Very new to Katalon Studio and selenium. Trying to write an automated test using selenium webdriver to change the value of the slider on the webpage.
I am failing to locate the element. Somehting is wrong with my findelement statement. Also once I locate the slider element successfully I don't know how to change the value of the slider. Can you help? or provide some quidance?
<div class="bdr-slider"
<span id="bdrText">bdr Amount</span>
<input min="0" max="15" step"0.5" value="5" type="range">
</div>
WebElement Slider = driver.findElement(By.xpath("*[div(#class,'bdr- slider')]"))
Try this :
WebElement Slider = driver.findElement(By.xpath("//div[#class='bdr-slider']"))
I'm not familiar with specifics of mobile automation, but I think that you need to locate the input element.
This one
<input min="0" max="15" step"0.5" value="5" type="range">
and then maybe call sendKeys method on that element.
You can try this line of code:
driver.findElement(By.xpath("//div[#class='bdr-slider']/input")).sendKeys("10");
Before proceeding, make sure the element is really present in the DOM, and thus making sure the problem is not with the path, enter $x('//div[#class="bdr-slider"]/input') to console in DevTools and see if you can locate the element.
If you can find the element, since you are using Katalon Studio, you can do this:
TestObject slider = new TestObject().addProperty('css', ConditionType.EQUALS, '.bdr-slider input')
or, if you prefer xpath:
TestObject slider = new TestObject().addProperty('xpath', ConditionType.EQUALS, "//div[#class='bdr-slider']/input")
You will need to import these two:
import com.kms.katalon.core.testobject.ConditionType
import com.kms.katalon.core.testobject.TestObject as TestObject

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.

How to write xpath in selenium webdriver for below HTML expressions?

I wrote xpath for below HTML code i.e. displayed below
1. //a[#text()='Life Insurance']
2. //span[#text()='Apply now']
But I got element not found exception. If I used Absolute xpath processor then It's working and I wrote own xpath then it thrown exception.
Please tell me how to write it.
Below are the HTML code for which I need xpath.
1.<a class="mainlink" href="https://leads.hdfcbank.com/applications/webforms/apply/HDFC_Life_Click2Protect/index.aspx?promocode=P4_hp_AppNow_LI" target="" rel="nofollow width=375 height=213">Life Insurance</a>
2." <div class="menutext"> <span class="mainlink">Apply now</span> <img class="pointer" alt="Pointer" src="/assets/images/nav_pointer.png" style="display: none;"> </div> "
Try these
For 1
//a[text()='Life Insurance']
For 2
//span[text()='Apply now']
You have to remove '#' in your code.
(or)
You can also use:
//a[contains(text(),'Life Insurance')]
For 2
//span[contains(text(),'Apply now')]