locked
Can XPath run under PowerShell? RRS feed

  • Question

  • I would like to know on whether PowerShell is able to run XPath statement or not.

    $sXpath = "//li[@class='sbsb_c gsfs')]//div[@class='sbqs_c']"

    Does anyone have any suggestions?
    Thanks in advance for any suggestions


    Thanks in advance for any suggestions

    • Moved by Bill_Stewart Wednesday, May 30, 2018 6:27 PM Question way, way outside forum scope
    Monday, February 12, 2018 11:27 AM

All replies

  • yes,

    if xml is

    <?xml version="1.0"?>
    <parent xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
    <child name='r'>tt</child>
    <child>rr</child>
    </parent>

    $r=[xml](cat C:\test.xml) $r.SelectNodes('//parent/child[@name="r"]') #output name #text ---- -----

    $r.SelectNodes('//parent/child') #output name #text ---- ----- r tt child rr



    Regards kvprasoon

    Monday, February 12, 2018 6:36 PM
  • if xml is

    <?xml version="1.0"?>
    <parent xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
    <child name='r'>tt</child>
    <child>rr</child>
    </parent>

    $r=[xml](cat C:\test.xml) $r.SelectNodes('//parent/child[@name="r"]') #output name #text ---- -----

    $r.SelectNodes('//parent/child') #output name #text ---- ----- r tt child rr


    Referring to following image, I would like to know on what PowerShell function should be used to perform following tasks as shown below:

    Step 1 Open "Google.com"

    Step 2 Input "k" into searching bar

    Step 3 Retrieve the display results

    $r.SelectNodes('//li[@class="sbsb_c gsfs")]//div[@class="sbqs_c"]')

    Step 4 Output the results into C:\Sample.txt file

    $r.SelectNodes('//????')

    Do you have any suggestions on what PowerShell function should be used?
    Thank you very much for any suggestions (^v^)






    Thanks in advance for any suggestions

    Monday, February 12, 2018 10:38 PM
  • It will be an interesting exercise for you.

    Below link should help you. Try to explore the output of the Navigate() method. Give some time to this exercise, it will really help you.

    https://cmdrkeene.com/automating-internet-explorer-with-powershell


    Regards kvprasoon

    Tuesday, February 13, 2018 3:49 AM
  • It will be an interesting exercise for you.

    Below link should help you. Try to explore the output of the Navigate() method. Give some time to this exercise, it will really help you.

    https://cmdrkeene.com/automating-internet-explorer-with-powershell


    I get luck to input "k" on searching bar, but don't know on what function can retrieve the list of texts and output them into C:\Sample.txt as shown below:

    $loginUrl = "http://www.google.com";
    #initialize browser
    $ie = New-Object -com internetexplorer.application;
    $ie.visible = $true;
    $ie.navigate($loginUrl);
    while ($ie.Busy -eq $true) { Start-Sleep -Seconds 1; }    #wait for browser idle
    ($ie.document.getElementsByName("q") |select -first 1).value = "k";

    $r.SelectNodes('//li[@class="sbsb_c gsfs")]//div[@class="sbqs_c"]')

    Do you have any suggestions on what PowerShell function should be used?
    Thank you very much for any suggestions (^v^)

    Thanks in advance for any suggestions

    Tuesday, February 13, 2018 11:03 AM
  • $ie.document.getElementsByTagName('b')|select -ExpandProperty innertext|%{"K$_"}

    Explore the output, you will have more fun


    Regards kvprasoon


    Tuesday, February 13, 2018 7:49 PM
  • $ie.document.getElementsByTagName('b')|select -ExpandProperty innertext|%{"K$_"}

    Explore the output, you will have more fun


    $loginUrl = "http://www.google.com";
    #initialize browser
    $ie = New-Object -com internetexplorer.application;
    $ie.visible = $true;
    $ie.navigate($loginUrl);
    while ($ie.Busy -eq $true) { Start-Sleep -Seconds 1; }    #wait for browser idle
    ($ie.document.getElementsByName("q") |select -first 1).value = "k";
    $ie.document.getElementsByTagName('b') | select -ExpandProperty innertext|%{"K$_"}
    foreach ($element in $ie.document.getElementsByTagName('b'))
    {
        Write-Host $element.innerText
    }

    Q1 : Referring to above coding, I would like to know on where to find getElementsByTagName('b'), should it be getElementsByTagName("q")? I cannot find any items with "b" as shown below image

    Q2 :  I don't get luck on retrieving innerText on following scripts, do you have any suggestions on what wrong it is?

    foreach ($element in $ie.document.getElementsByTagName('b'))
    {
        Write-Host $element.innerText
    }

    Q3 : Furthermore, would it be possible to use Invoke-WebRequest instead of $ie.navigate($loginUrl) under this situation?

    Do you have any suggestions?
    Thank you very much for any suggestions (^v^)


    Thanks in advance for any suggestions


    • Edited by oemMicrosoft Tuesday, February 13, 2018 11:32 PM
    Tuesday, February 13, 2018 11:32 PM
  • Does anyone have any suggestions?
    Thanks, to everyone very much for any suggestions (^v^)

    Thanks in advance for any suggestions

    Friday, February 16, 2018 1:51 PM
  • The information you seek is not available outside of the GUI for many reasons.  One reason is that the list is auto filled via a callback (Ajax) and the list is removed as soon as the browser losses focus.

    Most HTML is not XML compliant. HTTP5 is if it is decalred with a strict doctype.

    Using the HTML object model you can query by tagname, id, name, classname or other attributes of the object.  You will have to dig deep to find the object you seek but it will always return no values.

    The developer toolbar can find  the box easily because it is running inside of the browser and is controlling the page.

     


    \_(ツ)_/

    Friday, February 16, 2018 2:02 PM
  • so I would like to know on what function can be used within PowerShell for collecting innerText under this situation.

    Do you have any suggestions?
    Thanks, to everyone very much for any suggestions (^v^)


    Thanks in advance for any suggestions

    Friday, February 16, 2018 9:44 PM
  • Could you please reply and confirm on whether PowerShell's function can collect the innerText or not?

    Do you have any suggestions?
    Thanks, to everyone very much for any suggestions (^v^)


    Thanks in advance for any suggestions

    Saturday, February 17, 2018 2:42 AM
  • The methods (functions) you are calling are not part of PowerShell.  They are part of the IE HTMLDocumentClass Net object and yes,  "innerText" is a property of the objects.

    There is no XML and the HTML is mostly Ajax so it is not always loaded or "gettable" using code.

    S D:\scripts> $ie.Document.GetType()
    
    sPublic IsSerial Name                                     BaseType
    ------- -------- ----                                     --------
    rue     False    HTMLDocumentClass                        System.__ComObject
    

    https://msdn.microsoft.com/en-us/library/system.windows.forms.htmldocument%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

    Each Document "element" will have that property.  The property will be empty for many Ajax supplied values.

    If you would do what has been suggested many time and learn PowerShell instead of just guessing and copying you would be able to figure most of this out by yourself.

    What I have just posted did not come from my head.  I actually just used PS techniques to find the object class and its documentation.  That is how programmers work with hundreds of new APIs.  If we had to go to forums to learn how to do things the whole computer world would gind to a halt.

    You must learn a technology before trying to use it programmatically.  You cannot learn by guessing, copying or asking uninformed questions in a forum.

    Note that you have been asking question ns about PowerShell for much longer than 6 months.  You are still asking the same questions over and over.

    Learn PowerShell  

    I am not trying to chase you away.  I am just trying to get you to put in some effort on your part to help you.

    Do what all trained techs do and study the technologies you wish to work with.  The rest of use have done that and we continue to study the documentation even for older technologies.  No one can know all of this.  We all rely on documentation and restudying the technologies as needed.


    \_(ツ)_/

    Saturday, February 17, 2018 3:47 AM
  • The methods (functions) you are calling are not part of PowerShell.  They are part of the IE HTMLDocumentClass Net object and yes,  "innerText" is a property of the objects.

    There is no XML and the HTML is mostly Ajax so it is not always loaded or "gettable" using code.

    S D:\scripts> $ie.Document.GetType()
    
    sPublic IsSerial Name                                     BaseType
    ------- -------- ----                                     --------
    rue     False    HTMLDocumentClass                        System.__ComObject

    https://msdn.microsoft.com/en-us/library/system.windows.forms.htmldocument%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396


    If it belongs to IE HTMLDocumentClass Net object, could you have any suggestions on what kind of forum should I go to for this issue?

    Do you have any suggestions?
    Thanks, to everyone very much for any suggestions (^v^)


    Thanks in advance for any suggestions

    Saturday, February 17, 2018 5:33 AM
  • You don't need a forum.  You need to learn PowerShell and the Net framework as well as the IE object model.  These are all programming issues.  If you are not a programmer then this may be beyond your understanding.

    Keep studying basic computer technology and software engineering until you basic skills can help you understand this.

    Most of us have learned basic computer engineering and software engineering before trying to enter the world of technology as professionals.  If you do not have this training you need to acquire it.


    \_(ツ)_/

    Saturday, February 17, 2018 6:54 AM
  • Can you show me any example? I learn from example

    Do you have any suggestions?
    Thanks, to everyone very much for any suggestions (^v^)


    Thanks in advance for any suggestions

    Saturday, February 17, 2018 9:00 AM
  • Can you show me any example? I learn from example

    Do you have any suggestions?
    Thanks, to everyone very much for any suggestions (^v^)


    Thanks in advance for any suggestions

    You MUST start your learning by studying the tutorial.  No example can teach you when you do not know the fundamentals.

    Stop being lazy and study the tutorials until you understand what PowerShell is and how it works.

    This is your responsibility.  These forums are not free personal training forums.


    \_(ツ)_/

    Saturday, February 17, 2018 9:02 AM
  • Do you know another PowerShell forum?

    There is not much people on this forum

    Do you have any suggestions?
    Thanks, to everyone very much for any suggestions (^v^)


    Thanks in advance for any suggestions

    Saturday, February 17, 2018 9:29 AM
  • Seems like you don't even have patience to search for PowerShell forums...Google will show you...

    www.powershell.org

    www.powershell.com

    and much more... 

    Give some time to learn it, invest your time in learning, you will get the result. This is from my experience


    Regards kvprasoon


    Saturday, February 17, 2018 10:43 AM