How to write XPath based on elements in Chrome Dev Tools? - selenium

I am trying to get XPaths for underlined values in the image below. I have a list of cards which is always different, and I need to grab values from the inside of the highlighted card which has 'highlight' in its class.
When I grab the XPath through inspecting an element in Chrome, I get something like this //*[#id="rental-asset"]/div/div[1]/div/rdo-nav-card-asset[11]/div/div[1]/div/i which doesn't work for me since that 11 in brackets is always going to be a different value. How can I create an XPath for these elements based on their unique class?

Here is how to get the first underlined thing. I think you should be able to take it from here.
//rdo-nav-card-asset/div[contains(#class,'panel') and contains(#class,'highlight')]/div[contains(#class,'panel-heading')]/div[contains(#class,'panel-title')]

You can Split the xpath and iterate in the range they are expected to occur.
Suppose they have that value from 1-22 you can something like this.
This is a python code.
str1 = '//*[#id="rental-asset"]/div/div[1]/div/rdo-nav-card-asset['
str2 = ']/div/div[1]/div/i'
for i in range(1, 22):
str3 = str1 + str(i) + str2
a = driver.find_element_by_xpath(str3).text
print a
You can grab the other elements or data inside the loop.
Comment below if you have any confusion.

You can firstly find the highlight card and use it as the search context of those underline texts.
Code as following:
var activeCard = driver.findElement(By.css('rdo-nav-card-asset > div.rdo-card-highlight'));
// the text of 1rd underline
var title = activeCard.findElement(By.css('.panel-title')).getText();
// the text of 2rd underline
var title = activeCard.findElement(By.xpath('.//div[#class='rdo-card-metric'][1]'))
.getText();
// the text of 3rd underline
var title = activeCard.findElement(By.css('.text-danger')).getText();
// the text of 4rd underline
var title = activeCard.findElement(By.xpath('.//div[#class='rdo-card-metric'][3]'))
.getText();

Related

Google Docs script, return text value in the middle of specified text

[current result][1]
[1]: https://i.stack.imgur.com/O8AGa.png
The problem is that they all apply to the same line
I want to get the values individually from the same line
Rule
▶random text◁
→ Change only values between ▶ and ◁
Script currently in use
var body = DocumentApp.getActiveDocument().getBody();
var foundElement = body.findText('▶([^\S]+)◁');
while (foundElement != null) {
// Get the text object from the element
var foundText = foundElement.getElement().asText();
// Where in the element is the found text?
var start = foundElement.getStartOffset();
var end = foundElement.getEndOffsetInclusive();
// Set Bold
foundText.setBold(start, end, true);
// Change the Foreground color
foundText.setForegroundColor(start, end, "#ff326d");
// Find the next match
foundElement = body.findText('▶([^\S]+)◁', foundElement);
}
Some regular expressions are not fully supported
As per the documentation A subset of the JavaScript regular expression features are not fully supported, such as capture groups and mode modifiers.
I'd recommend to open a feature request on Google Issue Tracker as recommended here

Selenium webdriver: How to check an img element really have height attribute

in java Selenium web driver on chrome, I need to make sure that an image has a height attribute. The HTML code I have is this:
<img src="https://www.w3schools.com/tags/img_girl.jpg">
and I'm trying to execute
boolean s = driver.findElement(By.tagName("img")).getAttribute("height") !=null;
but this always returns true although there's no height attribute! and if I try to execute
String s = driver.findElement(By.tagName("img")).getAttribute("height");
I'll get 600 despite there is no height attribute in the HTML code. is there any other way I can check it?
Try this example:
#Test
public void testHeightAttribute(){
driver.get("https://webelement.click/en/welcome");
WebElement img = driver.findElement(By.xpath("//img[1]"));
System.out.println(img.findElements(By.xpath(".[#height]")).size());
System.out.println(img.findElements(By.xpath(".[#src]")).size());
}
I am not sure this is the most elegant way though :). The idea is that you're looking up elements using the element itself as a search context (looking up element within itself). If no attribute is present in DOM then the list would be empty (size() == 0). Otherwise it would be 1.
Basically the common method would look like:
public boolean hasAttributeInDom(WebElement element, String attr){
return !element.findElements(By.xpath(".[#" + attr + "]")).isEmpty();
}
You can check height parameter in styles section of Developer tools. Refer to attached screen shot of sample image from Stack overflow. You can see height parameter at right hand side. Generally, dimension are mentioned here.
You will need to understand and look for particular image property in Styles section as its display CSS properties for entire page.

How to Read text filled in a Text Box

Here I am Providing u the Text Box Image with its X-path
and Here is the Code for it, that I had tried:
{
String NameTxtBxData = driver.findelement(By.id("ngoName")).gettext();
System.out.println(NameTxtBxData);
}
The element with id "ngoName" is an input. The text from an input is defined in the value property/attribute:
String NameTxtBxData = driver.findElement(By.id("ngoName")).getAttribute("value");
Hi to read a value form a text filed or any input filed there is a hidden attribute value which keeps the value entered by you inside it hence to read the value in the text box simply do it like below
String NameTxtBxData = driver.findelement(By.id("ngoName")).getAttribute("value");
System.out.println(NameTxtBxData);
Hope this helps you

Create new paragraph with Docx4j

I'm having problem creating a Paragraph with docx4j. Well, actually not the paragraph itself, but it's contents. I'm putting together a new document from paragraphs (actually "blocks" made of paragraphs) and everything is working fine. I'm appending them to a list, and when all needed paragraphs are there, I assemble the document. Now, between these blocks, I need new paragraphs, with custom text added. I'm using this function to create the paragraph:
private P createParagraph(String content) {
P result = factory.createP();
R run = factory.createR();
Text text = factory.createText();
text.setValue(content);
run.getContent().add(text);
result.getContent().add(run);
System.out.println("HEADER : " + result.toString());
return result;
}
The print only prints "HEADER : ", the result.toString() is an empty string. Why is that?
BONUS question : I did not want to open a new thread for this. Is it possible, to add an id for a paragraph, which will appear in the generated html? (like p id="xyz" ...>
Thank you very much!
If you want to see the XML your P object will become, use:
System.out.println(
XmlUtils.marshaltoString(result, true, true) );
org.docx4j.wml.P is a class generated by JAXB's xjc.
There are a couple of plugins listed at https://java.net/projects/jaxb2-commons/pages/Home which we could have used to generate a toString method, but we didn't.
If you want the text content of the paragraph, you can use TextUtils

Rally Cardboard render : Add item to card header

I want to add a value from a custom user attribute to the header of the cards in the default cardboard Kanban app. I added the following to the render function :
.....
var idDiv = document.createElement("div");
dojo.addClass(idDiv, "leftCardHeader");
header.appendChild(idDiv);
// MY NEW CODE
if(item.Domain && item.Domain.length > 0) {
var domainDiv = document.createElement("div");
domainDiv.appendChild(document.createTextNode(
" Domain: " + item.Domain));
header.appendChild(domainDiv);
}
// END MY NEW CODE
var ownerImg = document.createElement("img");
dojo.addClass(ownerImg, "cardOwner");
var ownerName = document.createElement("div");
dojo.addClass(ownerName, "cardOwnerName");
header.appendChild(ownerImg);
header.appendChild(ownerName);
....
This adds the text value to the card header but in doing so it pushes the owner name and image down a row in alignment. I have looked at the CSS but don't see any formating that is sting length dependent but I am still relatively new to this area. I tried changing the font size in the CSS but that didn't change anything adding the above code always pushes the owner name and owner image down a line.
Any help on what I might be doing wrong or if there is a cleaner way to dothis is appreciated.
You are appending a div, which is a block element- that is why you are getting the new line. You could either add a style of float:left to this element so it lines up next to the id or you could put the div in the card body instead- you may find it looks better there (especially on narrow width cards).