Unable to create an appium-android xpath using following-sibling and child - selenium

I want to create a xpath for UNLIMITED DATA with the help of //android.widget.TextView[#text='Active'] xapth.
I have tried to locate the element using the below XPath using following-sibling and child but it didn’t work.
//android.widget.TextView[#text='Active']/following-sibling::androidx.recyclerview.widget.RecyclerView/child::androidx.cardview.widget.CardView/child::android.widget.TextView[contains(#text,'UNLIMITED Data')]
Please find the below image for your reference.
enter image description here
enter image description here
Please help.

Can you try this XPath :
//android.widget.TextView[#text='Active']/following-sibling::androidx.recyclerview.widget.RecyclerView[1]//android.widget.TextView[contains(#text,'UNLIMITED Data')]

Related

Xpath finder for selenium using python -automation

I am trying to find an unique xpath for the below element, please advice if there are any better xpaths to make it for general text as I have currently given for that specific name.
<td tabindex="4" style="text-align: left;" title="name" class="">Name</td>
xpath i am using: //td[#title='name']
here if the name is changed with something else in the code, this wouldn't work, could someone help me identify unique xpath which works in general for any text. Thanks!
You can concatenate (using and / or) multiple attributes of element to find the element precisely .
By.xpath("//td[#title= 'name' and contains(text(), 'Name')]")
However we need to see more details of the code and your DOM of page to find element.
There will always be some element which will never change in the page(like name of table) using that as a relative point ,we can refer to the row of the table.
the simplest way to find the XPath of any element is to go to the developer options and select the markup of the element you want XPath of.
Right Click -> Copy -> XPath
I believe this is the simplest way. And you will also where you are doing wrong.
Screenshot attached for your reference.
I have used the general syntax - "//td[text()='{}']" and passing the name parameter when i define a method so that it won't be specific to one and others can test using the same locator with their name changed when someone else uses the testcase.
Thanks everyone for your response!

Selenium FindElement by parent div

Trying to find link element of "a href". Snippet code:
<div id="contact-link">
Contact us
</div>
I managed doing it by:
Driver.FindElement(By.XPath("//*[#title='Contact Us']")).Click();
2.Driver.FindElement(By.XPath("//a[#href='http://automationpractice.com/index.php?controller=contact']")).Click();
3.Driver.FindElement(By.XPath("//*[text()='Contact us']")).Click();
Could someone tell me how can I get by firstly getting parent div and then find what's inside that div (by going from the top to the bottom)
So basically, with xpath, you are looking to replicate the HTML structure. What you need is:
//div[#id='contact-link']/a
This is going to return the a href under the div. Assuming its just 1, thats the way to go. If you want to go a little further, try:
//div[#id='contact-link']/a[#title='Contact Us']
Although you have already accepted the answer , I would like to highlight some point about Xpath and cssSelector. You should always pick cssSelector over Xpath :
Here is cssSelector for your requirement:
div[id='contact-link']>a
Code :
Driver.FindElement(By.CssSelector("div[id='contact-link']>a")).Click();
For more about cssSelector : https://www.w3schools.com/cssref/css_selectors.asp
For Difference between Xpath and cssSelector, you can read it from this SO post: Diff between Xpath and cssSelector

Unable to locate an element using text attribute in XPath

I am trying to locate element using relative XPath. I have attached HTML schema of the element. Below is the XPath I am using:
//a[contains(text(),"Sales")].
If you want to locate link by text you might need to use search by link text instead of XPath:
Java:
driver.findElement(By.linkText("Sales")).click();
Python:
driver.find_element_by_link_text("Sales").click()
Note that you should use exact value as it appears on rendered page in browser:
if it appears as SALES:
driver.find_element_by_link_text("SALES")
if it appears as "Sales":
driver.find_element_by_link_text("\"Sales\"")
In case some extra text is added by ::before pseudo-element, you can also use search by partial link text:
driver.find_element_by_partial_link_text("Sales")
//*[#class='**your class name**']//*[text()='Sales']
Hope above XPath helps you!

how can i handle dynamically changing ids on my webpage using selenium?

Here is my code
<mat-error class="mat-error" role="alert" id="mat-error-0"> </mat-error>
<mat-error class="mat-error" role="alert" id="mat-error-1"> </mat-error>
Here mat-error-0 will change to mat-error-1 on page refresh and mat-error-1 will change to mat-error-2 and so on..
There is no unique class or role attribute here that i can take here.
Please advise.
Use the following xpath
//mat-error[text()='Please enter a valid To date']
//mat-error[text()='Please enter a valid From date']
As long as innerHTML stays consistent after refreshing the page, you can use Xpath to find the element. e.g. the following Xpath can locate To date webElement.
//mat-error[contains(.,'valid To date')]
Use Following Xpath for dynamically changing id.
//mat-error[starts-with(#id,'mat-error')]
As per the HTML you have shared, as the id keeps changing so you have to construct dynamic xpath to identify both the elements as follows :
Element with text as Please enter a valid To date :
//mat-error[#class='mat-error' and starts-with(#id,'mat-error') and contains(.,'To')]
Element with text as Please enter a valid From date :
//mat-error[#class='mat-error' and starts-with(#id,'mat-error') and contains(.,'From')]

Not able to find Xpath for Parent Child relation in Div tag

Hi
I am not able to find the correct xpath for the Html code added in the attachment.
I have written below xpath but it is not working.
//div[#class="calendar-item-title"][1]//*[#class="calendar-item-time"][1]
Please find attached image for more details of Html code.
div[#class="calendar-item-title"] does not have a child *[#class="calendar-item-time"] according to your image.
Your *[#class="calendar-item-time"] is in div[#class="items-container"]
so your xpath would be:
//div[#class="items-container"][1]//a[#class="calendar-item-time"][1]
Just add : "/parent::*" at the end of //div[#class="calendar-item-title"] if you want the parent element
Try this xpath :
//a[contains(text(),'12:30 am')]
try with the below xpath
driver.findElement(By.xpath("//a[text()='12:30 am']")).click()
from ur image, its not clear there are more 12.30 containing text or not. In that case, use
driver.findElements(By.xpath("//a[text()='12:30 am']")).get(0).click();