selenium webdriver xpath search by index - java

I am trying to identify a xpath of a textfield where the class name is "DeleteBackwardTextField" and Origin class is "UITextField". There are no label, name and text it. No other information to identify it.
There are 2 textfield with same above properties.
So my following xpath identifies two text fields.
By.xpath("//textfield[#class='DeleteBackwardTextField']")
How can I identify the xpath of 1 particular field. I assume index can be used. Can anybody provide a syntax for indexing.
Appreciate your help.

You can go with XPATH or CSS. The trick is to have a list of WebElements identified and then you can iterate over it.
List<WebElement> myList = driver.findElements(by.cssSelector(...); //or XPATH;
for (WebElement txtField : myList) {
// do what you need here, for each element at a time
}

Use this xpath.
(//textfield[#class='DeleteBackwardTextField'])[index]
EX: (//textfield[#class='DeleteBackwardTextField'])[1] -> first element when you have many elements matching //textfield[#class='DeleteBackwardTextField']
Note that this xpth - //textfield[#class='DeleteBackwardTextField'][2] has different meaning - it means second child of its parent.

Related

Is it possible to find common xpath for this elements?

xpath of one element is: //div[#class=name-slider-header']//button//img[2]
xpath of another element is: //div[#class=name-slider-header']//button//img[1]
Actually I need to check attribute of element must contains "red" after element gets disabled
after clicking "n" times, so I am using element.getAttribute("src").contains("red");
element2.getAttribute("src").contains("red");
Is it possible to find common xpath for this elements?
Use the following xpath to identify the image elements where src value contains red
//div[#class='name-slider-header']//button//img[contains(#src, 'red')]
code:
imgElements = driver.findElements(By.xpath("//div[#class='name-slider-header']//button//img[contains(#src, 'red')]"));
The common XPath of these 2 elements is //div[#class=name-slider-header']//button//img. So, you can get a list of elements and then iterate over the list extracting the element attribute, as following:
elements = driver.findElements(By.xpath("//div[#class=name-slider-header']//button//img"));
for(WebElement element : elements){
if(element.getAttribute("src").contains("red")){
// do something
}
}

select any element that has text in webdriver

I'm looking to implement a general localisation test using webdriver and my idea is:
Select all elements that contain text
Use a java language identification library to verify that the text is in some specific language
I looked up various ways to select elements using text but everything documented seems to show ways of locating using specific text using contains() or text()
I thought the following would work:
//*[contains(text(), '')]
but that selects everything whether it has text or not. It also selects elements in the header. I want to select all visible text on the page, extract that text and pass it through the language identification library element by element.
You can use this XPath
//*[text() != ""]
This will give you all the elements with non-empty text.
So that
List<WebElement> list = driver.findElements(By.xpath("//*[text() != '']"));
will give you a list of all web elements on the page containing texts.
UPD
If you wish to get only elements containing texts themselves, you can exclude the elements containing texts in their children only by this code:
List<WebElement> real = new ArrayList<>();
for(WebElement element : list){
js = (JavascriptExecutor)driver;
String text = js.executeScript("""
return jQuery(arguments[0]).contents().filter(function() {
return this.nodeType == Node.TEXT_NODE;
}).text();
""", element);
if(text.length()>0){
real.add(element);
}
The final list of elements will be in real list.
The idea is from here. Translated from Python to Java according to this syntax

Trying to put a list of ids with a common phrase into an array/list in selenium java

I need to get the IDs present in the DOM that start with "starx-" into an array. After that hyphen, there is usually some combination of uppercase, lowercase, and digits - it depends upon the dropdown selected previously. But starx- is common to all ids, no matter what is selected in the previous dropdown (and there are a LOT of options so... no to creating a bunch of "if" statements based on the previous drop down)
To do so, I created this line here to match all of the id's in the DOM that start with starx-:
List<WebElement> allStarX = driver.findElements(By.cssSelector("[id^='starx-']"));
So it's good at finding them. A few print statements I threw in there show me that if there are 4 such IDs in the DOM, it will find them all. If there are 7, it will have 7 elements in that List. Unfortunately , this List doesn't contain the actual IDs. It contains:
[[ChromeDriver: chrome on WIN8_1 (f665490daee44e1039265763f67008cc)] -> css selector: [id^='starx-']]
for each id beginning with starx- that exists in the DOM. Ideally I'd get all the ID using the .getAttribute method, but that's for strings. For example, if I add it onto the end of this:
List<WebElement> allStarX = driver.findElements(By.cssSelector("[id^='starx-']")).getAttribute();
I have a type mismatch. And if I do this:
for (WebElement starx : allStarX)
{
starx.getAttribute("id");
System.out.println(starx);
}
It still prints out a bunch of this:
[[ChromeDriver: chrome on WIN8_1 (f665490daee44e1039265763f67008cc)] -> css selector: [id^='starx-']]
I kind of know why that last one doesn't work, but I don't understand why the first one doesn't.
Absolute xpaths don't seem to work because there are a lot of previous options add and remove things from the DOM.
Can anyone suggest something that might help?
As far as my understanding goes, you are trying to get the IDs which start with the text of 'starx-'. You have created a list of webelements (List<WebElement>) and while extracting the text, you are looping it by not extracting the attribute of the id, which is the issue.
You can handle it as mentioned below:
List<WebElement> allStarX = driver.findElements(By.cssSelector("[id^='starx-']"));
for (WebElement starx : allStarX)
{
System.out.println(starx.getAttribute("id"));
}
Hope this helps.

How to derive colour of text in element using Selenium?

Is it possible to derive (i.e. by automatically applying all inherited CSS styles and, if it is possible - JS scripts as well) colour of the text in given WebElement?
For instance, I want to detect colour of text in each visible element:
WebElement body = driver.findElement(By.tagName("body"));
List<WebElement> elements = body.findElements(By.cssSelector("*"));
for (WebElement we : elements) {
if (we.isDisplayed()) {
// ... colour of text in "we" element?
}
}
Is it possible to do?
P.S. I realise that each we element can contain nested elements and colour inside them can differ. But let's simplify the issue and consider each we as element without nested tags.
You can use getCssValue() something like below:
element.getCssValue("color")
which in your case will be
we.getCssValue("color")
For reference - http://www.seleniumeasy.com/selenium-tutorials/how-to-get-css-values-using-webdriver

In Jsoup, is it possible get the Elements from a list of Elements without runs through it?

I'm new to Jsoup, but this appears to be a great tool. I'm trying to extract the robots metatag.
I have the following code:
Document doc = Jsoup.parse(htmlContent);
Elements metatags = doc.select("meta");
Element robots = metatags.attr("name", "robots"); // is getting the first element of the list
The last line is wrong.
I want to know if is necessary to run the list of elements to find the element that matches the attribute or there a way that extracts the element that matches the attribute from the Elements list.
Edit 1: I solved this changing to doc.select("meta[name=robots]").
Edit 2: In another words: I want to know how to get all elements in a Elements list that matches some atribute requisite.
Edit 3: I was precipitated doing this question because I had not seen the main documentation yet. Sorry.
It's possible to set the attribute and value you want to retrieve in the select() method to do a better filtering.
Change the select to: doc.select("meta[name=robots]"); and it will get all elements that has the meta tag and it have the name attribute equals robots.
Have you read the JSoup documentation? Here it is from the method you are using:
attr
public Elements attr(String attributeKey,
String attributeValue)
Set an attribute on all matched elements.
Parameters:
attributeKey - attribute key
attributeValue - attribute value
Returns:
this
It returns this. Which means it will return an Elements object. This can't be assigned to an Element object.
I also think you want to use Document.getElementsByTag(String), instead of select.

Categories