How can I access a parent element using selenium? - selenium

How can I access the parent of an element using Selenium?
For instance, I already have an inner element defined by class="angle". How to get an outer a element?
<i class="angle"></i>

You should try using xpath locator as below :-
using parent axes :
.//*[#class='angle']/parent::a
Or
.//*[#class='angle']/..
using child axes :
.//a[child::*[#class='angle']]

Related

How to access 2nd element with same class name using css selectors

I want to access the 2nd element with same class name using css selectors.
1st element:
<a class="good">
2nd element:
<a class="good">
Css selector I am using :
a.good
but this accessing both of them.
How to access the 2nd one or anyone individually?
you can use pseudo selector, pseudo selector matches elements based on their position among a group of siblings.
check example link below
click here

Choose the correct element from the list of objects with the same className

Quick one, i am trying to avoid using xpath and using css selectors due to performance issues xpath can have so i would like to know the right approach of locating for example "A" in the list
<div class="input-search-suggests" xpath="1">
<div class="input-search-suggests-item">A</div>
<div class="input-search-suggests-item">B</div>
<div class="input-search-suggests-item">C</div>
</div>
Currently i am locating A using xpath / span but it would be indeed sufficient locating all elements and then grabbing A from the list that have same class which is "input-search-suggests-item"
#FindBy(xpath = "//span[contains(text(),'A')]")
CSS_SELECTOR does not have support for direct text what xpath has.
What this means is, for the below xpath
xpath = "//span[contains(text(),'A')]"
based on text A you can not write a css selector.
Instead to locate A using css selector, you can do :
div.input-search-suggests > div.input-search-suggests-item
In Selenium something like this :
#FindBy(cssSelector= "div.input-search-suggests > div.input-search-suggests-item")
Even though it will have 3 matching nodes, but findElement will take the first web element.
Also you may wanna look at nth-child(n)
div.input-search-suggests > nth-child(1)
to make use of index to locate A, B, C
Here is the Reference Link

XPath : Pass attribute value down the path

I am wondering if below is achievable using xpath
Given:
<label for="pt1:sc">Select Country</label>
<select id="pt1:sc">....</select>
Requirement:
I want to find select element using single xpath expression like below,
bcs ids are dynamic and always available in attribute 'for'.
//label[text()='Select Country']/#for//*[#id=#for]
Can we pass attribute value(here for attribute of label) in xpath, further down the path to find element.
Please do not suggest alternative using siblings, child, id or selenium get-attribute etc.
Thanks,
You can use something like this to select an element with an attribute value which refers to another attribute located in another element :
//*[#id=//label[text()='Select Country']/#for]
I'm not sure how it's going to work with your actual html, but it works on the example in the question:
//label[text()='Select Country'][#for=//select/#id]

How to handle multiple xpath for same locator using Selenium?

How to handle multiple xpath for same locator using Selenium, i.e if one is failed use another locator for same field before failing script.
To start with each WebElement within the DOM Tree can be uniquely identified using any of the available Locator Strategies.
However, you can construct multiple xpath for the same element using permutation and combination of the available attributes and their values. As an example, for the element below:
<div class="_2S1VP copyable-text selectable-text" data-tab="1" dir="ltr" spellcheck="true" contenteditable="true"></div>
You can construct multiple xpaths as follows:
"//div[contains(#class, 'copyable-text')]"
"//div[contains(#class, 'copyable-text') and #data-tab='1']"
"//div[contains(#class, 'copyable-text') and #data-tab='1'][#dir='ltr']
"//div[contains(#class, 'copyable-text') and #data-tab='1'][#dir='ltr' and #spellcheck='true']"
"//div[contains(#class, 'copyable-text') and #data-tab='1'][#contenteditable='true']"
All these xpaths would identify the same element. But what matters most is the xpath should be able to identify the desired element uniquely. The responsibility of constructing the optimized xpath is solely on the test creator.
Use OR expression for the same. You can pass multiple attribute of the same WebElement.
For example:
Xpath=//*[#type='submit' or #name='btnReset']

find specific element for button using selenium for web scraping

I am inspecting one button element from a web page using chrome driver and selenium. And the html code for the particular button is:
<div class="label text-left text-link link-blue text-
uppercase">Financial Statement Analysis <span class="count">(2)</span>
</div>
I have tried different element options like find element by name, xpath, link text etc. But none of them unable to locate the element.
What will be the element to locate the button. ?
try Xpath :
//span[contains(#class,'count') and text() = '(2)']
You can try with this css selector :
div.label.text-left.text-link.link-blue.text-.uppercase
To locate the element with text as Financial Statement Analysis (2) you can use the following solution:
Java Solution:
WebElement elem = driver.findElement(By.xpath("//div[#class='label text-left text-link link-blue text-uppercase'][contains(.,'Financial Statement Analysis')]"));