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
Related
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
}
}
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
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.
Is there a way to Identify and assign WebElement Names to a list of WebElements? For example using the following convention:W
#FindBy(xpath="")
WebElement listFirstObject;
#FindBy(xpath="")
WebElement listSecondObject;
Forgive me if this is an elementary question, but it seems like it would be a pretty common scenario for any tester as many applications have lists of objects with common names. In my case I have a control list with well over 700 objects and it would be nice to be able to write some iterative method to capture and individually create each common WebElement from the list.
** I have made Updates to my question for further clarification** Taking an entire grid of info is completely new to me so please be specific with the answers as I am trying to understand the logic behind it.
So I have elements I am looking for are Grid Data, I have successfully captured the entire Grid, for example
#FindBy(id="ctl00_SomeGridData_ucControlList_trgControlList_ctl00")
List<WebElement> someGridData;
If I were to individually capture each new addition to the grid it would look as such:
#FindBy(id="ctl00_SomeGridData_ucControlList_trgControlList_ctl00__0")
WebElement someGridObj1;
#FindBy(id="ctl00_SomeGridData_ucControlList_trgControlList_ctl00__1")
WebElement someGridObj2;
#FindBy(id="ctl00_SomeGridData_ucControlList_trgControlList_ctl00__2")
WebElement someGridObj3;
As you can see each individual grid element ends with "__#" Obviously this is an infinite list and I cannot capture every WebElement individually and assign a WebElement value to use for testing. What I am trying to ask is how do I capture this entire list and then if i need to call an individual WebElement later to test how do I do so? I hope this clarifies and thanks for reading.
Ok, now that have edited your question, it's clear what you want to know. Assuming your grid data is in td elements, you can do the following.
#FindBy(css = "td[id^=ctl00_SomeGridData_ucControlList_trgControlList_ctl00__]")
List<WebElement> allGridData;
By using the ^ (which means starts with) you collect all the td elements that have an id starting with ctl00_SomeGridData_ucControlList_trgControlList_ctl00__.
Yes you can do this like below:
List<WebElement> mylist = driver.findElements(By.xpath("ur xpath"));
System.out.println("Size of the list is : " +mylist.size());
This will give you how many webelements are inside your mylist.
Now apply for loop to print the value of the mylist on console
and also in the for loop you can assign them a value like this
for(int i = 0;i<mylist.size();i++){
System.out.println("index of webelements in increasing order is : " + i + mylist.get(i).getText());
// this will assign an index of 0 to max for the weblements in the
list like index 0 = for 1 webelement ,index 1 = for 2nd webelement ,
index 2 = for 3rd webelement and so on
}
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.