Xpath command in importXML - xpath

I need a some help in getting a ImportXML command to return the result I'm looking for.
I'm using Google Sheets.
The XML I'm using looks like this:
<DocumentElement>
<BuyOrder>
<itemName>Name of item 1</itemname>
<itemID>1</itemID>
<maxPrice>1000</maxPrice>
<quantity>100</quantity>
</BuyOrder>
<BuyOrder>
<itemName>Name of item 2</itemname>
<itemID>2</itemID>
<maxPrice>2000</maxPrice>
<quantity>200</quantity>
</BuyOrder>
</DocumentElement>
And so on.
What I need is an XPath expression I can use to get maxPrice on a specific itemID.
The ID for itemID comes from another cell in the sheet.

I think this xpath will help you
xpath = "//itemID[contains(.,'1')]/following-sibling::maxPrice"

Try something like the following:
//DocumentElement/BuyOrder[itemID=2]/maxPrice
http://www.w3schools.com/XPath/xpath_examples.asp has some nice examples for learning XPath. they also have an online XPath tester.
http://www.xpathtester.com/xpath is another online XPath tester.

Related

ImportXML function in Google Sheets produces error 'Imported content is empty'!

Here is the ImportXML formula I am using:
=IMPORTXML("https://finance.yahoo.com/quote/RY.TO/profile",K6)
Cell K6 contains the following xpath query:
//*[#id="Col1-0-Profile-Proxy"]/section/div[1]/div/div/p[2]/strong[1]
I got the xpath query by using the Copy XPath function in Google Chrome (e.g. after inspecting the element I am interested in).
The element I am interested in is the Sector associated with the Royal Bank (e.g. Financial Services)
Any help would be appreciated. Many thanks!!
Using the Copy XPath function is a handy feature. However, the suggested query is usually clumsy and sometimes does not yield the desired result. Here is an alternative approach:
//span[.='Sector']/following-sibling::strong[1]
Select the span that has the innerHtml "Sector" and then select the following strong sibling; finally, we can select the /text() directly too like this:
=IMPORTXML($A$10;"//span[.='Sector']/following-sibling::strong[1]/text()")
which returns: Financial Services

xpath query url with one folder depth only

I am using this XPath query succesfully:
//div[(#class="result")]//a[contains(#href,"pinterest.com")]/#href
The URL I am using the XPath query (with simple_html_dom.php) is this one here.
Now, I would like to find results for pinterest.com/one-folder-deep-only and exclude all URLs deeper than one directory, like pinterest.com/one-folder-deep-only/this or pinterest.com/one-folder-deep-only/this/this. I have no idea if there is a way to achieve that. Have googled a lot, but not found anything. Maybe my search terms weren't the best.
Do you have any ideas? Thanks for helping me here.
I am testing the query using the Chrome XPath Helper.
"//" is to evaluate all levels/depths. Instead use only one "/" for the "a" query to only evaluate immediate children
//div[(#id="first-result")]/a[contains(#href,"url.com")]/#href
Note use of / instead of // before the "a" tag.
Try below XPath to select #href from required anchors only:
//a[contains(#href, "url.com") and not(contains(substring-after(./#href, 'url.com/'), "/"))]/#href
Solution for XPath 2.0:
//a[contains(#href, "url.com") and count(tokenize(#href, "/"))=2]/#href
Note that if in real HTML source href starts-with "http://url.com" you should specify =4 instead of =2

Handling Dynamic Xpath

Am automating things using Selenium. Need your help to handle Dynamic Xpath as below:
Driver.findElement(By.xpath("//[#id='INQ_2985']/div[2]/tr/td/div/div[3]/div")).click();
As above INQ_2985 changes to 2986,2987,2988 etc during each run
HTML CODE:
< div> class="context-menu-item-inner" style="background-image:url(../images/productSmall.png);">Tender Assignment < /div>
Tried different combinations as below but with no success:
// Driver.findElement(By.name("//input[#name='Tender Assignment']")).click();
// Driver.findElement(By.className("context-menu-item-inner")).click();`
Can you help me on this.
you can try using contains() or starts-with() in xpath,
above xpath can be rewritten as follows,
Driver.findElement(By.xpath("//*[starts-with(#id,'INQ')]/div[2]/tr/td/div/div[3]/div")).click();
if you can post more of your html, we can help improve your xpath..
moreover using such long xpath's is not recommended, this may cause your test to fail more often
for example,if a "new table data or div" is added to the UI, above xpath will no longer be valid
you should try and use id, class or other attributes to get closer to the element your trying to find
i personally recommend using cssSelectors over xpath
you can use many methods,
use implicity wait;
driver.findElement(By.xpath("//*[contains(#id,'select2-result-label-535')]").click();
driver.findElement(By.xpath("//*[contains(text(), 'select2-result-label-535')]").click();
Good to use Regular expression
driver.findElement(By.xpath("//*[contains(#id,'INQ_')]")
Note: If you have single ID with name starts from INQ_ then you can take action on the element . If a bunch of ID then you can extract as a List<WebElements> and then match with the specific text of the element ( element.getText().trim() =="Linked Text" and if it matched then take action. You can follow other logic to traverse and match.
you can use css -
div.context-menu-item-inner
Use this xpath:
driver.findElement(By.cssSelector("div.context-menu-item-inner").click();
The best choice is using full xpath instead of id which you can get easily via firebug.
e.g.
/html/body/div[3]/div[3]/div[2]/div/div[2]/div[1]/div/div[1]
if your xpath is varying
Ex: "//*[#id='msg500']" , "//*[#id='msg501']", "//*[#id='msg502']" and so on...
Then use this code in script:
for (int i=0;i<=9;i++) {
String mpath= "//*[#id='msg50"+i+"']";
driver.findElement(By.xpath(mpath)).click();
}

Google Spreadsheet ImportXML error #NA not received any data as a result of XPath queries

I have a problem with scraping one website - motoallegro
I want to get title of all ads in this page
So I set formula in google spreadsheet:
=ImportXML("http://allegro.pl/samochody-149?order=qd&string=Primera+GT&search_scope=automotive&department=automotive";"//header/h2/a/span")
This formula always return #NA error: not received any data as a result of XPath queries
But if I try to get other data from the same page, for example H1 text:
=ImportXML("http://allegro.pl/samochody-149?order=qd&string=Primera+GT&search_scope=automotive&department=automotive";"//h1/span")
The result is correct: "Primera GT"
I want to add, that xPath rule - //header/h2/a/span IS CORRECT. I tested it on few firefox xPath plugins.
Any ideas, why google spreadsheet formula ImportXML with correct xPath rule not return correct data?
Google seems to strip non-HTML4-tags like <header/> and <section/>. You could use <div id="listing">...</div> for accessing only the headlines you need.
Try this XPath expression:
//div[#id='listing']//h2/a/span

Select element with a changing Id string using XPath

I have a textarea control with an Id that goes something like this:
<textarea id="NewTextArea~~51887~~1" rows="2"/>
And the xpath that has worked before has been
//textarea[#id, "NewTextArea~~51887~~1"]
But now the '51887' portion of the id is become diverse (changing every time) so I need to select the NewtextArea~~*~~1 element without actually specifying the number. Is there a way I can wildcard part of the string so that it will match a particular pattern? I tried using starts-with and ends-with but couldn't get it to work:
//textarea[starts-with(#id, 'NewTextArea~~') and ends-with(#name, '~~1')]
Bare in mind there are other fields with the difference being the number on the end.
Any advice or guidance would be greatly appreciated :)
I tried using starts-with and ends-with but couldn't get it to work:
//textarea[starts-with(#id, 'NewTextArea~~') and ends-with(#name, '~~1')]
ends-with() is available as a standard function only in XPath 2.0 and you seem to be using XPath 1.0.
Use:
//textarea
[starts-with(#id, 'NewTextArea~~')
and
substring(#id, string-length(#id) - 2) = '~~1'
]
Explanation:
See the answer to this question, for how to implement ends-with() in XPath 1.0:
https://stackoverflow.com/a/405507/36305

Resources