Can we use Condition with axes in XPATH? - selenium

Can we use Condition with axes in XPATH?
Trying to check both parent and child in XPath. Example:
//span[text()='Accounts']/parent::a OR child::div
I know this is wrong. But is there any way to check the condition?
I know the situation looks vague. But I am trying to find a logic where I can use both parent and child axes to create an XPath.
The main motive is to inspect an element from UI and create XPath using axes. and automate the process.
When I am trying the above XPath, it throws some error.

Maybe if you use | instead of OR
//span[text()='Accounts']/(parent::a|child::div)

Related

Identifying an element from a group, span[i] is the differentiating factor

I have added the screenshot I have a group of elements that have the exact same xpath except the span tag.I want to identify the individual input fields, but unable to.
I have tried using contains, with class but unable to attach span to the xpath
Here is what the HTML looks like:
/html/body/div[#id='app']/div/div[#class='LayoutModify_LayoutModify_1Akxb']/main[#class='LayoutModify_main_5aBy3']/section[#class='sub-detail inner ProductDetail_productdetail_bJWN2']/div[#class='ProductDetail_productsphere_kgNGm']/div[#class='ProductDetail_threecol_2zA1n ProductDetail_productsphereleft_2pLZT']/span[4]/div[#class='el-input el-input--medium ProductDetail_productsphereinput_3eVZg']/input[#class='el-input__inner']
/html/body/div[#id='app']/div/div[#class='LayoutModify_LayoutModify_1Akxb']/main[#class='LayoutModify_main_5aBy3']/section[#class='sub-detail inner ProductDetail_productdetail_bJWN2']/div[#class='ProductDetail_productsphere_kgNGm']/div[#class='ProductDetail_threecol_2zA1n ProductDetail_productsphereright_3BrqC']/span[4]/div[#class='el-input el-input--medium ProductDetail_productsphereinput_3eVZg']/input[#class='el-input__inner']
Notice the span[4] and span[15] are the only differences
Quick question:
do either of these locators:
locator A
//span[4]/div/input[#class='el-input__inner']
locator B
//span[15]/div/input[#class='el-input__inner']
find any input on Your page?
If not - could You please post here the whole HTML page code here please?
Here is the xpath that worked:
//div[contains(#class,'ProductDetail_productsphereright')]/span[4]/div/input

Nested predicates in Xpath

I am trying to access the div node via
//div[#data-full='2018-1-15']
Normally I would just search by Xpath and grab this node. However the nature of the site is that there are a number of nodes with this property, and only one is clickable.
Because of this I have to grab the
//div[#class='dw-cal-slide dw-cal-slide-a']
node first and then step down. I know I'm trying to do something like this:
Step down one node:
//div[#class='dw-cal-slide dw-cal-slide-a']/div/
And then search for child nodes that have a child node of their own with the property
//div[#data-full='2018-1-15'].
Having trouble with the syntax. Any help would be great!
Try
//div[contains(#class, 'dw-cal-slide') and contains(#class, 'dw-cal-slide-a')]//div[#data-full='2018-1-15']
But IMHO it's better (shorter expression) to use CSS selector
div.dw-cal-slide.dw-cal-slide-a div[data-full='2018-1-15']
If you want to locate ancestor and descendant div in two code lines, then you can use (Python example)
ancestor = driver.find_element_by_xpath("//div[contains(#class, 'dw-cal-slide') and contains(#class, 'dw-cal-slide-a')]")
and
descendant = ancestor.find_element_by_xpath(".//div[#data-full='2018-1-15']")
As you said, there are multiple node with same property and only one is clickable. you can click on the node by checking isEnable. other thing you can try with following-sibling or preceding-sibling. example can be found on below stackoverflow link:
How to use XPath preceding-sibling correctly

How to find exact value using xpath in selenium webdriver?

I am using XPath to find exact value:
//h5[#class='familyName productFamilyName'][contains(text(),'Dozers ')]
but it was failing because in my application there are 2 elements with text values "Dozers " and "Dozers wheel" which is come under same class.
I can't use id locators,because it is dynamically generating in my application like //div[#id="482"]/div/div[1]/h5.
Please suggest me any solution.
If you want to match element with exact innerHTML value just use
//h5[#class='familyName productFamilyName'][text()='Dozers')]
or
//h5[#class='familyName productFamilyName'][text()='Dozers wheel')]
Depending on HTML structure you might need to use [.='Dozers'] or
[normalize-space(.)='Dozers'] instead of [text()='Dozers']

how to select the check box using selenium?

How to select the checkbox which has a dynamically changing ID and XPath?
Multiple ways:
You should look at a pattern like id or name somoething like
CT_CHKBox_157, CT_CHK_158 etc.. For example, to click the first
Checkbox having a pattern of Ids
You can use a dynamic xpath like driver.findelement(By.xpath(//input[starts-with(#id,'CT_CHK'][1]).click()
Identify the Unique Element which are close ancestors to the
Checkbox in question and reach out to it through xpath or css path
relatively or through indexing from within.
Hope that clarifies.
Have you tried XPath by position? Ultimately the check boxes are like buttons or link that can be clicked so driver.findElement(By.xpath("//xpath by position")).click();
Alternativey you might want to use JavaScript:
((JavascriptExecutor) driver).executeScript("return document.getElementsByName('ChkboxValue')[0].checked;");
Hope this helps.
Selenium uses what is called locators to find and match the elements.There are 8 locators strategies included in Selenium:
Identifier
Id
Name
Link
DOM
XPath
CSS
UI-element
you can try using any other Locator in the list.

How to find all WebElements on page by presence of id?

I know I can find element by id:
driver.findElement(By.xpath("//*[#id='some_id']"));
And I can find all elements with this id, but I want to find all elements with an id attribute. I'm looking for something like:
driver.findElements(By.xpath("//*[#id]"));
// or
driver.findElements(By.xpath("//*[#id='*']"));
// or
driver.findElements(By.xpath("//*[contains(#id)]"));
I'm using Java. Thanks!
UPD: The only solution for me is to get all elements by "//*", go through them and get their id attributes. Is there a way to get all attributes at once, something like "//*#A" from Java?
Attribute A of element where A contains 't'
//E[contains(#A,'t')]/#A
or
//E[contains(#A,'t')]#A
Attribute A of any element
//*/#A
or //*#A
so I use java too. So if you need to find all elements with id attribute you can simply use
driver.findElements(By.xpath(//*#id))
Hope this helps you.
ALso look here. Nice manual for xpath and css selectors
have you tried "//*[contains(#id, '')]"