How to locate the first element in selenium - selenium

I have a question,
I have this xpath that return 9 results
//nz-select[(#formcontrolname='deviceType')]
I want to locate the first element, so I tried in the xpath the following
//nz-select[(#formcontrolname='deviceType')][1]
//nz-select[1][(#formcontrolname='deviceType')]
however in the F12 console it always 9 results. how can I locate the [1] results, without putting all in list and other issues.
Just want hard coded the results of the first one
regards

To get the first item Try this xpath.
(//nz-select[#formcontrolname='deviceType'])[1]

Please try below code:
(.//nz-select[(#formcontrolname='deviceType')])[1]

Related

How to find the previous element of an element found in Appium Xpath?

PROBLEM:
I am having an issue to identify an element in Appium.
As I can't identify the element, I managed to identify by xpath the next element. Let's call it "FOUND" element.
SO now I try to get the previous element from this element "FOUND".
DETAILS:
In this screenshot above, you can see the elements I am speaking about.
To find the "FOUND" element, I am looping in all the element with the class "android.widget.TextView", I extract the attribute 'text' and compate it to the string 'Website'.
Then from the element FOUND, I try to find the element I need. I tried so many various expression, but I didn't succeed to get it. I use a "try, except" to try to cath it, but without success.
here is the code:
elements_of_profile_detail_page = driver.find_elements_by_class_name("android.widget.TextView")
list_xpath=[
"preceding-sibling::android.widget.TextView[1]",
"preceding-sibling::android.widget.TextView[1]",
"(/preceding-sibling::android.widget.TextView)[1]",
"/*preceding-sibling::android.widget.TextView[1]",
"(/*preceding-sibling::android.widget.TextView)[1]",
"(preceding-sibling::android.widget.TextView)[1]",
"../android.widget.TextView[0]",
"preceding-sibling::*[1]",
"/preceding-sibling::android.widget.TextView",
"preceding-sibling::android.widget.TextView",
"(preceding-sibling::android.widget.TextView)[1]"
]
i=0
while i<len(list_xpath):
try:
website = element_of_profile_detail_page.find_element_by_xpath(list_xpath[i]).get_attribute('text')
print(f"website : {website}")
print(f"xpath : {xpath}")
break
except:
print("It didn't work!")
i+=1
And here is another screenshot with more details of the element I need:
I am using Appium 1.15.1 and Python 3.7. I don't think it is important as it is a matter of Xpath.
I hope I gave enough details to find the solution. I am working on it since very early this morning.
You can directly find the element by using its text in the xpath like:
element = driver.find_element_by_xpath("//*[contains(#text,'AlfangeAcademy')]")

how to locate specific node for Xpath on index basis?

I do need to locate second node, on basic of same class available in the Table.
There are six results found on webpage, for query : //td[#class='checkboxCollumn']
If I locate it for second node, By using //td[#class='checkboxCollumn'][2] its giving 0 result.
I have tried some other variations : //td[#class='checkboxCollumn']//i[2] and so on its giving 0 result.
What is the correct way to locate it with index ?
You need to locate like this
(//td[#class='checkboxCollumn'])[2]
Try:
(//td[#class='checkboxCollumn'])[2]
With this:
//td[#class='checkboxCollumn'][2] you trying to match second element within the same container
Use this.. to locate second node...
(//td[#class='checkboxCollumn'])[position()=2]
Here position() is 1-indexed i.e. position will start counting from 1
try using the
(//td[#class='checkboxCollumn'])[2]
as [] has higher priority over //.So rememeber to put the expression is brackets when need to specify the exact node of the selected node-list.
In ur case,it will search for all the elements in the document,that are at the second place.
Even though above thing not work,let me know.

Find specific cell of a celltable with Selenium xpath

I have a celltable , where i have multiple rows and is a dynamic table,
Screenshot attached ..
What i want is to find the row where column contains useren#lim.mobi
and then click its checkBox.
I am trying with xpath but not much experience here , If i can get some help please
Thanks
Here is the html code of the specific cell
I don't know the exact html of your table to form an xpath would be difficult.
however it should look something like this
//*[contains(text(),'useren#lim.mobi ')]/../td[2]
for following table, if I have to find the corrosponding contact for some company, This is how I would do it.
https://www.w3schools.com/html/html_tables.asp
//*[contains(text(),'Magazzini Alimentari Riuniti')]/../td[2]
Please try with the following xpath:
//tr[./td[contains(text(),'useren#lim.mobi')]/td[2]/input
if above xpath is not working please attach html code screenshot so i can tell you the extact xpath.
This might help to solve your issue:
//td[#title="useren#lim.mobi"]/following::input[#type="checkbox"]
If you get NoSuchElementException with this XPath you might need to add ExplicitWait as below:
WebElementWait wait = new WebElementWait(getWebDriver, 10);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//td[#title='useren#lim.mobi']/following::input[#type='chec‌​k‌​b‌​ox']")));

Not able to identify a lenghty linktext in selenium webdriver

I want to identify an element by linktext, I am facing strange issue
If the linktext value is short i.e addFirst, addLast will be able to locate the element by using
driver.findelement(By.linktext("addLast, addFirst")).click
IF the linktext is addmanualreferraltocheckthelenghtF, addmanualreferraltocheckthelenghtF lengthy as above not able to identify the element
Please help me to find the solution
Why don't you try partial link text.
driver.findelement(By.partialLinkText("addLast, addFirst")).click
try out contains method. use the XPATH value and in XPATH use contains for link text.
EG:
.//*[contains(#Linktext, "addmanualreferraltocheckthelenghtF")]
driver.findelement(By.xpath("//*[contains(#Linktext, "addmanualreferraltocheckthelenghtF")]").click
Alternatively you can also use starts with XPATH value.
.//*[starts-with(#linktext,'addmanual')]
driver.findelement(By.xpath("
//*[starts-with(#linktext,'addmanual')]").click
Hope it helps you.

Webdriver click second element in list

The page I'm testing has 2 elements with the same name and I need to click the second Element.
I can get the elements by using:
driver.findElements(By.linkText("Services"));
But I don't know how to click on the second element.
There are two ways to do this:
1) Using xpath, try in following manner.
driver.findElement(By.xpath("('xpath of the link')[2]"));//If you had given html, I could have added exact xpath.
2) Using findElements() you can try following:
List<WebElement> li = driver.findElements(By.linkText("Services"));;
li.get(1).click();//If there are only two such element, here 1 is index of 2nd element in list returned.
Hope you get the idea. :)
Or for short you can do
driver.FindElements(By.ClassName("drop"))[1].Click();
If you`re using python, you can try this way:
li = driver.findElements(By.linkText("Services"))[index].click()